2013-02-20 108 views
0

基本上如我的問題所述,它適用於phpmyadmin,但不適用於php本身。當我運行腳本時,它會返回true,並且不會死亡。幫幫我!mysql DELETE在phpmyadmin上工作,但不在php腳本上

這裏是查詢:

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 
+0

如果它在phpmyadmin工作,而不是在PHP我的猜測是你的變量不包含你期望他們的值。您還需要將其分解爲最簡單的用例場景,以便於進行故障排除。 – 2013-02-20 15:09:17

+1

你知道你的代碼在sql注入的情況下非常不安全嗎?在傳遞給你的sql查詢之前,你應該驗證從'$ _GET'得到的所有數據。 – oktopus 2013-02-20 15:12:13

回答

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你的價值觀!

+0

好吧。這就是爲什麼您無法開發禁用完全錯誤報告的PHP代碼。 – 2013-02-20 15:14:08

+0

謝謝!我是一個經驗豐富的程序員,我感到非常愚蠢。但我想我仍然會錯過一些事情。至於SQL注入,我總是構建我的腳本並讓它們工作,然後返回並修復所有的漏洞。 – 2013-02-20 15:15:56

+0

@ÁlvaroG.Vicario是的...這樣的問題,他們的回憶讓我皺眉頭,當我不得不面對PHP ... – ppeterka 2013-02-20 15:16:32

相關問題