【问题标题】:POST Doesnt work on my ComputerPOST 在我的电脑上不起作用
【发布时间】:2017-07-01 17:44:39
【问题描述】:

我试图使用 Post 从我的表单中读取一些内容,但即使是来自 https://www.w3schools.com/php/php_forms.asp 的简单示例也不起作用。我自己的代码是这样的:

<form id="eingabe" method="post" action="../php/suche.php">
    <input id="suche" name="suche" type="text" size="50px" placeholder="Suche">
    <input type="submit" value="Submit">
</form>

suche.php

 $rows = array();
   if (isset($_POST['suche'])) {
       $suche = $_POST['suche'];
       $sql= "SELECT * FROM Buch WHERE  titel LIKE '%" . $suche . "%' OR autor LIKE '%" . $suche  ."%' OR isbn LIKE '%" . $suche  ."%' OR genre LIKE '%" . $suche  ."%'";
       $result=$conn->query($sql);
       if ($result->num_rows > 0) {
          while ($row=$result->fetch_assoc()) {
              $titel = $row['titel'];
              $autor = $row['autor'];
              $isbn = $row['isbn'];
              $genre = $row['genre'];
              $preis = $row['preis'];
              $bild = $row['image'];
              $beschreibung = $row['beschreibung'];
              $rows[] = $row;
          }
      }
  }

var_dump($_POST) 始终是array(0) { },但在var_dump($GLOBALS) 中,我可以找到我发送的单词:

array(6) { ["HTTP_RAW_POST_DATA"]=> string(10) "suche=test" ["_GET"]=> array(0) { } ["_POST"]=> array(0) { }...

问题是:

我正在使用 php7.1,但已经尝试过旧版本。 如果我更改为 GET 它可以工作,但我真的需要使用 POST。 我还检查了我的 php.ini,但 POST 已激活并且有 128MB 的空间供使用。 有谁知道为什么 Post 对我不起作用?

PS:我的一个朋友正在使用完全相同的代码,并且对他非常有效,所以它不是代码

【问题讨论】:

  • 我认为可能是您的浏览器,因为它可能会导致问题。正如您所说,您的代码在朋友 PC 上运行良好。尝试其他浏览器,因为您的浏览器可能会受到影响。
  • 您和您的朋友是否在同一台服务器上运行代码?如果不是,您和您的朋友是否具有相同的服务器配置并重新编写规则?
  • 我们工作正常,只有数据库在服务器上?如果这回答了你的问题
  • PHP 是一种服务器端语言,无论运行什么都是您的服务器。
  • 所以我们都使用 PHP 版本 7.0.20RC1 运行,甚至使用相同的 php.ini

标签: php html forms post


【解决方案1】:

你可以这样写你的代码 -

 $rows = array();
 if($_SERVER["REQUEST_METHOD"] == "POST"){
   $suche = $_POST['suche'];
   $sql= "SELECT * FROM Buch WHERE  titel LIKE '%" . $suche . "%' OR autor LIKE '%" . $suche  ."%' OR isbn LIKE '%" . $suche  ."%' OR genre LIKE '%" . $suche  ."%'";
   $result=$conn->query($sql);
   if ($result->num_rows > 0) {
      while ($row=$result->fetch_assoc()) {
          $titel = $row['titel'];
          $autor = $row['autor'];
          $isbn = $row['isbn'];
          $genre = $row['genre'];
          $preis = $row['preis'];
          $bild = $row['image'];
          $beschreibung = $row['beschreibung'];
          $rows[] = $row;
      }
      echo "<pre>";
      print_r($rows);
  }
 }else{
   echo "POST method not working";
 }

如果您的 POST 方法不起作用,那么您将收到一条消息“POST 方法不起作用”,如果您的 POST 方法起作用,那么您将使用 print_r() 函数获取数据。

注意-此代码仅用于测试目的,请根据需要修改。

【讨论】:

  • "注意:未定义的偏移量:0 in..."
  • 应该是if($_SERVER["REQUEST_METHOD"] == "POST"){ - 右方括号放错了位置。
  • 抱歉输入错误
  • 我做了,没有警告,但仍然没有变化,$_POST 中仍然没有内容
  • 如果您没有得到 POST 方法不起作用。这意味着 POST 方法正在工作。我可能还有其他问题。尝试打印$suche,并查看数据是否来自表单。如果数据来了,则打印您的查询字符串($sql)并复制查询并尝试直接执行到 phpmyadmin 并查看查询是否有效。 @memporer
猜你喜欢
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
相关资源
最近更新 更多