I am receiving a request from a w2ui table to insert data into my database from a form. I am able to successfully submit the data but it submits 3 other entry's every time I submit a form once.
I tried stripcslashes
and escaping the string but that's not the problems
Here is how the untrimmed request looks:
request=%7B%22cmd%22%3A%22save%22%2C%22recid%22%3A0%2C%22name%22%3A%22foo%22%2C%22record%22%3A%7B%22imei%22%3A%22doe%22%2C%22mobile_no%22%3A%22Doe%22%2C%22iccid%22%3A%22jdoe%40email.com%22%2C%22driver%22%3A%22jdoe%22%2C%22company%22%3A%22jdoe%40email.com%22%2C%22type%22%3A%22jdoe%40email.com%22%2C%22channels%22%3A%22jdoe%40email.com%22%2C%22cameras%22%3A%22jdoe%40email.com%22%7D%7D
Here is how the trimmed request looks:
I filled in the fields to 0
for testing.
{"cmd":"save","recid":0,"name":"foo","record":{"imei":"0","mobile_no":"0","iccid":"0","driver":"0","company":"0","type":"0","channels":"0","cameras":"0"}}
The request had to be trimmed and processed to get the Json format.
Here is my PHP code to submit the request:
<?php
//get request a put into variable
$input = file_get_contents('php://input');
$decoded = urldecode($input);
//trim "request=" off
$Trimmed = trim($decoded, 'request=');
//con vars
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";
//time vars
$date_added1 = date("Y-m-d");
$time_added1 = date("h:i:sa");
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
$array = json_decode($Trimmed, true);
foreach($array as $row)
{
$sql = "INSERT into `adddevices` (imei, mobile_no, iccid, driver, company, type, channels, date_added, time_added, cameras)
VALUES ('".$row["imei"]."', '".$row["mobile_no"]."', '".$row["iccid"]."', '".$row["driver"]."', '".$row["company"]."', '".$row["type"]."', '".$row["channels"]."', '$date_added1', '$time_added1', '".$row["cameras"]."')";
mysqli_query($conn, $sql);
}
echo "Data inserted";
mysqli_close($conn);
?>
I expected it to submit the entry once.
You are running foreach()
on $array
, but it looks to me like what you are interested in only on the "record" part of the json data. Then you could drop the foreach and do
$row = $array['record'];