【发布时间】:2017-05-08 18:13:57
【问题描述】:
我的 php 脚本没有将我的数组的内容插入 MySql 数据库。这是一个代码 sn-p。
<?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 是通过 ajax 从 javascript 发送的。现在它只在title 存在于数据库中时插入一条记录。例如,如果我尝试插入标题为“哈利波特”的记录,而数据库中没有标题为“哈利波特”的记录,则不会插入。
我试过使用unset($datx);,但没有成功。 title 字段是 MySQL 中的文本类型。请帮忙,谢谢。
【问题讨论】:
-
or die(mysql_error())在查询文本中。你明白你的代码里写了什么吗? -
尝试在查询方法后打印
mysqli_error() -
or die(mysql_error());不应该是 sql 的一部分。应该是$sql = "insert into ... '$price')"; $result = mysql_query($con, $sql) or die(mysql_error())。注意:您的代码容易受到 sql 注入攻击。 -
检查 print_r($datx) 并检查结果你得到了什么?
-
我删除了
or die(mysql_error()),但没有任何变化。