2014-03-07 31 views
0

首先,每個人都很開心,如果這個問題可能很簡單,但是我在php方面的知識很少,那麼對不起我。SQL和PHP語法錯誤,我找不到

我得到的頁面這樣的錯誤:

* 您的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以在第一行使用'WHERE compagni_id = NULL'附近的正確語法*

這裏是我的代碼,它相當長,所以我只把起始部分放在我認爲錯誤可能是。我知道這肯定是一個非常愚蠢的錯誤,比如缺少一個逗號或類似的東西,但是即使使用一些PHP代碼檢查器網站,我也找不到任何東西。

<?php 
if (!function_exists("GetSQLValueString")) { 
function GetSQLValueString($theValue, $theType, $theDefinedValue = "",  $theNotDefinedValue = "") 
{ 
    if (PHP_VERSION < 6) { 
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
    } 

    $theValue = function_exists("mysql_real_escape_string") ?  mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

switch ($theType) { 
case "text": 
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
    break;  
case "long": 
case "int": 
    $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
    break; 
case "double": 
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
    break; 
case "date": 
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
    break; 
case "defined": 
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
    break; 
    } 
    return $theValue; 
} 
} 

$editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form_compagni")) { 
    $updateSQL = sprintf("UPDATE compagni SET av_compagni_numero=%s, av_compagni_tipo01=%s, av_livello_01=%s, av_nome01=%s, av_compagni_tipo02=%s, av_livello_02=%s, av_nome02=%s, anim_taglia01=%s, anim_tipo01=%s, anim_iniz01=%s, anim_velo01=%s, anim_pf01=%s, anim_ca01=%s, anim_ca_sprov01=%s, anim_ca_cont01=%s, anim_atta01=%s, anim_lotta01=%s, anim_spazi01=%s, anim_porta01=%s, anim_atsp01=%s, anim_quali01=%s, anim_temp01=%s, anim_rifl01=%s, anim_volo01=%s, anim_forz01=%s, anim_dest01=%s, anim_cost01=%s, anim_inte01=%s, anim_sagg01=%s, anim_cari01=%s, anim_abil01=%s, anim_tale01=%s, anim_coma01=%s, anim_ogge01=%s, cava_taglia01=%s, cava_tipo01=%s, cava_iniz01=%s, cava_velo01=%s, cava_pf01=%s, cava_ca01=%s, cava_ca_sprov01=%s, cava_ca_cont01=%s, cava_atta01=%s, cava_lotta01=%s, cava_spazi01=%s, cava_porta01=%s, cava_atsp01=%s, cava_quali01=%s, cava_temp01=%s, cava_rifl01=%s, cava_volo01=%s, cava_forz01=%s, cava_dest01=%s, cava_cost01=%s, cava_inte01=%s, cava_sagg01=%s, cava_cari01=%s, cava_abil01=%s, cava_tale01=%s, cava_ogge01=%s, comd_taglia01=%s, comd_tipo01=%s, comd_iniz01=%s, comd_velo01=%s, comd_pf01=%s, comd_ca01=%s, comd_ca_sprov01=%s, comd_ca_cont01=%s, comd_atta01=%s, comd_lotta01=%s, comd_spazi01=%s, comd_porta01=%s, comd_atsp01=%s, comd_quali01=%s, comd_temp01=%s, comd_rifl01=%s, comd_volo01=%s, comd_forz01=%s, comd_dest01=%s, comd_cost01=%s, comd_inte01=%s, comd_sagg01=%s, comd_cari01=%s, comd_abil01=%s, comd_tale01=%s, comd_coma01=%s, comd_ogge01=%s, comr_taglia01=%s, comr_tipo01=%s, comr_iniz01=%s, comr_velo01=%s, comr_pf01=%s, comr_ca01=%s, comr_ca_sprov01=%s, comr_ca_cont01=%s, comr_atta01=%s, comr_lotta01=%s, comr_spazi01=%s, comr_porta01=%s, comr_atsp01=%s, comr_quali01=%s, comr_temp01=%s, comr_rifl01=%s, comr_volo01=%s, comr_forz01=%s, comr_dest01=%s, comr_cost01=%s, comr_inte01=%s, comr_sagg01=%s, comr_cari01=%s, comr_abil01=%s, comr_tale01=%s, comr_coma01=%s, comr_ogge01=%s, fami_taglia01=%s, fami_tipo01=%s, fami_iniz01=%s, fami_velo01=%s, fami_pf01=%s, fami_ca01=%s, fami_ca_sprov01=%s, fami_ca_cont01=%s, fami_atta01=%s, fami_bab01=%s, fami_lotta01=%s, fami_spazi01=%s, fami_porta01=%s, fami_atsp01=%s, fami_quali01=%s, fami_temp01=%s, fami_rifl01=%s, fami_volo01=%s, fami_forz01=%s, fami_dest01=%s, fami_cost01=%s, fami_inte01=%s, fami_sagg01=%s, fami_cari01=%s, fami_abil01=%s, fami_tale01=%s, fami_ogge01=%s, greg_classe01=%s, greg_liv01=%s, greg_razza01=%s, greg_taglia01=%s, greg_iniz01=%s, greg_velo01=%s, greg_pf01=%s, greg_ca01=%s, greg_ca_sprov01=%s, greg_ca_cont01=%s, greg_atta01=%s, greg_bab01=%s, greg_lotta01=%s, greg_spazi01=%s, greg_porta01=%s, greg_atsp01=%s, greg_inca01=%s, greg_inca_classe01=%s, greg_inca_classe01_descr=%s, greg_quali01=%s, greg_temp01=%s, greg_rifl01=%s, greg_volo01=%s, greg_forz01=%s, greg_dest01=%s, greg_cost01=%s, greg_inte01=%s, greg_sagg01=%s, greg_cari01=%s, greg_abil01=%s, greg_tale01=%s, greg_ogge01=%s, greg_descr01=%s, anim_taglia02=%s, anim_tipo02=%s, anim_iniz02=%s, anim_velo02=%s, anim_pf02=%s, anim_ca02=%s, anim_ca_sprov02=%s, anim_ca_cont02=%s, anim_atta02=%s, anim_lotta02=%s, anim_spazi02=%s, anim_porta02=%s, anim_atsp02=%s, anim_quali02=%s, anim_temp02=%s, anim_rifl02=%s, anim_volo02=%s, anim_forz02=%s, anim_dest02=%s, anim_cost02=%s, anim_inte02=%s, anim_sagg02=%s, anim_cari02=%s, anim_abil02=%s, anim_tale02=%s, anim_coma02=%s, anim_ogge02=%s, cava_taglia02=%s, cava_tipo02=%s, cava_iniz02=%s, cava_velo02=%s, cava_pf02=%s, cava_ca02=%s, cava_ca_sprov02=%s, cava_ca_cont02=%s, cava_atta02=%s, cava_lotta02=%s, cava_spazi02=%s, cava_porta02=%s, cava_atsp02=%s, cava_quali02=%s, cava_temp02=%s, cava_rifl02=%s, cava_volo02=%s, cava_forz02=%s, cava_dest02=%s, cava_cost02=%s, cava_inte02=%s, cava_sagg02=%s, cava_cari02=%s, cava_abil02=%s, cava_tale02=%s, cava_ogge02=%s, comd_taglia02=%s, comd_tipo02=%s, comd_iniz02=%s, comd_velo02=%s, comd_pf02=%s, comd_ca02=%s, comd_ca_sprov02=%s, comd_ca_cont02=%s, comd_atta02=%s, comd_lotta02=%s, comd_spazi02=%s, comd_porta02=%s, comd_atsp02=%s, comd_quali02=%s, comd_temp02=%s, comd_rifl02=%s, comd_volo02=%s, comd_forz02=%s, comd_dest02=%s, comd_cost02=%s, comd_inte02=%s, comd_sagg02=%s, comd_cari02=%s, comd_abil02=%s, comd_tale02=%s, comd_coma02=%s, comd_ogge02=%s, comr_taglia02=%s, comr_tipo02=%s, comr_iniz02=%s, comr_velo02=%s, comr_pf02=%s, comr_ca02=%s, comr_ca_sprov02=%s, comr_ca_cont02=%s, comr_atta02=%s, comr_lotta02=%s, comr_spazi02=%s, comr_porta02=%s, comr_atsp02=%s, comr_quali02=%s, comr_temp02=%s, comr_rifl02=%s, comr_volo02=%s, comr_forz02=%s, comr_dest02=%s, comr_cost02=%s, comr_inte02=%s, comr_sagg02=%s, comr_cari02=%s, comr_abil02=%s, comr_tale02=%s, comr_coma02=%s, comr_ogge02=%s, fami_taglia02=%s, fami_tipo02=%s, fami_iniz02=%s, fami_velo02=%s, fami_pf02=%s, fami_ca02=%s, fami_ca_sprov02=%s, fami_ca_cont02=%s, fami_atta02=%s, fami_bab02=%s, fami_lotta02=%s, fami_spazi02=%s, fami_porta02=%s, fami_atsp02=%s, fami_quali02=%s, fami_temp02=%s, fami_rifl02=%s, fami_volo02=%s, fami_forz02=%s, fami_dest02=%s, fami_cost02=%s, fami_inte02=%s, fami_sagg02=%s, fami_cari02=%s, fami_abil02=%s, fami_tale02=%s, fami_ogge02=%s, greg_classe02=%s, greg_liv02=%s, greg_razza02=%s, greg_taglia02=%s, greg_iniz02=%s, greg_velo02=%s, greg_pf02=%s, greg_ca02=%s, greg_ca_sprov02=%s, greg_ca_cont02=%s, greg_atta02=%s, greg_bab02=%s, greg_lotta02=%s, greg_spazi02=%s, greg_porta02=%s, greg_atsp02=%s, greg_inca02=%s, greg_inca_classe02=%s, greg_inca_classe02_descr=%s, greg_quali02=%s, greg_temp02=%s, greg_rifl02=%s, greg_volo02=%s, greg_forz02=%s, greg_dest02=%s, greg_cost02=%s, greg_inte02=%s, greg_sagg02=%s, greg_cari02=%s, greg_abil02=%s, greg_tale02=%s, greg_ogge02=%s, greg_descr02 WHERE compagni_id=%s", 
        GetSQLValueString($_POST['av_compagni_numero'], "text"), 
        GetSQLValueString($_POST['av_compagni_tipo01'], "text"), 

[...這裏繼續與所有相關信息發佈..]

GetSQLValueString($_POST['greg_ogge02'], "text"), 
        GetSQLValueString($_POST['greg_descr02'], "text"), 
        GetSQLValueString($_POST['compagni_id'], "int")); 

    mysql_select_db($database_Database, $Database); 
    $Result1 = mysql_query($updateSQL, $Database) or die(mysql_error()); 

    $updateGoTo = "compagni.php?" . $row_compagni['compagni_id'] . "=" .  $row_compagni['compagni_id'] . ""; 
    if (isset($_SERVER['QUERY_STRING'])) { 
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; 
$updateGoTo .= $_SERVER['QUERY_STRING']; 
} 
header(sprintf("Location: %s", $updateGoTo)); 
} 

$colname_compagni = "1"; 
if (isset($_GET['compagni_id'])) { 
$colname_compagni = $_GET['compagni_id']; 
} 
mysql_select_db($database_Database, $Database); 
$query_compagni = sprintf("SELECT * FROM compagni WHERE compagni_id = %s",  GetSQLValueString($colname_compagni, "int")); 
$compagni = mysql_query($query_compagni, $Database) or die(mysql_error()); 
$row_compagni = mysql_fetch_assoc($compagni); 
$totalRows_compagni = mysql_num_rows($compagni); 

$colname_compagni = "1"; 
if (isset($_GET['compagni_id'])) { 
    $colname_compagni = $_GET['compagni_id']; 
} 
mysql_select_db($database_Database, $Database); 
$query_compagni = sprintf("SELECT * FROM compagni WHERE compagni_id = %s",  GetSQLValueString($colname_compagni, "int")); 
$compagni = mysql_query($query_compagni, $Database) or die(mysql_error()); 
$row_compagni = mysql_fetch_assoc($compagni); 
?> 

我知道代碼是有點混亂,但我用Dreamweaver(不幸的)工作,他正在編寫php代碼。

錯誤在哪裏?

謝謝您的耐心閱讀所有代碼的建議!

+2

打印原始SQL查詢並使用phpMyAdmin或您的MySQL命令提示符手動執行它。它工作嗎? –

+1

嘗試echo $ query_compagni;在sprintf之後查看產生的查詢。 – keepwalking

+0

mysql_query已被棄用使用[mysqli](http://jp.php.net/manual/en/book.mysqli.php) – MamaWalter

回答

0

您沒有設置greg_descr02的值。更改

greg_descr02 WHERE compagni_id=%s 

greg_descr02=%s WHERE compagni_id=%s 
+1

就是這樣!非常感謝!這確實是一個非常愚蠢的錯誤。 –

1

追查MySQL錯誤的最好方法是通過查看實際的錯誤。在這種情況下:

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 'WHERE compagni_id=NULL' at line 1

所以我們看到,我們有近WHERE ...語法錯誤,所以我們會看看,看看有什麼直接不久的字符串之前。如果這對於具有多個聯合或聯接的長查詢不明確,請嘗試添加換行符並再次運行查詢以獲取行號。

因此,錯誤發生之前的部分查詢是greg_descr02

接下來要做的事情是look for what the syntax should be以及您實際擁有的東西。

UPDATE [LOW_PRIORITY] [IGNORE] table_reference 
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... 
[WHERE where_condition] 
[ORDER BY ...] 
[LIMIT row_count] 

哦,我們看到的是WHERE前右要來的唯一的事情是列名後跟一個等號,然後或者一個表達式或默認。你錯過了等號和價值。

我希望這可以幫助你在未來。

給一個男人一條魚,他吃了一天,教一個男人去釣魚,他會在他的餘生中吃東西。