Hallo, ich versuche, das Json-Array in meine MySQL-Datenbank einzufügen. Ich übergebe die Daten von meinem iPhone. Ich habe die Daten in das Json-Format konvertiert, und ich leite die Daten an meinen Server weiter. Dabei wird die URL verwendet, die nicht in meinen Server eingefügt wird.
Dies sind meine Json-Daten.
[{"name": "0", "phone": "dsf", "city": "sdfsdf", "email": "dsf"}, {"name": "13123123", "phone": "sdfsdfdsfsd "," city ":" sdfsf "," email ":" 13123123 "}]
Dies ist mein PHP-Code.
<?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);
//}
?>
Mein Datenbankverbindungscode.
<?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');
?>
Bitte sagen Sie mir, was ich im obigen Code falsch mache. Im Grunde bin ich kein PHP-Entwickler. Ich bin Entwickler von Mobilanwendungen. Ich verwende das PHP als serverseitiges Scripting. Bitte sagen Sie mir, wie ich dieses Problem lösen kann.
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
Ich denke, Sie übergeben die falsche Variable. Sie sollten $json
in json_decode
wie oben gezeigt übergeben.
Es gibt keine solche Variable wie $data
. Versuchen
$obj = json_decode($json,true);
Rest sieht gut aus. Wenn der Fehler weiterhin besteht, aktivieren Sie error_reporting
.
Es fehlt eine JSON-Quelldatei. Erstellen Sie eine JSON-Datei und weisen Sie sie den var-Daten zu:
<?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."));
}
}