【问题标题】:PHP MYSQL $_GET['ID'] from table on dynamically generated pagePHP MYSQL $_GET['ID'] 来自动态生成页面上的表
【发布时间】:2016-01-28 12:49:59
【问题描述】:

我有一些产品存储在一个名为“product_id”的具有自动递增 ID 的表中。

我设法创建了一个页面,该页面显示从数据库中选择的产品名称列表,并为每个产品动态创建链接。假设我有产品苹果。当我点击苹果时,它会将我带到 view_product_details.php?id=9

但是当我点击苹果时,view_product_details.php 页面告诉我 "注意:未定义索引:第 16 行 C:\xampp\htdocs\working\product-website-exercise\view_product_details.php 中的 product_id"

<?php 

//$result = mysql_query("SELECT * FROM temaproduct.products WHERE ID = '".mysql_real_escape_string($_GET['product_id'])."'");


        $id = $_GET['product_id']; //This is line 16

        $result = mysqli_query($conn,"SELECT * FROM products WHERE ID = $id");

        echo $result['product_description'];

echo "<br>";
var_dump($result);

?>

我尝试了不同的查询,但无法弄清楚,请帮助我正确建立连接,以便我可以根据 product_id 从 view_product_details 页面上的表中读取其他字段。

编辑:谢谢你们,在你们的帮助下,如果每个人都需要这个 sn-p,这里是现在可以工作的代码:

<?php
$id = intval($_GET['id']); 
$sql = mysqli_query($conn,"SELECT * FROM products WHERE product_id = ".$id);
if(mysqli_num_rows($sql)){
    $product_data = mysqli_fetch_array($sql);
    echo "<h2><center>".$product_data['title']."</h2></center>";
}
?>

【问题讨论】:

  • 显示你是如何通过product_id的?
  • 我不明白你的意思。
  • 使用$_GET['id'] 而不是$_GET['product_id']
  • 然后使用 mysql_fetch_* 从数据库中获取行:) @ThomasRollet

标签: php mysql database get


【解决方案1】:

您在此 URL 中使用 id 作为查询字符串:

view_product_details.php?id=9

所以,你需要获取 id 为:

$id = $_GET['id']; //This is line 16

您的代码中的第二个问题是,如果不使用mysqli_fetch_* 函数,您将无法从数据库中获取结果。

echo $result['product_description']; // this will return nothing

您修改后的代码:

<?
$id = intval($_GET['id']); 
$sql = mysqli_query($conn,"SELECT * FROM products WHERE ID = ".$id);
if(mysqli_num_rows($sql)){
    $result = mysqli_fetch_array($sql);
    echo $result['product_description'];
}
else{
    echo "No record found";
}
?>

建议:

您还需要做一件事,请使用intval()函数,如果任何一个传递字符串或查询字符串中的任何其他内容都不会返回错误,只返回0条记录,例如:

示例:

view_product_details.php?id=abcdJUNK

比将其转换为0为:

$id = intval($_GET['id']); 

对于未来的访客:

调试后发现这个错误“Unknown Column ID

所以正确的查询是 OP 提到的(column name was product_id):

SELECT * FROM hangouts WHERE product_id = 9

【讨论】:

  • @J.Doe:检查您的连接 $result 如果您的连接工作正常,将返回一个资源 id..
  • 你检查过代码中的 $conn 连接了吗?你得到了什么 var_dump($sql);如果不是检查您的连接,这应该返回一个资源 ID。 @J.Doe
  • 不要有那种感觉,至少你想杀死动物。加入聊天@J.Doe
  • 好吧,显然,“SELECT * FROM hangouts WHERE ID = 9”是不正确的,如果有人好奇的话,它应该是“SELECT * FROM hangouts WHERE product_id = 9”。
【解决方案2】:

您实际上是在view_product_details.php?id=9 查询参数中传递id 而不是在网址中的product_id

要使用product_id,您可以更改网址,例如view_product_details.php?product_id=9,或者您可以使用$_GET['id']

& 替换这一行

$id = $_GET['product_id'];

有了这个

$id = $_GET['id'];

您可以使用get_defined_vars(http://php.net/manual/en/function.get-defined-vars.php) 检查哪些变量是 可用。

另外我建议您抑制生产错误并显示开发中的所有错误。

隐藏生产中的错误

ini_set("display_errors", 0);
ini_set("log_errors", 1);

显示开发中的错误

error_reporting(E_ALL);
ini_set('display_errors', 1);

【讨论】:

    【解决方案3】:

    根据您提供的代码和 URL,您在变量 id 中而不是在 product_id 中传递值。这是您的 URL view_product_details.php?id=9 这里值在变量 id 中,即 id=9

    $id = $_GET['id']; //This is line 16
    

    【讨论】:

      【解决方案4】:

      这里有两个问题,首先你的 html 形式的 name 属性不同,然后你在 php 中使用的我更改了该问题的 php 代码,第二个问题是你缺少将结果转换为关联数组并直接调用产品描述只是在下面的代码修改版本中阅读了 cmets。

      这是您当前的代码

          $id = $_GET['product_id']; //This is line 16
      
          $result = mysqli_query($conn,"SELECT * FROM products WHERE ID = $id");
      
          echo $result['product_description'];
      

      应该是这样的

          // this is the solution for first issue
          $id = $_GET['id']; //This is line 16
      
          $result = mysqli_query($conn,"SELECT * FROM products WHERE ID = $id");
      
          // this is the solution for second issue
          // covert it to associative array
          $resultData = mysqli_fetch_assoc($result);
      
          echo $resultData['product_description'];
      

      【讨论】:

      • 把 $_GET['product_id'] 换成 $_GET['id'] :)
      • 早期每个人都是新手和困惑我的兄弟快乐编码.. :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2012-12-14
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多