您好我试图将json数组插入我的MySQL数据库。我将数据从我的iPhone传递到那里,我已将数据转换为json格式,并且我使用不将其插入服务器的URL将数据传递到我的服务器。
这是我的json数据。
[{"名称":" 0""电话":" DSF""城市&#34 ;: " sdfsdf""电子邮件":" DSF"},{"名称":" 13123123"&#34 ;电话":" sdfsdfdsfsd""城市":" sdfsf""电子邮件":" 13123123" }]
这是我的Php代码。
<?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
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="dbname";
$username="username";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database) or die('Could not select database');
?>
请告诉我在上述代码中出错的地方我基本上不是php开发人员我是移动应用程序开发人员所以我使用php作为服务器端脚本请告诉我如何解决这个问题。
答案 0 :(得分:7)
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
我认为你传递了错误的变量。您应该在$json
中传递json_decode
,如上所示。
答案 1 :(得分:4)
没有$data
这样的变量。尝试
$obj = json_decode($json,true);
休息看起来不错。如果错误仍然存在,请启用error_reporting
。
答案 2 :(得分:3)
您缺少JSON源文件。创建一个JSON文件,然后将其分配给var data:
<?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);
}
?>
答案 3 :(得分:0)
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."));
}
}
答案 4 :(得分:0)
很简单,您可以按如下所示插入json数据类型。参考链接:click here for more details
insert into sgv values('c-106', 'admin','owner',false,'[{"test":"test"},
{"test":"test"}]',0,'pasds');
答案 5 :(得分:-3)
$string=mysql_real_escape_string($json);