记录不会从数据库中删除

时间:2013-05-10 10:13:36

标签: php mysql

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

phpmyadmin为我提供了以下代码:DELETE FROM'the shop'。'customer'WHERERE'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的记录 我的意图是,一旦这个工作,我将用Username替换CustomerID,将'8'替换为将通过表单给出值的相关变量

感谢任何帮助

6 个答案:

答案 0 :(得分:3)

您使用的是引号而不是后退

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

此外,您不需要返回刻度(在这种情况下,因为您未在此处使用任何保留关键字)以及您在错误的位置使用die()

答案 1 :(得分:1)

您的陈述不正确。你使用quoted而不是back ticks。但是你可以让你的陈述更容易。

$sql4 = "DELETE FROM customer WHERE CustomerID = 8";

答案 2 :(得分:1)

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

答案 3 :(得分: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;
}

&GT;

答案 4 :(得分:0)

  1. 您无需在查询中指定要查询的数据库。 这就足够了:

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

答案 5 :(得分:0)

你可以使用它。您无需指定数据库。

delete from customer where CustomerID = 8