I get a JSON string from a page and then convert it to a JSON array and then insert it into the database, but all that is inserted is NULL, and I'm sure I can get to the JSON string.
Include_once (' inc/conn.php ');
$json _string=$_request["Visitor"];
$addressInfo =json_decode ($json _string,true);
$name = $addressInfo [' name '];
$idcard = $addressInfo [' IDCRD '];
$visitedphone = $addressInfo [' Visitedphone '];
$address = $addressInfo [' Address '];
$visitedorganization = $addressInfo [' visitedorganization '];
echo "1$visitedphone";
echo "2$address";
echo "3$visitedorganization";
$sql = "INSERT into Adress_list (name,idcrad,tel,adress,visitorganization) VALUES (' $name ', ' $idcard ', ' $visitedphone ', ' $address ', ' $visitedorganization '); ";
$rest = 0;
$rest = mysql_query ($sql);
Echo Mysql_error ();
if ($rest) {
echo "Congratulations, upload success!";
}else{
echo "Upload failed, please re-upload!";
}
Close the database
Mysql_close ($con);
?>
This is my code to seek the Great God Guide, urgent beg ...
Reply to discussion (solution)
Print_r ($addressInfo);
Take a look
Print_r ($addressInfo); What's the result?
$addressInfo =json_decode ($json _string,true);
If it is empty, estimate that the value of $json_string is not in JSON format, please check here.
It's probably a quotation mark problem. Need to be escaped, otherwise SQL is error-guaranteed.
$sql = "INSERT into Adress_list (name,idcrad,tel,adress,visitorganization) VALUES ('". Mysql_real_escape_string ($name) . "', '". Mysql_real_escape_string ($idcard). "', '". Mysql_real_escape_string ($visitedphone). "', '". Mysql_real_escape _string ($address). "', '". Mysql_real_escape_string ($visitedorganization). "');";
$addressInfo;//This value should be judged if it is empty and then perform the following logical comparison well.
The value of $addressinfo when debugging;
How to get the value of Name,idcard and so on, I use $name = $addressInfo [0][' name '];
$name = $addressInfo [' Visitors '][0][' name ']; That's fine.
The array is like this
$addressInfo = Array (' visitors ' = = = Array ( ' visitedphone ' = ' 13333333333 ', ' Organization ' + ' Zhejiang University ', ' phone ' = ' 13888888888 ', ' address ' = ' 38th, Zhejiang Zhe Road ', ' name ' = ' Zhang San ', ' visitedname ' = ' li total ', ' idcard ' and ' 510181198209108435 ', ' visitedorganization ' and ' Hangzhou Network Technology Co., Ltd. ' ), ));
You take a number like this: $name = $addressInfo [' name '];
Obviously, it's wrong.
One less layer of foreach, and you'll see.
It should be written.
$addressInfo =json_decode ($json _string,true);
$addressInfo =[' visitors '][0];
It's your original code.
is not the object that you obtained is objects, not arrays array
$name = $addressInfo [' Visitors '][0][' name ']; That's fine.
Thank you, you can get it like this.