2011-09-27 60 views
0

我想知道是否有人可以幫助我解決這個煩人的問題。嘗試將一些數據插入到表中。與此同時,我想省略一些領域,而不是在那裏插入某些東西。出於某種原因,我得到Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING錯誤。有什麼我在下面做錯了嗎?插入MySQL表錯誤

您的幫助將不勝感激。

 <?php 

     function sqlEscape($string){  
     return "'".mysql_real_escape_string($string)."'"; 
     } 

       if(  $_GET['location'] == '' 
       || $_GET['name']  == '' 
        || $_GET['school'] == '' 
       || $_GET['reason'] == ''    
        || $_GET['address'] == '' 
       || $_GET['postcode'] == '' 
       || $_GET['email']  == '' 
       || $_GET['telephone'] == '') {  

     exit('You missed a value'); 
    } 
include('theConfig.php'); 
$con = mysql_connect($host, $username, $password) or die(mysql_error()) ; 


    if (!$con){ 
    die('Could not connect: ' . mysql_error()); 
    } mysql_select_db($db, $con); //$description = mysql_real_escape_string($_GET[description]); 


$sql = sprintf('INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country) 
     VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,'','')', 

       sqlEscape($_GET['location']),         
       sqlEscape($_GET['name']),     
       sqlEscape($_GET['school']),       
       sqlEscape($_GET['reason']),     

       sqlEscape($_GET['address']),     
       sqlEscape($_GET['postcode']),     
       sqlEscape($_GET['email']),     
       sqlEscape($_GET['telephone'])); 

     if (!mysql_query($sql,$con)){  
      die('Error: ' . mysql_error()); 
     } 
     header('Location: thankyou.php');  
     mysql_close($con) 

?> 

回答

0
$sql = sprintf('INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country) 
     VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,'','')', 

你有「」,「」)「這是不正確,b/C在這個序列中的第一次報價關閉字符串,因此它實際上是三個字符串togather:」 INSERT。 ..',然後',',然後')'。你必須轉義字符串與反斜槓引號或用雙引號括整個字符串:

(逃逸)

$sql = sprintf('INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country) 
     VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,\'\',\'\')', 

(使用雙引號)

$sql = sprintf("INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country) 
     VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,'','')", 
+0

並且在修復此編譯時錯誤之後,您將得到另一個運行時「參數太少」錯誤。以下答案描述瞭如何修復它。 – kstep

+0

謝謝,這有幫助。現在就開始工作了。感謝所有給我建議和幫助的人。我很感激。 –

0

你在你的sprintf()調用中使用的字符串單引號,但你也可以使用單引號的字符串內爲好。

1

你應該已經建立了鎮,縣價值觀 - 或者用默認值(空字符串和其他人一樣)設置:

$sql = sprintf("INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country) 
     VALUES(%s, %s, %s, %s, %s, %s, %s, %s, '','','','')", ...) 

編輯: 此外 - 用雙引號包圍第一sprintf參數單引號內使用...

+0

謝謝,但你可以在上面看到我對城鎮和縣有空值。不知道是否有什麼我失蹤。 –

0

嘗試改變

function sqlEscape($string){  
    return "'".mysql_real_escape_string($string)."'"; 
    } 

function sqlEscape($string){  
    return mysql_real_escape_string($string); 
} 

或更好,但只是把它放在你的sprintf

$sql = sprintf('INSERT INTO applications (location, name, school, reason, address, postcode, email, telephone, town, county, state, country) 
    VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','','')', 

      mysql_real_escape_string($_GET['location']), 

等等

注意到我改變%s到 '%s' 的