I have an associative php array that is created using the JSON decode function.
I am attempting to then insert this array row by row into my database.
The values are inserting but I am getting repeats of the last row.
PHP
<?php
include "config.php";
$gambatch = $_POST["mydata"];
$myjson = json_decode($gambatch,true);
foreach ($myjson as $row){
//get the tweet details
$name = $row[0];
$points = $row[1];
}
$sql1 = "INSERT INTO testjson(named,points)
VALUES ('".$name."','".$points."')";
if(!mysqli_query($con, $sql1))
{
die('Error : ' . mysql_error());
}
if (!mysqli_query($con,$sql1)) {
die('Error: ' . mysqli_error($con));
}
echo '<br><br><br><br><br><br>';
echo "data added";
The Json is from another page when dumped from php after json decode looks like this :
array(2) { [0]=> array(7) { [0]=> string(6) "dddddd" [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) [6]=> int(0) } [1]=> array(7) { [0]=> string(7) "fffffff" [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) [5]=> int(0) [6]=> int(0) } }
This is the full array but I am only trying to insert the first two values in each row) in the code above (eventually I want to insert all but I am kept it smaller while getting it to work).
This is also an array with just two entries it will eventually be dynamic and accept many more so the code needs to be flexible enough. I think I am nearly there?
I am getting no PHP errors the problem is the duplicate entries. It either replacing itself or I have a piece of looping code that I have not spotted.
Many thanks in advance for any help
Finally I will be using prepared statements - using this dangerous sql because it is easy to mock up