mysql DELETE适用于phpmyadmin但不适用于php脚本

时间:2013-02-20 15:07:13

标签: php mysql

基本上我的问题是,它适用于phpmyadmin而不是php本身。当我运行脚本时,它返回true,并且不会死。帮助!

这是查询:

DELETE FROM selections WHERE selectorID = '$userID' AND selectedID = '$ID'

这是php脚本:

<?php

include_once "connect_to_mysql.php";

$errorB = false;

$error = '';
$id = '';
$userID = '';
if (empty($_GET['ID']) || empty($_GET['userID'])) {
$errorB = true;

if (empty($_GET['ID'])) {
    $error = $error.'-No selected ID was sent';

}

if (empty($_GET['userID'])) {
    $error = $error.'-No selector ID was sent';

}


} else {
$id = $_GET['ID'];
$userID = $_GET['userID'];
echo $id.$userID;
$sqlInsert = mysql_query("DELETE FROM selections WHERE selectorID = '$userID' AND selectedID = '$ID'") or die (mysql_error());
echo 'shz';
}

if ($errorB) {
echo $error;

}



?>

我的数据库如下所示:

 id|selectorID|selectedID
 3  1          4
 4  1          5

1 个答案:

答案 0 :(得分:2)

你的第一个,但较小的麻烦是case sensitive nature of PHP variables

下面

$sqlInsert = mysql_query("DELETE FROM selections WHERE selectorID = '$userID' AND selectedID = '$ID'") or die (mysql_error());

您引用的是$ID,但只有$id

$id = $_GET['ID'];

正确:

$sqlInsert = mysql_query("DELETE FROM selections WHERE selectorID = '$userID' AND selectedID = '$id'") or die (mysql_error());

重要提示

虽然这很可能会奏效,但这很容易SQL injection!请改用PDO,或者至少使用escape您的值!!!