2017-05-08 85 views
0

我的php腳本沒有將我的數組的內容插入MySql數據庫。這裏有一個代碼片段。PHP不插入數組到MySQL數據庫

<?php 

session_start(); 

$host="localhost"; 
$user="root"; 
$password=""; 
$database="burudani db"; 
$con=mysqli_connect($host,$user,$password,$database); 

if(!$con or !$database){ 
     echo'Connection to MySQL failed!'; 
     echo json_encode(0); 
    }else{ 


     $datx=$_POST['data']; 
     if(isset($_POST['data'])){ 

     $title=$datx[0]; 
     $year=$datx[1]; 
     $format=$datx[3]; 
     $type=$datx[5]; 
     $genre=$datx[6]; 
     $desc=$datx[7]; 
     $actors=$datx[11]; 
     $imi=$datx[8]; 
     $imr=$datx[9]; 
     $pos=$datx[10]; 
     $comments=$datx[2]; 
     $price=$datx[4]; 

     $sql="insert into `movies` values(NULL,'$title','$year','$format','$type','$genre','$desc','$actors','$imi','$imr','$pos','$comments','$price') or die(mysql_error());"; 
     $result=mysqli_query($con,$sql); 

     if(!$result){ 
      echo json_encode(1); 
     } 
     else{ 
      echo json_encode(2); 
     } 
     } 
     else if(!isset($_POST['dat'])){ 
      echo json_encode(3); 
     } 
} 

mysqli_close($con); 
?> 

數組$ datx通過javascript中的ajax發送。現在只有在數據庫中存在title時纔會插入記錄。例如,如果我嘗試插入標題爲「哈利波特」的記錄,並且數據庫中沒有帶有標題「哈利波特」的記錄,則不會插入。 我試過使用unset($datx);但沒有成功。 title字段是MySQL中的文本類型。請幫忙,謝謝。

+1

'或die(mysql_error())'在查詢文本中。你知道你的代碼中寫了什麼嗎? –

+0

嘗試在查詢方法 –

+0

或die(mysql_error())之後打印'mysqli_error()';'不應該是sql的一部分。應該是'$ sql =「插入...'$ price')」; $ result = mysql_query($ con,$ sql)或死(mysql_error())'。注意:您的代碼容易受到SQL注入攻擊。 – iblamefish

回答

0

SQL中存在錯誤。 or die(mysql_error())不屬於那裏。

我懷疑你的意思寫:

$sql="insert into `movies` values(NULL,'$title','$year','$format','$type','$genre','$desc','$actors','$imi','$imr','$pos','$comments','$price')"; 
$result=mysqli_query($con,$sql) or die(mysqli_error()); 

但是請注意,你的腳本是容易受到SQL注入式攻擊

prepared statements閱讀起來。您的代碼將變成如下所示:

// create a statement with placeholders for variables 
$statement = mysqli_prepare($con, "insert into `movies` values(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 

// bind the variables to the placeholders 
// note: I do not know what datatypes you're expecting, so have assumed strings. 
// modify the 'ssssssssssss' as required 
mysqli_stmt_bind_param($statement, 'ssssssssssss', $title, $year, $format, $type, $genre, $desc, $actors, $imi, $imr, $pos, $comments, $price); 

// execute the statement or show error (on production environment 
// consider logging instead of displaying errors 
mysqli_stmt_execute($statement) or die(mysqli_error()); 

// clean up the statement 
mysqli_stmt_close($statement); 
+0

''ssssssssssss'''代表什麼? – mwaniki

+0

你的每個參數的數據類型http://php.net/manual/en/mysqli-stmt.bind-param.php#refsect1-mysqli-stmt.bind-param-parameters – iblamefish

+0

它的工作方式很神奇。感謝您的幫助。 – mwaniki

0

不要在mysqlmysqli之間劃分。

你也混淆了查詢和MySQL錯誤函數

$sql="insert into `movies` values(NULL,'$title','$year','$format','$type','$genre','$desc','$actors','$imi','$imr','$pos','$comments','$price')"; 

//回聲$ SQL;死;嘗試在執行之前進行打印和調試

$result=mysqli_query($con,$sql) or die(mysqli_error($con)); 
+0

我已經嘗試使用'my_sql'和'my_sqli',但沒有改變。 – mwaniki

+0

我將如何去'/ echo $ sql;死;'因爲這個腳本在客戶端被調用,並且只使用'echo json_encode'返回一個響應。 – mwaniki