我正在尝试使用ProductID
创建一个有效的搜索字段,然后使用它在ProductID
存在的产品表中显示该行。我遇到的问题是,如果我具有以下代码:以下提交按钮将执行$deleteprod
和$deltesale
代码,根据我的理解,所有代码应提交ProductID
变量。
<form action="delete.php" method="post">
<input value="" placeholder="Product ID" name="ProductID" type="text"/> <br>
<input type="submit" onclick="" value="Search"/
</form>
<?php
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
$deleteprod = mysql_query("DELETE FROM gameshop.product WHERE (product.ProductID = '$ProductID')");
$deletesale = mysql_query("DELETE FROM gameshop.sale WHERE (sale.ProductID = '$ProductID')");
$deletebutton = "<button onclick=\"$deletesale; $deleteprod; location.href='database.php'\" style='width:200px; background-color:red;'>Delete Row</button>";
mysql_select_db("gameshop",$db);
$result = mysql_query("SELECT product.*, sale.Price
FROM gameshop.product, gameshop.sale
WHERE (product.ProductID = '$ProductID' AND sale.ProductID = '$ProductID')",$db);
if (!$result){
print mysql_rror();
}
elseif ($myrow = mysql_fetch_array($result)){
echo "<table style='text-align:center;' border=1>\n";
echo "<tr><td>Product ID</td><td>Product Name</td>", "<td>Publisher</td><td>Developer</td>", "<td>Release Date</td><td>Stock</td>", "<td>Console</td> <td>Rank</td><td>Price</td><td>Delete?</td></tr>\n";
do{
printf("<tr><td>%4d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%1d</td><td>%s</td><td>%1d</td><td>%2d</td><td>%s</td></tr>\n", // %s are syntax (string)
$myrow["ProductID"], $myrow["ProductName"], $myrow["Publisher"], $myrow["Developer"], $myrow["ReleaseDate"], $myrow["Stock"], $myrow["Console"], $myrow["Rank"], $myrow["Price"], $deletebutton);
}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
mysql_close($db);
?>
如果我将$db = MySQL_connect("localhost:3307", "root", "usbw");
移到$delete
变量下面,则不会发生此问题,它会创建单行表。但是,当尝试单击$deletebutton
时,该代码不会执行,并且不会删除任何行。
我究竟做错了什么?
答案 0 :(得分:0)
问题是,当您单击按钮时,您调用的是JavaScript,而不是PHP。
我认为您可以解决它,使用deletesale
和deleteprod
制作另一个PHP页面,而将Product_ID
和post
一起使用。
因此此文件将具有如下所示的php脚本:
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
mysql_select_db("gameshop",$db);
$result = mysql_query("SELECT product.*, sale.Price
FROM gameshop.product, gameshop.sale
WHERE (product.ProductID = '$ProductID' AND sale.ProductID = '$ProductID')",$db);
if (!$result){
print mysql_error();
}
elseif ($myrow = mysql_fetch_array($result)){
echo "<table style='text-align:center;' border=1>\n";
echo "<tr><td>Product ID</td><td>Product Name</td>", "<td>Publisher</td> <td>Developer</td>", "<td>Release Date</td><td>Stock</td>", "<td>Console</td> <td>Rank</td><td>Price</td><td>Delete?</td></tr>\n";
do{
printf("<tr><td>%4d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%1d</td><td>%s</td><td>%1d</td><td>%2d</td><td>",$myrow["ProductID"], $myrow["ProductName"], $myrow["Publisher"], $myrow["Developer"], $myrow["ReleaseDate"], $myrow["Stock"], $myrow["Console"], $myrow["Rank"], $myrow["Price"]);
printf "<form method=\"post\" action=\"delete_page.php\"><input type=\"text\" style=\"display:none\" name=\"ProductID\" value=\"$ProductID\"/><input type=\"submit\" value=\"delete product\"/></form>"
printf "</td></tr>"
}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
mysql_close($db);
然后您将delete_page.php
与
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
mysql_select_db("gameshop",$db);
mysql_query("DELETE FROM gameshop.product WHERE (product.ProductID = '$ProductID')");
mysql_query("DELETE FROM gameshop.sale WHERE (sale.ProductID = '$ProductID')");
mysql_close($db);
然后您可以将链接添加回到首页。
请确保检查拼写错误:)