2015-05-22 52 views
0

我一直在試圖插入使用此代碼的數據到數據庫中,但一個錯誤出來是這樣的:錯誤的SQL語法與「隨機存取存儲器」

您的SQL錯誤句法;檢查 對應於你的MySQL服務器版本使用 附近「隨機存取存儲器」 -RAM正確的語法手冊 - 「‘DDR1內存’)」在1號線

<?php 
    $dbhost = 'localhost'; 
    $dbuser = ''; 
    $dbpass = ''; 
    $dbname = 'ticketstatus'; 

    $conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); 
    mysqli_select_db($conn,$dbname); 

    $fname ="report.csv"; 
    $chk_ext = explode(".",$fname); 

    if(strtolower($chk_ext[1]) == "csv") 
    { 
     $filename = "report.csv"; 
     $handle = fopen($filename, "r"); 

     while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) 
     { 
      //print_r($data); 

      $sql1 = "INSERT into ticket(ticketID,ticketDate,ticketResDate,ticketStatus) values('$data[0]','$data[1]','$data[15]','$data[13]')"; 
      mysqli_query($conn,$sql1) or die(mysqli_error($conn)); 

      $sql2 = "INSERT into enduser(ticketID,endUserID,endUserName,endUserPhone,endUserLoc,endUserState) values('$data[0]','@dummy','$data[2]','$data[3]','$data[4]','$data[5]')"; 
      mysqli_query($conn,$sql2) or die(mysqli_error($conn)); 

      $sql3 = "INSERT into defect(ticketID,defectID,defectCategory,defectBrand,defectModel,defectSerialNo,defectType,defectDesc,defectCauses,defectReplace) values('$data[0]','@dummy','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[14]')"; 
      mysqli_query($conn,$sql3) or die(mysqli_error($conn)); 

      $sql4 = "INSERT into technician(ticketID,techID,techName,techRemarks) values('$data[0]','@dummy','$data[16]','$data[17]')"; 
      mysqli_query($conn,$sql4) or die(mysqli_error($conn)); 

      $sql5 = "INSERT into status(ticketID,statusID,statusAz,statusAF) values('$data[0]','@dummy','$data[18]','$data[19]')"; 
      mysqli_query($conn,$sql5) or die(mysqli_error($conn)); 

     } 
     fclose($handle); 

     echo "Successfully Imported"; 

    } 
    else 
    { 
     echo "Invalid File"; 
    } 

    ?> 

哪一部分做我做錯了?

+0

「RANDOM ACCESS MEMORY」-RAM-'中有單引號,因爲它會干擾字符串周圍的單引號,所以需要進行轉義。 [這篇文章](http://stackoverflow.com/questions/9596652/how-to-escape-apostrophe-in-mysql)應該會幫助你 – Val

+0

列出完全失敗的SQL查詢,請 –

+0

@Val,謝謝,我嘗試一下:) – emi

回答

1

將參數插入查詢字符串時,必須確保插入數據中的任何字符都已正確編碼,以便它們不會干擾對查詢語言具有特殊含義的字符,例如單個用來表示字符串的引號。

通常,您可以通過傳遞所有字符串參數通過mysqli_real_escape_string()功能做到這一點:

while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) 
{ 
    $safe = array(); 
    foreach ($data as $d) { 
     $safe[] = mysqli_real_escape_string ($conn, $d); 
    } 

    $sql1 = "INSERT into ticket(ticketID,ticketDate,ticketResDate,ticketStatus) values('$safe[0]','$safe[1]','$safe[15]','$safe[13]')"; 

    mysqli_query($conn,$sql1) or die(mysqli_error($conn)); 

    /* And so on */ 

另一種方式來解決這個問題是使用prepared statements


注意,有一些安全問題mysqli_real_escape_string(),但主要是在模糊的情況。您可以在以下位置閱讀更多信息:SQL injection that gets around mysql_real_escape_string()