【问题标题】:record won't delete from database记录不会从数据库中删除
【发布时间】:2013-05-05 00:02:07
【问题描述】:

我正在尝试使用 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 的记录 我的意图是,一旦这个工作,我将用用户名替换 CustomerID,用相关变量替换“8”,这些变量将通过表单给出一个值

感谢任何帮助

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您使用引号而不是反引号

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

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

    【讨论】:

      【解决方案2】:
      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;
      }
      

      ?>

      【讨论】:

      • 我不知道你改变了什么,所以我很难从你的回答中学习,你能告诉我吗?我已经做出了改变,你是对的,它现在正在工作:)
      • 你可以检查 'the_shop'.'customer' 和 the_shop.customer 之间的区别,在你和 rakesh 的 $sql4 中检查。即你使用了“'”,你应该使用“`”。
      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:
        $sql4 = "DELETE FROM `the_shop`.`customer` WHERE `customer`.`CustomerID` = 8"
        mysql_query($sql4);or die('error deleting record');
        echo $sql4;
        

        【讨论】:

          【解决方案5】:
          1. 您无需在查询中指定要查询的数据库。 这样就足够了:

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

          【讨论】:

          • 单引号无效
          【解决方案6】:

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

          delete from customer where CustomerID = 8
          

          【讨论】:

            猜你喜欢
            • 2020-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-04
            • 1970-01-01
            • 2016-10-16
            相关资源
            最近更新 更多