2009-05-04 141 views
0

我試圖從這個頁面上的mysql行顯示信息。我正在使用$ _GET,因爲該ID包含在頁面鏈接中:www.example.com/page.php?id=1但它返回此錯誤:mysql語法問題

錯誤:您在您的錯誤SQL語法;檢查對應於您的MySQL服務器版本的手冊,以便在第1行'='1''附近使用正確的語法。

有誰知道如何解決這個問題?下面

代碼:

<?php 

    $username="xxx"; 
    $password="xxx"; 
    $database="xxx"; 
    mysql_connect(localhost,$username,$password); 
    @mysql_select_db($database) or die("Unable to select database"); 


include 'library/config.php'; 
include 'library/opendb.php'; 

if(isset($_GET['id'])) 
{ 
    $query = "SELECT id, title, content, contactname, contactemail, contactnumber ". 
      "FROM vacancies". 
      "WHERE id = '{$_GET['id']}'"; 
    $result = mysql_query($query) or die('Error : ' . mysql_error()); 
    list($id, $title, $content, $contactname, $contactemail, $contactnumber) = mysql_fetch_array($result, MYSQL_NUM); 

    $content = htmlspecialchars($content); 
} 

if(isset($_POST['update'])) 
{ 
    $id = $_POST['id']; 
    $title = $_POST['title']; 
    $content = $_POST['content']; 
    $contactname = $_POST['contactname']; 
    $contactemail = $_POST['contactemail']; 
    $contactnumber = $_POST['contactnumber']; 

    if(!get_magic_quotes_gpc()) 
    { 
     $title = addslashes($title); 
     $content = addslashes($content); 
     $contactname = addslashes($contactname); 
     $contactemail = addslashes($contactemail); 
     $contactnumber = addslashes($contactnumber); 
    } 

    // update the article in the database 
    $query = "UPDATE vacancies 
      SET title = '$title', content = '$content', contactname = '$contactname', contactemail = '$contactemail', contactnumber = '$contactnumber'". 
     "WHERE id = '$id'"; 
    mysql_query($query) or die('Error : ' . mysql_error()); 

    // then remove the cached file 
    $cacheDir = dirname(__FILE__) . '/cache/'; 
    $cacheFile = $cacheDir . '_' . $_GET['id'] . '.html'; 

    @unlink($cacheFile); 

    // and remove the index.html too because the file list 
    // is changed 
    @unlink($cacheDir . 'index.html'); 

    echo "<b>Job Entry: '$title' updated</b>"; 

    // now we will display $title & content 
    // so strip out any slashes 
     $title = stripslashes($title); 
     $content = stripslashes($content); 
     $contactname = stripslashes($contactname); 
     $contactemail = stripslashes($contactemail); 
     $contactnumber = stripslashes($contactnumber); 

} 

include 'library/closedb.php'; 
?> 

回答

0

試試這個:

$query = "SELECT id, title, content, contactname, contactemail, contactnumber ". 
     "FROM vacancies ". 
     "WHERE id = '".$_GET['id']."'"; 

我總是試圖離開這個變量了我的琴絃,只需添加它們與時間,我發現它消除了很多的困惑。

0

一個問題:

$query = "UPDATE vacancies 
      SET title = '$title', content = '$content', contactname = '$contactname', contactemail = '$contactemail', contactnumber = '$contactnumber'". 
     "WHERE id = '$id'"; 

結果在最後一列之間沒有空間的WHERE子句。將其更改爲:

$query = "UPDATE vacancies 
      SET title = '$title', content = '$content', contactname = '$contactname', contactemail = '$contactemail', contactnumber = '$contactnumber' ". 
     "WHERE id = '$id'"; 

或我的首選格式:

$query = <<<END 
UPDATE vacancies 
SET title = '$title', 
    content = '$content', 
    contactname = '$contactname', 
    contactemail = '$contactemail', 
    contactnumber = '$contactnumber' 
WHERE id = '$id' 
END; 

注:您應使用mysql_real_escape_string真正逃脫字段()。

0

在您所有的疑問圍繞

{$_GET['id']} 

$id 

刪除引號。

你的ID爲整數類型我認爲,這不能帶引號的版本,或者嘗試將整數鍵匹配字符串「1」

- 改變這一行

$result = mysql_query($query) or die('Error : ' . mysql_error()); 

$result = mysql_query($query) or die('Error : ' . mysql_error() . "\n\n" . $query); 

然後,你可以看到什麼查詢進入數據庫。然後你可以在這裏發佈給我們看。

還請發表

describe <tablename>; 
+0

嗯,我對這一切都很陌生,我已經這樣做了,也是cletus建議的,但沒有喜悅。 感謝您的全力幫助 還有其他建議嗎? – user96828 2009-05-04 02:44:21

+0

已更新,你可以做我所問的。謝謝 – Louis 2009-05-04 03:54:32