我正在制作一个用于更新多个记录的php。我尝试打印发布的JSON数据,但失败了,并说我遇到了解析错误。怎么解决呢?

抱歉,我是php新手,我认为我编写的代码有很多问题。

updateAccountbook.php

require("../db/MySQLDAO.php");

$config = parse_ini_file('../conn.ini');

$json = file_get_contents('php://input');
$obj = json_decode($json, true);

echo $obj;
print_r($obj);

$dbhost = trim($config["dbhost"]);
$dbuser = trim($config["dbuser"]);
$dbpass = trim($config["dbpass"]);
$dbname = trim($config["dbname"]);

$dao = new MySQLDAO($dbhost, $dbuser, $dbpass, $dbname);

$dao->openConnection();

$foreach($obj as $item) {
   mysql_query("INSERT INTO account_book (book_cid, book_name, book_type, book_category, book_user)
   VALUES ('".$item['book_cid']."','".$item['book_name']."', '".$item['book_type']."', '".$item['book_category']."', '".$item['book_user']."')");

 }

$dao->closeConnection();

echo json_encode($returnValue);


mySQLDAO.php

class MySQLDAO{

var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $conn = null;
var $dbname = null;
var $result = null;

function __construct($dbhost, $dbuser, $dbpassword, $dbname) {
    $this->dbhost = $dbhost;
    $this->dbuser = $dbuser;
    $this->dbpass = $dbpassword;
    $this->dbname = $dbname;
}


public function openConnection() {
    $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
    if (mysqli_connect_errno())
        throw new Exception("Could not establish connection with database");
    $this->conn->set_charset("utf8");
}

public function closeConnection() {
    if ($this->conn != null)
        $this->conn->close();
}

}


JSON格式

{
    "book_cid": "1234",
    "book_name" : "Test",
    "book_type": "Test",
    "book_category": "Test",
    "book_user": "Test"
}


错误信息

php - 在PHP中将多个JSON数据更新到MySQL-LMLPHP

最佳答案

更换

$foreach($obj as $item)




foreach($obj as $item)

关于php - 在PHP中将多个JSON数据更新到MySQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47848116/

10-15 18:15