使用 PHP & JavaScript 从数据库中删除数据
Delete data from database using PHP & JavaScript
我正在处理 PHP 文件。我正在尝试制作一个 table 来显示数据库中的产品列表。还将有一个用于删除任何产品的按钮。我使用 javascript 从数据库中删除产品。我已经写了代码,没有发现任何错误。当我点击删除按钮时,它会显示确认框,但不会删除产品。这是代码:
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("grocery_shop",$con);
error_reporting(E_ALL^E_NOTICE);
session_start();
$sql = mysql_query("select * from products");
if($_GET['did']){
mysql_query("delete from products where product_id='$_GET[did]'");
header("Location: product.php");
}
?>
<table border="1px" style="width:100%">
<tr>
<th>Serial No</th>
<th>Product Name</th>
<th>Product Type</th>
<th>Quantity</th>
<th>Price</th>
<th>Delete Product</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i=1;
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "<a href=\"javascript:delproduct(id=$u[product_id])\">Delete</a>";?></td>
</tr>
<?php
$i++;
}
?>
<script>
function delproduct(id){
var msg = confirm("Are you sure you want to delete this product?");
if (msg) {
window.location = "product.php?did="+product_id;
}
}
</script>
</table>
你忘记了 $_GET 中 did 的 ''[did]:
mysql_query("delete from products where product_id='{$_GET['did']}'");
此外,正如@chris85 指出的那样,直接使用 $_GET 或 $_POST 不是一个好主意,请记住在查询中使用它们之前先清理这些值。
$did = filter_input(INPUT_GET, 'did', FILTER_SANITIZE_NUMBER_INT);
mysql_query("delete from products where product_id={$did}");
您应该尝试:delproduct($u[product_id]) 而不是 delproduct(id=$u[product_id ])
mysql_query("delete from products where product_id='".$_GET['did']."'");
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "<a href=\"javascript:delproduct($u[product_id])\">Delete</a>";?></td>
</tr>
<?php
$i++;
}
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("grocery_shop",$con);
error_reporting(E_ALL^E_NOTICE);
session_start();
$sql = mysql_query("select * from products");
if($_GET['did']){
mysql_query("delete from products where product_id='$_GET[did]'");
header("Location: product.php");
}
?>
<table border="1px" style="width:100%">
<tr>
<th>Serial No</th>
<th>Product Name</th>
<th>Product Type</th>
<th>Quantity</th>
<th>Price</th>
<th>Delete Product</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i=1;
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "<a href=\"javascript:delproduct(".$u[product_id].")\">Delete</a>";?></td>
</tr>
<?php
$i++;
}
?>
<script>
function delproduct(id){
var product_id = id;
var msg = confirm("Are you sure you want to delete this product?");
if (msg) {
window.location = "product.php?did="+product_id;
}
}
</script>
</table>
问题出在你的javascript,product_id不存在
if (msg) {
window.location = "product.php?did="+id;
}
出于调试目的,请尝试将其替换为您的代码,并让我们知道您收到的错误消息。
if($_GET['did']){
mysql_query("delete from products where product_id='".$_GET['did']."'");
echo "delete from products where product_id='".$_GET['did']."'";
echo mysql_errno() . ": " . mysql_error() ;
die();
//header("Location: product.php");
}
此外还尝试 运行 不带单引号的查询,我假设 product_id 是一个整数。
所以mysql_query("delete from products where product_id=".$_GET['did']);
我正在处理 PHP 文件。我正在尝试制作一个 table 来显示数据库中的产品列表。还将有一个用于删除任何产品的按钮。我使用 javascript 从数据库中删除产品。我已经写了代码,没有发现任何错误。当我点击删除按钮时,它会显示确认框,但不会删除产品。这是代码:
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("grocery_shop",$con);
error_reporting(E_ALL^E_NOTICE);
session_start();
$sql = mysql_query("select * from products");
if($_GET['did']){
mysql_query("delete from products where product_id='$_GET[did]'");
header("Location: product.php");
}
?>
<table border="1px" style="width:100%">
<tr>
<th>Serial No</th>
<th>Product Name</th>
<th>Product Type</th>
<th>Quantity</th>
<th>Price</th>
<th>Delete Product</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i=1;
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "<a href=\"javascript:delproduct(id=$u[product_id])\">Delete</a>";?></td>
</tr>
<?php
$i++;
}
?>
<script>
function delproduct(id){
var msg = confirm("Are you sure you want to delete this product?");
if (msg) {
window.location = "product.php?did="+product_id;
}
}
</script>
</table>
你忘记了 $_GET 中 did 的 ''[did]:
mysql_query("delete from products where product_id='{$_GET['did']}'");
此外,正如@chris85 指出的那样,直接使用 $_GET 或 $_POST 不是一个好主意,请记住在查询中使用它们之前先清理这些值。
$did = filter_input(INPUT_GET, 'did', FILTER_SANITIZE_NUMBER_INT);
mysql_query("delete from products where product_id={$did}");
您应该尝试:delproduct($u[product_id]) 而不是 delproduct(id=$u[product_id ])
mysql_query("delete from products where product_id='".$_GET['did']."'");
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "<a href=\"javascript:delproduct($u[product_id])\">Delete</a>";?></td>
</tr>
<?php
$i++;
}
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("grocery_shop",$con);
error_reporting(E_ALL^E_NOTICE);
session_start();
$sql = mysql_query("select * from products");
if($_GET['did']){
mysql_query("delete from products where product_id='$_GET[did]'");
header("Location: product.php");
}
?>
<table border="1px" style="width:100%">
<tr>
<th>Serial No</th>
<th>Product Name</th>
<th>Product Type</th>
<th>Quantity</th>
<th>Price</th>
<th>Delete Product</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php
$i=1;
while ($u = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $u['product_name'];?></td>
<td><?php echo $u['product_type'];?></td>
<td><?php echo $u['quantity'];?></td>
<td><?php echo $u['price'];?></td>
<td><?php echo "<a href=\"javascript:delproduct(".$u[product_id].")\">Delete</a>";?></td>
</tr>
<?php
$i++;
}
?>
<script>
function delproduct(id){
var product_id = id;
var msg = confirm("Are you sure you want to delete this product?");
if (msg) {
window.location = "product.php?did="+product_id;
}
}
</script>
</table>
问题出在你的javascript,product_id不存在
if (msg) {
window.location = "product.php?did="+id;
}
出于调试目的,请尝试将其替换为您的代码,并让我们知道您收到的错误消息。
if($_GET['did']){
mysql_query("delete from products where product_id='".$_GET['did']."'");
echo "delete from products where product_id='".$_GET['did']."'";
echo mysql_errno() . ": " . mysql_error() ;
die();
//header("Location: product.php");
}
此外还尝试 运行 不带单引号的查询,我假设 product_id 是一个整数。
所以mysql_query("delete from products where product_id=".$_GET['did']);