2012-01-05 36 views
-4

儘管彈出了一條錯誤消息,但我的php代碼正在成功執行。該代碼刪除一行從我的一個表,但會顯示以下錯誤信息:儘管「SQL語法錯誤」消息成功執行

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1

我的PHP代碼如下:

<?php 
$id = $_GET['meeting_id']; 
$username = $_GET['username']; 

$result = mysql_query("DELETE FROM attendees WHERE meeting_id = '$id' AND username = '$username'") 
or die(mysql_error()); 

if (!mysql_query($result)) 
    { 
     die('Error: ' . mysql_error()); 
    } 
    else 
    { 
     echo '<h2>The User Has Been Removed From The Meeting</h2>'; 

    } 
?> 

任何人都可以在這裏看到的解決方案? 謝謝

+10

你的代碼是SQL注入攻擊敞開的。 – 2012-01-05 20:09:12

+0

看來'meeting_id'是一個整數類型的列,因此'$ 1'沒有引號。第二:這個代碼是_highly_ insecure!驗證_any_輸入!最後:回答查詢,看看結果如何。 – KingCrunch 2012-01-05 20:09:52

+1

http://php.net/manual/en/security.database.sql-injection.php – 2012-01-05 20:10:35

回答

1

您正在結果集上執行兩次查詢。 BTW

$result = mysql_query("DELETE FROM attendees WHERE meeting_id = '$id' AND username = '$username'"); 

if (!$result) 
    { 
     die('Error: ' . mysql_error()); 
    } 
    else 
    { 
     echo '<h2>The User Has Been Removed From The Meeting</h2>'; 

    } 

:試試這個代碼很容易出現SQL注入

+0

謝謝,它現在可行! :) – user1114080 2012-01-05 20:14:05

11

您正在運行mysql_query()兩次;一旦與(正確)查詢,第二次

if (!mysql_query($result)) 

與之前的查詢結果。這將導致錯誤。

你可能想

if (!mysql_fetch_object($result)) 

或類似的東西。

此外,正如評論部分所指出的,您的PHP代碼易受SQL injection的影響,您應該修復此問題。

+1

+1幹得好。 – Paulpro 2012-01-05 20:10:55

+1

您可能還想提及他的代碼中的漏洞並將其鏈接到此處:http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php或告訴他關於PDO – Paulpro 2012-01-05 20:12:10

+0

@user這總是一個好的點。我已經在上面的評論部分做過了,但我會將其添加到答案 – 2012-01-05 20:17:05

1

您對您的查詢調用mysql_query(),然後你再稱之爲上查詢的結果。這是你的問題所在。刪除第二個mysql_query()呼叫。