PHP通过链接查询字符串参数
我试图在用户单击链接时传递查询字符串 这样,在它指向的下一个页面上,将从mysql检索特定的poduct(查询字符串参数)详细信息 下面是我的代码,我该怎么做呢 当前页面:PHP通过链接查询字符串参数,php,mysql,Php,Mysql,我试图在用户单击链接时传递查询字符串 这样,在它指向的下一个页面上,将从mysql检索特定的poduct(查询字符串参数)详细信息 下面是我的代码,我该怎么做呢 当前页面: <?php // Write a series of five PHP statements to retrieve the data for all product items in the database // Write a statement to prepare the SQL SELECT statem
<?php
// Write a series of five PHP statements to retrieve the data for all product items in the database
// Write a statement to prepare the SQL SELECT statement
$sql = "SELECT skuCode, brand, model FROM productItem;";
// Write a statement to open a connection to MySQL server
$link = mysql_connect("localhost", "root", "password");
// Write a statement to select the required database
mysql_select_db("is1112t4q3", $link);
// Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
$resultset = mysql_query($sql);
// Write a statement to close the connection
mysql_close($link);
// Write a suitable expression for the while loop to iterate through each data row in the resultset.
// Each data row represents one product item that you need to display
while($row = mysql_fetch_array($resultset))
{
// Write a echo command to display the summary information for each product item. The required attributes are
// indicated in the HTML table header row.
// The image that is displayed should be the first image of each product item, e.g., MP300001_1.jpg.
// The Action column should contain a hyperlink to viewProductItem.php where user can view the complete product information
// of the selected product item.
// You need to use a query string parameter to pass the SKU Code of the required product item to viewProductItem.php
echo("<tr><td>".$row[0].
"</td><td>".$row[1].
"</td><td>".$row[2].
"</td><td>"."<img src=images/".$row["skuCode"]."_1.jpg>".
"</td><td><a href=viewProductItem.php?var=skuCode>View</a>".
"</td></tr>");
}
?>
下一页,此页应检索单击链接
时从上一页传递的SKU代码
使用PHP用类似的参数表示URL
viewProductItem.php?skuCode=123&anotherVariable=yes
。。以及类似的后续参数您可以使用Cups描述的GET。然而得到的不是安全的吗
另一种方法是会话变量。这是一种更安全的方法,但并不完全安全
//在第1页(当前页)
//第2页(viewProductItem.php)
您必须重写url,如:在第一页中。不是“var”
"<a href=viewProductItem.php?skuCode=".$row["skuCode"].">View</a>"
你修改的代码是:试试这个,它对你有用。。
第一页:
<?php
// Write a series of five PHP statements to retrieve the data for all product items in the database
// Write a statement to prepare the SQL SELECT statement
$sql = "SELECT skuCode, brand, model FROM productItem;";
// Write a statement to open a connection to MySQL server
$link = mysql_connect("localhost", "root", "password");
// Write a statement to select the required database
mysql_select_db("is1112t4q3", $link);
// Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
$resultset = mysql_query($sql);
// Write a statement to close the connection
mysql_close($link);
// Write a suitable expression for the while loop to iterate through each data row in the resultset.
// Each data row represents one product item that you need to display
while($row = mysql_fetch_array($resultset))
{
// Write a echo command to display the summary information for each product item. The required attributes are
// indicated in the HTML table header row.
// The image that is displayed should be the first image of each product item, e.g., MP300001_1.jpg.
// The Action column should contain a hyperlink to viewProductItem.php where user can view the complete product information
// of the selected product item.
// You need to use a query string parameter to pass the SKU Code of the required product item to viewProductItem.php
echo("<tr><td>".$row[0].
"</td><td>".$row[1].
"</td><td>".$row[2].
"</td><td>"."<img src=images/".$row["skuCode"]."_1.jpg>".
"</td><td><a href=viewProductItem.php?skuCode=".$row['skuCode'].">View</a>".
"</td></tr>");
}
?>
第二页:
<?php
// Complete the if expression using the isset function to determine whether the query string parameter skuCode has been provided
if(isset($_GET["skuCode"]))
{
// Write a series of five PHP statements to retrieve the data for the selected product item from the database
// Write a statement to prepare the SQL SELECT statement. The SQL statement should have a WHERE condition
$sql = "SELECT * FROM productItem WHERE skuCode = '".mysql_real_escape_string($_GET["skuCode"])."';";
// Write a statement to open a connection to MySQL server
$link = mysql_connect("localhost", "root", "password");
// Write a statement to select the required database
mysql_select_db("is1112t4q3", $link);
// Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
$resultset = mysql_query($sql);
// Write a statement to close the connection
mysql_close($link);
// The statement below will retrieve the first data row in the resultset.
// Note that we are only expecting at most one data row since we are filtering by the primary key skuCode
// mysql_fetch_array will an array of values corresponding to the fetched data row.
// If there is no matching data row, mysql_fetch_array will return FALSE
$row = mysql_fetch_array($resultset);
if($row)
{
// Write a series of echo commands to print out the complete information of the selected product item using a HTML table
echo("<tr><td>".$row[0].
"</td><td>".$row[1].
"</td><td>".$row[2].
"</td><td>"."<img src=images/".$row["skuCode"]."_1.jpg>".
"</td><td><a href=viewProductItem.php>View</a>".
"</td></tr>");
}
else
{
echo("<h3 style=\"color: red;\">Product item ".$_GET["skuCode"]." does not exist</h3>");
}
}
else
{
echo("<h3 style=\"color: red;\">No product item has been selected</h3>");
}
我试图修改。但是第二页不起作用。第二页的代码是这样的吗?我已经编辑了第二个页面,现在检查它是否工作。在第二个页面中,查询不返回任何值意味着如果(isset($\u GET[“skuCode”])存在另一个不需要的“}”而该页面不工作,它将转到else部分,而else部分位于该页面之外。现在我已经改变了答案,现在它可以工作了,请现在检查一下。是的,它现在可以工作了。是因为}。谢谢你的帮助。
$var_value = $_SESSION['skuCode'];
"<a href=viewProductItem.php?skuCode=".$row["skuCode"].">View</a>"
$skuCode =mysql_real_escape_string($_REQUEST['skuCode']);
$sql = "SELECT * FROM productItem WHERE skuCode = '".$skuCode."';";
<?php
// Write a series of five PHP statements to retrieve the data for all product items in the database
// Write a statement to prepare the SQL SELECT statement
$sql = "SELECT skuCode, brand, model FROM productItem;";
// Write a statement to open a connection to MySQL server
$link = mysql_connect("localhost", "root", "password");
// Write a statement to select the required database
mysql_select_db("is1112t4q3", $link);
// Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
$resultset = mysql_query($sql);
// Write a statement to close the connection
mysql_close($link);
// Write a suitable expression for the while loop to iterate through each data row in the resultset.
// Each data row represents one product item that you need to display
while($row = mysql_fetch_array($resultset))
{
// Write a echo command to display the summary information for each product item. The required attributes are
// indicated in the HTML table header row.
// The image that is displayed should be the first image of each product item, e.g., MP300001_1.jpg.
// The Action column should contain a hyperlink to viewProductItem.php where user can view the complete product information
// of the selected product item.
// You need to use a query string parameter to pass the SKU Code of the required product item to viewProductItem.php
echo("<tr><td>".$row[0].
"</td><td>".$row[1].
"</td><td>".$row[2].
"</td><td>"."<img src=images/".$row["skuCode"]."_1.jpg>".
"</td><td><a href=viewProductItem.php?skuCode=".$row['skuCode'].">View</a>".
"</td></tr>");
}
?>
<?php
// Complete the if expression using the isset function to determine whether the query string parameter skuCode has been provided
if(isset($_GET["skuCode"]))
{
// Write a series of five PHP statements to retrieve the data for the selected product item from the database
// Write a statement to prepare the SQL SELECT statement. The SQL statement should have a WHERE condition
$sql = "SELECT * FROM productItem WHERE skuCode = '".mysql_real_escape_string($_GET["skuCode"])."';";
// Write a statement to open a connection to MySQL server
$link = mysql_connect("localhost", "root", "password");
// Write a statement to select the required database
mysql_select_db("is1112t4q3", $link);
// Write a statement to send the SQL statement to the MySQL server for execution and retrieve the resultset
$resultset = mysql_query($sql);
// Write a statement to close the connection
mysql_close($link);
// The statement below will retrieve the first data row in the resultset.
// Note that we are only expecting at most one data row since we are filtering by the primary key skuCode
// mysql_fetch_array will an array of values corresponding to the fetched data row.
// If there is no matching data row, mysql_fetch_array will return FALSE
$row = mysql_fetch_array($resultset);
if($row)
{
// Write a series of echo commands to print out the complete information of the selected product item using a HTML table
echo("<tr><td>".$row[0].
"</td><td>".$row[1].
"</td><td>".$row[2].
"</td><td>"."<img src=images/".$row["skuCode"]."_1.jpg>".
"</td><td><a href=viewProductItem.php>View</a>".
"</td></tr>");
}
else
{
echo("<h3 style=\"color: red;\">Product item ".$_GET["skuCode"]." does not exist</h3>");
}
}
else
{
echo("<h3 style=\"color: red;\">No product item has been selected</h3>");
}