I have a few products stored in a table with auto-incremented ID entitled "product_id".
我有一些产品存储在一个表中,该表具有名为“product_id”的自动递增ID。
I have managed to create a page that displays a list of the products names as selected from the db and dynamically created links for each one. Let's say I have product apple. When I click on apple it takes me to view_product_details.php?id=9
我设法创建了一个页面,显示从db中选择的产品名称列表以及为每个名称动态创建的链接。假设我有产品苹果。当我点击苹果时,它会转到view_product_details.php?id = 9
But when I click apple, the view_product_details.php page tells me "Notice: Undefined index: product_id in C:\xampp\htdocs\working\product-website-exercise\view_product_details.php on line 16"
但是当我点击apple时,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);
?>
I have tried with different queries but can't figure it out, please help me establish the connection properly so I can read the other fields from the table on the view_product_details page, based on product_id.
我尝试了不同的查询,但无法弄明白,请帮我正确建立连接,以便我可以根据product_id从view_product_details页面上的表中读取其他字段。
EDIT: Thank you guys, with your help, here is the code that works now, if everybody needs this snippet:
编辑:谢谢大家,在你的帮助下,这里是现在有效的代码,如果每个人都需要这个片段:
<?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>";
}
?>
4 个解决方案
#1
3
You are using id as a query string in this URL as:
您在此URL中使用id作为查询字符串:
view_product_details.php?id=9
So, you need to get id as:
所以,你需要获得id为:
$id = $_GET['id']; //This is line 16
Second issue in your code is that, you can not get result from database without using mysqli_fetch_*
function.
您的代码中的第二个问题是,如果不使用mysqli_fetch_ *函数,则无法从数据库获取结果。
echo $result['product_description']; // this will return nothing
Your Modified Code:
您的修改代码:
<?
$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";
}
?>
Suggestion:
You need to do one more thing, please use intval()
function if any one pass string or anything else in the query string than your query will not return an error only return 0 record like:
你还需要做一件事,请使用intval()函数,如果任何一个传递字符串或查询字符串中的任何其他内容,则查询不会返回错误,只返回0记录,如:
Example:
view_product_details.php?id=abcdJUNK
Than convert it into 0 as:
将其转换为0为:
$id = intval($_GET['id']);
For Future Visitors:
对于未来的访客:
After debugging, found this error "Unknown Column ID"
调试后,发现此错误“未知列ID”
so correct query was this as OP mentioned (column name was product_id):
所以正确的查询是OP提到的(列名是product_id):
SELECT * FROM hangouts WHERE product_id = 9
#2
1
You are actually passing id
in view_product_details.php?id=9
query params not product_id
in the url.
你实际上是在view_product_details.php中传递id?id = 9查询参数而不是url中的product_id。
To use product_id
you can change the url like view_product_details.php?product_id=9
or you can use $_GET['id']
要使用product_id,您可以像view_product_details.php?product_id = 9一样更改网址,也可以使用$ _GET ['id']
& replace this line
并替换此行
$id = $_GET['product_id'];
with this
$id = $_GET['id'];
You can use
get_defined_vars
(http://php.net/manual/en/function.get-defined-vars.php) to check which variables are available.您可以使用get_defined_vars(http://php.net/manual/en/function.get-defined-vars.php)来检查哪些变量可用。
Also I suggest you suppress your errors for production & show all errors in developement.
此外,我建议您抑制生产中的错误并显示开发中的所有错误。
To hide errors in production
隐藏生产中的错误
ini_set("display_errors", 0);
ini_set("log_errors", 1);
To show errors in developement
显示发展中的错误
error_reporting(E_ALL);
ini_set('display_errors', 1);
#3
1
According to your code and URL that you provide you are passing value in variable id
not in product_id
. This is your URL view_product_details.php?id=9
here value is in variable id
i.e id=9
.
根据您提供的代码和URL,您在变量id中传递的值不在product_id中。这是你的URL view_product_details.php?id = 9这里的值是变量id,即id = 9。
$id = $_GET['id']; //This is line 16
#4
1
two problems here first your name attribute in html form is different then what you are using in php i changed the php code for that issue and second problem is you are missing to convert the result in to assosiative array and directly calling the product description just read the comments in modified version of code down below.
这里的两个问题首先你的html形式的你的名字属性是不同的然后你在php中使用我改变了该问题的PHP代码和第二个问题是你缺少将结果转换为assosiative数组并直接调用产品描述只是读取下面修改后的代码版本中的注释。
this is your current code
这是您当前的代码
$id = $_GET['product_id']; //This is line 16
$result = mysqli_query($conn,"SELECT * FROM products WHERE ID = $id");
echo $result['product_description'];
this is how it should be
这应该是怎么回事
// 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'];
#1
3
You are using id as a query string in this URL as:
您在此URL中使用id作为查询字符串:
view_product_details.php?id=9
So, you need to get id as:
所以,你需要获得id为:
$id = $_GET['id']; //This is line 16
Second issue in your code is that, you can not get result from database without using mysqli_fetch_*
function.
您的代码中的第二个问题是,如果不使用mysqli_fetch_ *函数,则无法从数据库获取结果。
echo $result['product_description']; // this will return nothing
Your Modified Code:
您的修改代码:
<?
$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";
}
?>
Suggestion:
You need to do one more thing, please use intval()
function if any one pass string or anything else in the query string than your query will not return an error only return 0 record like:
你还需要做一件事,请使用intval()函数,如果任何一个传递字符串或查询字符串中的任何其他内容,则查询不会返回错误,只返回0记录,如:
Example:
view_product_details.php?id=abcdJUNK
Than convert it into 0 as:
将其转换为0为:
$id = intval($_GET['id']);
For Future Visitors:
对于未来的访客:
After debugging, found this error "Unknown Column ID"
调试后,发现此错误“未知列ID”
so correct query was this as OP mentioned (column name was product_id):
所以正确的查询是OP提到的(列名是product_id):
SELECT * FROM hangouts WHERE product_id = 9
#2
1
You are actually passing id
in view_product_details.php?id=9
query params not product_id
in the url.
你实际上是在view_product_details.php中传递id?id = 9查询参数而不是url中的product_id。
To use product_id
you can change the url like view_product_details.php?product_id=9
or you can use $_GET['id']
要使用product_id,您可以像view_product_details.php?product_id = 9一样更改网址,也可以使用$ _GET ['id']
& replace this line
并替换此行
$id = $_GET['product_id'];
with this
$id = $_GET['id'];
You can use
get_defined_vars
(http://php.net/manual/en/function.get-defined-vars.php) to check which variables are available.您可以使用get_defined_vars(http://php.net/manual/en/function.get-defined-vars.php)来检查哪些变量可用。
Also I suggest you suppress your errors for production & show all errors in developement.
此外,我建议您抑制生产中的错误并显示开发中的所有错误。
To hide errors in production
隐藏生产中的错误
ini_set("display_errors", 0);
ini_set("log_errors", 1);
To show errors in developement
显示发展中的错误
error_reporting(E_ALL);
ini_set('display_errors', 1);
#3
1
According to your code and URL that you provide you are passing value in variable id
not in product_id
. This is your URL view_product_details.php?id=9
here value is in variable id
i.e id=9
.
根据您提供的代码和URL,您在变量id中传递的值不在product_id中。这是你的URL view_product_details.php?id = 9这里的值是变量id,即id = 9。
$id = $_GET['id']; //This is line 16
#4
1
two problems here first your name attribute in html form is different then what you are using in php i changed the php code for that issue and second problem is you are missing to convert the result in to assosiative array and directly calling the product description just read the comments in modified version of code down below.
这里的两个问题首先你的html形式的你的名字属性是不同的然后你在php中使用我改变了该问题的PHP代码和第二个问题是你缺少将结果转换为assosiative数组并直接调用产品描述只是读取下面修改后的代码版本中的注释。
this is your current code
这是您当前的代码
$id = $_GET['product_id']; //This is line 16
$result = mysqli_query($conn,"SELECT * FROM products WHERE ID = $id");
echo $result['product_description'];
this is how it should be
这应该是怎么回事
// 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'];