2017-03-15 33 views
1

上傳php中的表單時出現錯誤消息。插入記錄時,您的SQL語法錯誤消息中有錯誤

「您的SQL語法錯誤;檢查對應於你的MySQL服務器版本使用附近的正確語法手冊」

我已經按照從其他職位說明如下,無濟於事:

1-在反引號中包裝列標題名稱。 2-確保所有字符串均以字符串形式傳遞,並以整數形式傳入。 3-發送前清除任何字符串。 4-確保與數據庫的連接正常工作,我們可以從中進行查詢。 5檢查並重新檢查我的html代碼。

這裏是我的PHP代碼:

<?php 

include('../config/config.php'); 

// Redirect browser if the upload form WAS NOT submited. 
if (!isset($_POST['submit_upload'])) 
{ 
    header("location: upload.html"); 
} 

// Continue if the upload form WAS SUBMITED 

else 
{ 

    // Set the upload directory path 

    $target_path = realpath(dirname(__FILE__)) . "/uploads/audio/"; 

    // Array to store validation errors 

    $error_msg = array(); 

    // Validation error flag, if this becomes true we won't upload 

    $error_flag = false; 
    // We get the data from the upload form 

    $filename = $_FILES['file']['name']; 
    $temp_filename = $_FILES['file']['tmp_name']; 
    $filesize = $_FILES['file']['size']; 
    $mimetype = $_FILES['file']['type']; 

    // Convert all applicable characters to HTML entities 

    $filename = htmlentities($filename); 
    $mimetype = htmlentities($mimetype); 

    // Check for empty file 

    if ($filename == "") 
    { 
     $error_msg[] = 'No file selected!'; 
     $error_flag = true; 
    } 

    // Check the mimetype of the file 

    if ($mimetype != "audio/x-mp3" && $mimetype != "audio/mp3") 
    { 
     $error_msg[] = 'The file you are trying to upload does not contain expected data. 
       Are you sure that the file is an MP3 one?'; 
     $error_flag = true; 
    } 

    // Get the file extension, an honest file should have one 

    $ext = substr(strrchr($filename, '.') , 1); 
    if ($ext != 'mp3') 
    { 
     $error_msg[] = 'The file type or extention you are trying to upload is not allowed!  
       You can only upload MP3 files to the server!'; 
     $error_flag = true; 
    } 

    // Check that the file really is an MP3 file by reading the first few characters of the file 

    $open = @fopen($_FILES['file']['tmp_name'], 'r'); 
    $read = @fread($open, 3); 
    @fclose($open); 
    if ($read != "ID3") 
    { 
     $error_msg[] = "The file you are trying to upload does not seem to be an MP3 file."; 
     $error_flag = true; 
    } 

    // Now we check the filesize. 
    // The file size shouldn't include any other type of character than numbers 

    if (!is_numeric($filesize)) 
    { 
     $error_msg[] = 'Bad filesize!'; 
     $error_flag = true; 
    } 

    // If it is too big or too small then we reject it 
    // MP3 files should be at least 1MB and no more than 10 MB 
    // Check if the file is too large 

    if ($filesize > 10485760) 
    { 
     $error_msg[] = 'The file you are trying to upload is too large!  
      Please upload a smaller MP3 file'; 
     $error_flag = true; 
    } 

    // Check if the file is too small 

    if ($filesize < 1048600) 
    { 
     $error_msg[] = 'The file you are trying to upload is too small! 
      It is too small to be a valid MP3 file.'; 
     $error_flag = true; 
    } 

    // Function to sanitize values received from the form. Prevents SQL injection 

    function clean($conn, $str) 
    { 
     $str = @trim($str); 
     if (get_magic_quotes_gpc()) 
     { 
      $str = stripslashes($str); 
     } 

     return mysqli_real_escape_string($conn, $str); 
    } 

    // Sanitize the POST values 

    $title = clean($conn, $_POST['title']); 
    $context = clean($conn, $_POST['context']); 
    $source = clean($conn, $_POST['source']); 
    $interviewer = clean($conn, $_POST['interviewer']); 
    $interviewee = clean($conn, $_POST['interviewee']); 
    $intervieweeAge = (int)$_POST['intervieweeAge']; 
    $geoRegion = clean($conn, $_POST['geoRegion']); 
    $language = clean($conn, $_POST['language']); 
    $recDate = clean($conn,$_POST['recDate']); 
    $keywords = $_POST['keywords']; 

    if ($title == '') 
    { 
     $error_msg[] = 'Title is missing'; 
     $error_flag = true; 
    } 

    if ($interviewee == '') 
    { 
     $error_msg[] = 'Interviewee name/anonymous is missing'; 
     $error_flag = true; 
    } 

// If there are input validations, show errors 

if ($error_flag == true) 
{ 
    foreach($error_msg as $c => $p) echo "Error " . $c . ": " . $p . "<br />"; 
} 
// Else, all checks are done, move the file. 
else 
{ 
    if (is_uploaded_file($temp_filename)) 
    { 
     // Generate an uniqid 
     $uniqfilename = $interviewee . '_' . str_replace("_", "", $recDate) . '.mp3'; 
     $filePath = '/uploads/audio/' . $uniqfilename; 

     // If the file was moved, change the filename 

     if (move_uploaded_file($temp_filename, $target_path . $uniqfilename)) 
     { 

      // Again check that the file exists in the target path 
      if (@file_exists($target_path . $uniqfilename)) 
      { 

       // Assign upload date to a variable 

       $upload_date = date("Y-m-d"); 

       // Create INSERT query 

       $qry = "INSERT INTO FDM177_AUDIO_CLIPS (title,context,source,interviewer,interviewee,intervieweeAge,geoRegion,language,recDate,fileName,filePath) 
       VALUES('$title','$context','$source','$interviewer',$interviewee',$intervieweeAge,'$geoRegion','$language','$recDate','$uniqfilename','$filePath')"; 

       $result = mysqli_query($conn, $qry) or die(mysqli_error($conn)); 

       if ($result) 
       { 
        $id = mysqli_insert_id($conn); 
        echo "File uploaded. Now it is called :" . $uniqfilename . "<br />" . $date . "<br />"; 

       } 
       else 
       { 
        echo "There was an error uploading the file, please try again!"; 
       } 

       if(1) { 
        //if (is_array($keywords) || is_object($keywords)) { 
        foreach($keywords as $k) { 
          // $idQuery = "SELECT keyword_ID from KEYWORDS WHERE keywordName=" . $k"; 
          $idQuery = mysqli_query($conn, "SELECT * FROM FDM177_KEYWORDS WHERE (`keywordName` LIKE '%".$k."%')") or die(mysql_error()); 

          $matchingKArray = mysqli_fetch_array($idQuery); 

          $keyword_FK = $matchingKArray[keyword_ID]; 

          // echo $kQuery; 
          echo $keyword_FK; 

          $qry = "INSERT INTO FDM177_JNCT_KWDS_CLIPS (keyword_FK, clip_FK) 
          VALUES ('$keyword_FK', '$id')"; 
          $result = mysqli_query($conn, $qry); 
          if ($result) 
          { 
           echo 'inserted with keyword.' . $k . ' <br />'; 

          } 
         } 
        } 
        else { 
         echo "keywords are missing"; 
        } 




       } 
      } 
      else { 
       echo "There was an error uploading the file, please try again!"; 
      } 

     } 
     else 
     { 
      echo "There was an error uploading the file, please try again!"; 
     } 
    } 
} 

?> 

在那開始爲MySQL查詢INSERT INTO FDM177_AUDIO_CLIPS第一MySQL查詢時出現問題...

我缺少什麼?

謝謝!

+0

您是否嘗試過重新命名錶?從而刪除下劃線 – Swellar

+0

你想要插入的值中有任何撇號嗎?您可能需要使用mysql_real_escape_string將其轉義。最好的做法是使用準備好的語句,但:http://php.net/manual/en/mysqli.quickstart.prepared-statements.php – tjfo

+0

請你可以複製和粘貼錯誤*全*。 「在正確的語法附近使用」之後,文本會過早切斷。 –

回答

2

報價在一個查詢打破'$interviewer',$interviewee',

$qry = "INSERT INTO FDM177_AUDIO_CLIPS 
       (title, context, source,interviewer, interviewee, 
       intervieweeAge,geoRegion,language,recDate,fileName,filePath) 
       VALUES 
       ('$title', '$context', '$source', '$interviewer', '$interviewee', 
       $intervieweeAge,'$geoRegion','$language','$recDate','$uniqfilename','$filePath')"; 
+0

您的查詢將無法正常工作,因爲它沒有以正確的格式寫入。 –

+0

謝謝!這完全是爲我做的。永遠不會抓住它。 – user2030942

+0

高興地幫助:) –

相關問題