Hi I'm trying to insert the json array into my MySQL database. I'm passing the data form my iphone there i have converted the data into json format and I'm passing the data to my server using the url its not inserting into my server.
嗨,我正在尝试将json数组插入我的MySQL数据库。我正在通过我的iphone传递数据,我将数据转换为json格式,并且我使用url将数据传递到我的服务器,而不是插入我的服务器。
This is my json data.
这是我的json数据。
[{"name":"0","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"13123123","phone":"sdfsdfdsfsd","city":"sdfsf","email":"13123123"}]
This is my Php code.
这是我的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);
//}
?>
My database connection code.
我的数据库连接代码。
<?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');
?>
Please tell where I'm doing wrong in the above code basically I'm not a php developer I'm mobile application developer so I'm using the php as a server side scripting please tell me how to resolve this problem.
请告诉我在上面的代码中哪里做错了基本上我不是php开发人员我是移动应用程序开发人员所以我使用php作为服务器端脚本请告诉我如何解决这个问题。
4 个解决方案
#1
7
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
I think you are passing the wrong variable. You should pass $json
in json_decode
as shown above.
我认为你传递了错误的变量。您应该在json_decode中传递$ json,如上所示。
#2
3
There is no such variable as $data
. Try
没有像$ data这样的变量。尝试
$obj = json_decode($json,true);
Rest looks fine. If the error still persists, enable error_reporting
.
休息看起来很好。如果错误仍然存在,请启用error_reporting。
#3
2
You are missing JSON source file. Create a JSON file then assign it to var data:
您缺少JSON源文件。创建一个JSON文件,然后将其分配给var数据:
<?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);
}
?>
#4
-3
$string=mysql_real_escape_string($json);
#1
7
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
I think you are passing the wrong variable. You should pass $json
in json_decode
as shown above.
我认为你传递了错误的变量。您应该在json_decode中传递$ json,如上所示。
#2
3
There is no such variable as $data
. Try
没有像$ data这样的变量。尝试
$obj = json_decode($json,true);
Rest looks fine. If the error still persists, enable error_reporting
.
休息看起来很好。如果错误仍然存在,请启用error_reporting。
#3
2
You are missing JSON source file. Create a JSON file then assign it to var data:
您缺少JSON源文件。创建一个JSON文件,然后将其分配给var数据:
<?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);
}
?>
#4
-3
$string=mysql_real_escape_string($json);