Php 如何在mysql数据库中插入json数组
嗨,我正在尝试将json数组插入我的MySQL数据库。我将iphone中的数据传递到那里,我将数据转换成json格式,并使用url将数据传递到我的服务器,而不是插入到我的服务器中 这是我的json数据 [{“姓名”:“0”,“电话”:“dsf”,“城市”:“sdfsdf”,“电子邮件”:“dsf”},{“姓名”:“13123123”,“电话”:“SDFSDFDSD”,“城市”:“sdfsf”,“电子邮件”:“13123123”}] 这是我的Php代码Php 如何在mysql数据库中插入json数组,php,mysql,arrays,json,Php,Mysql,Arrays,Json,嗨,我正在尝试将json数组插入我的MySQL数据库。我将iphone中的数据传递到那里,我将数据转换成json格式,并使用url将数据传递到我的服务器,而不是插入到我的服务器中 这是我的json数据 [{“姓名”:“0”,“电话”:“dsf”,“城市”:“sdfsdf”,“电子邮件”:“dsf”},{“姓名”:“13123123”,“电话”:“SDFSDFDSD”,“城市”:“sdfsf”,“电子邮件”:“13123123”}] 这是我的Php代码 <?php $json = fi
<?php
$json = file_get_contents('php://input');
$obj = json_decode($data,true);
//Database Connection
require_once 'db.php';
/* insert data into DB */
foreach($obj as $item) {
mysql_query("INSERT INTO `database name`.`table name` (name, phone, city, email)
VALUES ('".$item['name']."', '".$item['phone']."', '".$item['city']."', '".$item['email']."')");
}
//database connection close
mysql_close($con);
//}
?>
请告诉我上面的代码哪里做错了基本上我不是php开发人员我是移动应用程序开发人员所以我使用php作为服务器端脚本请告诉我如何解决这个问题。没有
$data
这样的变量。试一试
$obj = json_decode($json,true);
其余的看起来不错。如果错误仍然存在,请启用错误报告
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
我认为您传递了错误的变量。您应该在
json\u decode
中传递$json
,如上所示。您缺少json源文件。创建一个JSON文件,然后将其分配给var数据:
$string=mysql_real_escape_string($json);
<?php
require_once('dbconnect.php');
// reading json file
$json = file_get_contents('userdata.json');
//converting json object to php associative array
$data = json_decode($json, true);
// processing the array of objects
foreach ($data as $user) {
$firstname = $user['firstname'];
$lastname = $user['lastname'];
$gender = $user['firstname'];
$username = $user['username'];
// preparing statement for insert query
$st = mysqli_prepare($connection, 'INSERT INTO users(firstname, lastname, gender, username) VALUES (?, ?, ?, ?)');
// bind variables to insert query params
mysqli_stmt_bind_param($st, 'ssss', $firstname, $lastname, $gender, $username);
// executing insert query
mysqli_stmt_execute($st);
}
?>
很简单,您可以插入json数据类型,如下所示。参考链接:
还向我们展示db.php的内容。您的代码看起来不错,即解析JSON和插入查询,我想它与连接和db select相关。在查询后使用mysql_error()查看发生了什么。我认为您的连接有问题。正如Amit所说,显示您的db.php代码使用错误报告(E_ALL)@amit在我的问题中更新了我的db连接代码,请检查它好的捕获!!甚至我在本地尝试了他的代码,但我在检查时发现它是正确的,所以完全没有检查到,以至于他使用了错误的变量名!!这比其他两个答案提供了什么?你能进一步解释你的代码吗?它如何在数据库中插入任何内容?@NicoHaase请参阅已编辑的代码。数据库连接文件是另一个文件夹中的文件。我认为没有这些附加文件,任何人都无法使用您的代码。我没有要求你提供更多的代码,但要提供一个解释——这就是让其他人能够从你的答案中学习的原因。这个答案中90%-100%的代码与问题无关。任何可能相关的内容似乎都隐藏在此处未显示的其他文件中。也许你没有真正理解问题的要点。你能进一步解释你的代码吗?它做了什么,是什么让你认为它解决了问题?
<?php
require_once('dbconnect.php');
// reading json file
$json = file_get_contents('userdata.json');
//converting json object to php associative array
$data = json_decode($json, true);
// processing the array of objects
foreach ($data as $user) {
$firstname = $user['firstname'];
$lastname = $user['lastname'];
$gender = $user['firstname'];
$username = $user['username'];
// preparing statement for insert query
$st = mysqli_prepare($connection, 'INSERT INTO users(firstname, lastname, gender, username) VALUES (?, ?, ?, ?)');
// bind variables to insert query params
mysqli_stmt_bind_param($st, 'ssss', $firstname, $lastname, $gender, $username);
// executing insert query
mysqli_stmt_execute($st);
}
?>
header("Access-Control-Allow-Origin: http://localhost/rohit/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
//files needed to connect to database
include_once 'config/database.php';
include_once 'objects/main.php';
//get Database connection
$database = new Database();
$db = $database->getConnection();
//instantiate product object
$product = new Product($db);
//get posted data
$data = json_decode(file_get_contents("php://input"));
//set product property values
$product->b_nm = $data->Business_Name;
$product->b_pno = $data->Business_Phone;
$product->b_ads = $data->Business_Address;
$product->b_em = $data->Business_Email;
$product->b_typ = $data->Business_Type;
$product->b_ser = $data->Business_Service;
$name_exists = $product->nameExists();
if($name_exists){
echo json_encode(
array(
"success"=>"0",
"message" => "Duplicate Record Not Exists."
)
);
} else{
if($product->b_nm != null && $product->b_pno != null && $product->b_ads != null && $product->b_em != null && $product->b_typ != null && $product->b_ser != null){
if($product->create()){
//set response code
http_response_code(200);
//display message: Record Inserted
echo json_encode(array("success"=>"1","message"=>"Successfully Inserted"));
}
}
else{
//set response code
http_response_code(400);
//display message: unable to insert record
echo json_encode(array("success"=>"0","message"=>"Blank Record Not Exists."));
}
}
insert into sgv values('c-106', 'admin','owner',false,'[{"test":"test"},
{"test":"test"}]',0,'pasds');