简体   繁体   English

从MySQL数据库删除记录

[英]Deleting record from MySQL database

I don't have a lot of experience with MySQL, so there is a very good chance I am missing something very obvious. 我没有使用MySQL的丰富经验,因此很可能会错过一些非常明显的东西。

I am simply trying to delete a record from a table where the (from_=neighbor and to_=id) OR (from_=id and to_=neighbor) . 我只是试图从(from_=neighbor and to_=id) OR (from_=id and to_=neighbor)的表中删除一条记录。 The problem is the record is never deleted. 问题是记录永远不会删除。

  1. The basic database setup (username, password, etc) is correct and is actually in an include file that I have used numerous times. 基本的数据库设置(用户名,密码等)是正确的,并且实际上在我使用过多次的include文件中。
  2. The name of the table is correct, I triple checked. 该表的名称正确,我三重检查。
  3. The names of the columns in the database are correct, I triple checked. 数据库中各列的名称正确,我进行了三遍检查。
  4. I know the script is receiving id and neighbor as per the javascript alert tests, and I know that the particular integers received match a record in the database (I triple checked). 我知道脚本正在按照javascript alert测试接收ID和邻居,并且我知道接收到的特定整数与数据库中的记录匹配(我进行了三遍检查)。

This is the code I am using: 这是我正在使用的代码:

 $dbhost = "..."; 
 $dbuser = "...";
 $dbpass = "...";
 $dbname    = "...";

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql");
    mysql_select_db($dbname);

$id = mysql_real_escape_string($_GET['id']);
$neighbor = mysql_real_escape_string($_GET['neighbor']);
$result = mysql_query("DELETE FROM jointable WHERE to_='". $id ."' AND from_='".$neighbor."'") or die(mysql_error()); 
$result = mysql_query("DELETE FROM jointable WHERE from_='". $neighbor ."' AND to_='".$id."'") or die(mysql_error()); 
mysql_close($conn);     

echo "<script type='text/javascript'>
    alert('$id');
    alert('$neighbor');
    <!-- 
    window.location = 'index.php?node=$id';   
    //-->
    </script>";

Build the query into a separate string, then dump it out to see what's really going on: 将查询构建为单独的字符串,然后将其转储以查看实际情况:

$sql = <<<EOL
DELETE FROM jointable
WHERE (to_ = '$id') AND (from_ = '$neighbor')
EOL;
$result = mysql_query($sql) or die(mysql_error());

die("<pre>$sql</pre> $result");

Note that I'm running the query only once. 请注意,我只运行一次查询。 Your two queries simply rearrange the WHERE clause, which is pointless. 您的两个查询只是重新排列了WHERE子句,这毫无意义。 (P and Q) is exactly the same as (Q and P) in this case. 在这种情况下, (P and Q)(Q and P)完全相同。

Once you've got the query text, try running it manually yourself in the MySQL monitor, and see what's going on. 获取查询文本后,尝试自己在MySQL监视器中手动运行它,然后查看发生了什么。 Turn it into a SELECT query with the same WHERE clause, eg 将其转换为具有相同WHERE子句的SELECT查询,例如

SELECT *
FROM jointable WHERE (to_ = '$id') AND (from_ = '$neighbor')

and see if that returns anything. 看看是否返回任何东西。 Since MySQL isn't spitting out an error on the query, then most likely it's your WHERE clause that's causing it to not find the record you want. 由于MySQL不会在查询中吐出错误,因此很可能是WHERE子句导致它找不到所需的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM