2017-05-08 87 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