我正在尝试用mysql中的删除记录解决问题,请参阅代码
<?php
$dbcs = new mysqli("localhost", "root", "password", "database-name");
// Check connection
if (mysqli_connect_errno($dbcs))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$product_list = "";
$sql = "SELECT * FROM product ORDER BY product_id DESC";
$result=mysqli_query($dbcs,$sql);
while($row = mysqli_fetch_array($result)){
$product_id = $row["product_id"];
$product_name = $row["product_name"];
$product_category = $row["product_category"];
$product_retail_price = $row["product_retail_price"];
$product_price = $row["product_price"];
$product_detail = $row["product_detail"];
$product_image = $row["screenshot"];
$screenshot = $row["screenshot"];
$product_list .= '<table width="80%" border="1">
<tr>
<td width="172" valign="top"> echo <img src="' . GW_UPLOADPATH . $screenshot .'" width="111" height="149" alt="<?php echo $product_name; ?>" /><br /><a href="../product_images/<?php echo $product_id; ?>.jpg">View Full Size Image</a></td>
<td width="85" class="product-text">' . $product_id . '</td>
<td width="402" class="product-text">' . $product_name . '</td>
<td width="108" align="center" class="product-text">' . $product_price . '</td>
<td width="34" align="center" class="product-text"><a href="edit_product.php?pid=' . $product_id . '">Edit</a></td>
<td width="56" align="center" class="product-text"><a href="product.php?deleteid=' . $product_id . '">Delete </a></td>
<td width="56" align="center" class="product-text"><a href="view_product.php?pid=' . $product_id . '">View</a></td>
</tr>
</table> ';
}
if (empty($product_list)) {
$product_list = "You have no product listed in your data yet";
}
mysqli_close($dbcs);
?>
然后这是用于php删除代码
<?php
///////////////////////////////////////////////////////////////////////////
// Delete Item Question to Admin, and Delete Product if they choose
///////////////////////////////////////////////////////////////////////////
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="product.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="product.php">No</a>';
exit();
}
if (isset($_GET['yesdelete'])) {
//////////////////////////////////////////////////////////
// remove item from system and delete its picture
// delete from database
//////////////////////////////////////////////////////////
$id_to_delete = $_GET['yesdelete'];
$sql ("DELETE FROM product WHERE product_id='$id_to_delete' LIMIT 1") or die (mysql_error());
/////////////////////////////////////////////////////////////
// unlink the image from server
// Remove The Pic -------------------------------------------
/////////////////////////////////////////////////////////////
$pictodelete = ("../product_images/$id_to_delete.jpg");
if (file_exists($pictodelete)) {
unlink($pictodelete);
}
header("location: product.php");
exit();
}
?>
当我运行它来选择项目并通过按删除删除并按是,我得到错误说
Fatal error: Call to undefined function SELECT * FROM product ORDER BY product_id DESC() in C:\VertrigoServ\www\shopone\admin\data\product.php on line 131
他的意思是什么,怎么能解决这个问题! AM
答案 0 :(得分:1)
首先,您将字符串保存到变量$sql
...
$sql = "SELECT * FROM product ORDER BY product_id DESC";
...然后你调用一个名称在变量$sql
中表示的函数。
$sql ("DELETE FROM product WHERE product_id='$id_to_delete' LIMIT 1")
您正在使用变量作为函数。我想你想使用函数mysqli_query()
。
答案 1 :(得分:0)
你正在使用$sql
之类的函数,所以PHP试图用它来执行带有名字的函数。
您必须使用mysql_query()
。
另请考虑转移到PDO
,这比弃用mysql_*
更好(prepared statements
也值得学习)。
答案 2 :(得分:0)
错别字:
$sql ("DELETE FROM ...
^---missing =
你很容易被SQL injection attacks攻击。在您了解问题以及如何避免此问题之前, NOT 是否使用此代码。
答案 3 :(得分:0)
我使用您的源代码在我自己的网络服务器上创建了一个表,并总结了以下内容。
但首先,如果您的删除文件/功能位于单独的文件中,那么您将需要该文件中的数据库连接凭据,否则它将无法工作,除非您将其作为包含或全局设置。< / p>
在你的情况下,它会像:
$dbcs = new mysqli("localhost", "root", "password", "database-name");
mysqli_query($dbcs,"DELETE FROM product WHERE product_id='$id_to_delete' LIMIT 1") or die (mysql_error());
替换:
$sql ("DELETE FROM product WHERE product_id='$id_to_delete' LIMIT 1") or die (mysql_error());
使用:
mysqli_query($sql,"DELETE FROM product WHERE product_id='$id_to_delete' LIMIT 1") or die (mysql_error());
或者在您的情况下,它可能是:
mysqli_query($dbcs,"DELETE FROM product WHERE product_id='$id_to_delete' LIMIT 1") or die (mysql_error());