T2Admin T2Admin - 4 months ago 29
JSON Question

storing values from JSON.stringify to mysql

I am trying to store values from html form and stored/send via JSON.stringify to my php file, and then my PHP file write those values to DB.

So far i've managed to receive the $_POST values but is it in array, like this:


This is my PHP file now:
$array = $_POST["input"];
$i_ment = $array[0];
$odd_ment = $array[1];
$em_ment = $array[2];
$mobi_ment = $array[3];

mysql_query("INSERT INTO team VALUES ('$i_ment', '$odd_ment', '$em_ment', '$mobi_ment')")

But the values that comes to db are still in arrow, like this:



when receiving JSON-data, it is just a string, not an array.

you have to decode it before you can actually use it:

$array = json_decode($_POST["input"], true);

note the true, which tells PHP to create an associative array instead of an object.

also: you shouldn't use mysql-functions, which are deprecated and in PHP7 removed; and your code is vulnerable to SQL injections.

both mysqli_ and PDO provide parameterized queries which protect you from this kind of attack.