0

我正在尝试使用 php 从数据库中删除记录。这应该在我单击按钮时发生,没有显示错误并且查询出现在屏幕上,但记录仍保留在数据库中

phpmyadmin 给了我以下代码来使用: DELETE FROM 'the shop'.'customer' WHERE 'customer'.'CustomerID' = 8

<?php
$host="localhost"; // Host name
$tbl_name="customer"; // Table name
$db_user="root";
$db_pass="";

$connect = mysql_connect("$host", "$db_user", "$db_pass");
$db_name="the_shop"; // Database name
mysql_select_db("$db_name");
if (!$connect)
 {
 die("MySQL could not connect!");
 }

if(isset($_GET['submit2'])){

$db_username = $_GET['username'];

$sql4 = "DELETE FROM 'the_shop'.'customer' WHERE 'customer'.'CustomerID' = 8"
or die('error deleting record');
mysql_query($sql4);
echo $sql4;
}
?>

我知道这只会删除 CustomerID = 8 的记录

任何帮助表示赞赏

4

6 回答 6

3

您正在使用引号而不是反引号

$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8";

此外,您不需要反引号(在这种情况下,因为您在这里没有使用任何保留关键字)以及您die()在错误的地方使用

于 2013-05-10T10:15:02.090 回答
1

你的说法不正确。您使用引号而不是反引号。但是你可以让你的陈述更容易。

$sql4 = "DELETE FROM customer WHERE CustomerID = 8";
于 2013-05-10T10:17:19.240 回答
1
Use this,It is working.
<?php
$host="localhost"; // Host name
$tbl_name="customer"; // Table name
$db_user="root";
$db_pass="";

$connect = mysql_connect("$host", "$db_user", "$db_pass");
$db_name="the_shop"; // Database name
mysql_select_db("$db_name",$connect);
if (!$connect)
{
 die("MySQL could not connect!");
  }

if(isset($_GET['submit2'])){

$db_username = $_GET['username'];

$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8";

mysql_query($sql4,$connect) or die('error deleting record');
echo $sql4;
}

?>

于 2013-05-10T10:37:42.703 回答
1
$sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8"
mysql_query($sql4);or die('error deleting record');
echo $sql4;
于 2013-05-10T10:23:49.080 回答
0
  1. 您无需在查询中指定要查询的数据库。这就足够了:

    DELETE FROM customer WHERE CustomerID = 8
    
  2. 不推荐使用 Mysql 扩展。这意味着它不再被 PHP 支持并且不应该被使用。改用mysqlipdo

于 2013-05-10T10:17:27.583 回答
0

你可以用这个。您无需指定数据库。

delete from customer where CustomerID = 8
于 2013-05-10T11:46:32.400 回答