【问题标题】:PHP not inserting array into MySQL databasePHP没有将数组插入MySQL数据库
【发布时间】: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()),但没有任何变化。

标签: php mysql arrays


【解决方案1】:

您的 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);

【讨论】:

【解决方案2】:

不要在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));

【讨论】:

  • 我尝试过同时使用my_sqlmy_sqli 但没有任何变化。
  • 我将如何处理//echo $sql ; die;,因为此脚本是在客户端调用的,并且仅使用echo json_encode 返回响应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 2014-12-02
  • 2015-09-10
  • 1970-01-01
相关资源
最近更新 更多