0

Hi I have a jqgrid setup with a custom action button that sends selected rows to a database. I have everything working except that it posts the data 7 times per each row select where I only want it posted once per selected row. Please any and all help appreciated. JQGrid, How to post JSON string to PHP to Process and send to Database?

$decarr= array(
['id'] =>
518
['name'] =>
'Brochure for amada'
['id_continent'] =>
' Ramon'
['lastvisit'] =>
'5/15/2013'
['cdate'] =>
'5/29/2013'
['ddate'] =>
'5/31/2013'
['email'] =>
'<a href="/media-management/uploads/1368166339.zip">Files</a>'
)

PHP:

//First decode the array
$arr = $_POST["json"];
$decarr = json_decode($arr, true);

$count = count($decarr);

$values = array(); // This will hold our array values so we do one single insert

for ($x=0; $x < $count; $x++){
$newrec = $decarr; 
$id = $newrec['id']; $id = mysql_real_escape_string($id);
$name = $newrec['name']; $name = mysql_real_escape_string($name);
$id_continent = $newrec['id_continent']; $id_continent = mysql_real_escape_string($id_continent);
$email = $newrec['email']; $email = mysql_real_escape_string($email);
$lastvisit = $newrec['lastvisit']; $lastvisit = mysql_real_escape_string($lastvisit); 
$cdate = $newrec['cdate']; $cdate = mysql_real_escape_string($cdate); 
$ddate = $newrec['ddate']; $ddate = mysql_real_escape_string($ddate); 


// Create insert array
$values[] = "('".$id."', '".$name."', '".$id_continent."', '".$lastvisit."','".$cdate."','".$ddate."','".$email."' )";  



}

// Insert the records   

$sql = "INSERT INTO finish (id, name, id_continent, lastvisit,cdate,ddate, email)
VALUES ".implode(',', $values);

$result = mysql_query($sql, $con) or die(mysql_error());  


?>
Community
  • 1
  • 1
NewHistoricForm
  • 121
  • 1
  • 8
  • 26
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 May 15 '13 at 17:41
  • Repeating the question doesn't improve its chances to be answered. Refining it does, though. – raina77ow May 15 '13 at 17:43
  • I understand I am a noob still so I am trying to grasp php. I will get into the habit. What can you suggest regarding my issue? – NewHistoricForm May 15 '13 at 17:45
  • Actually, apart from `mysql` usage, nothing looks wrong within PHP part. What's in `$decarr`, can you log its value? – raina77ow May 15 '13 at 17:47
  • OK I added what's in $decarr up above. So what I'm I missing? Why are 7 being posted to database? – NewHistoricForm May 15 '13 at 22:38

1 Answers1

0

Fixed! I figured out the the variables were reiterated over and over with the other lines. Much love y'all.

//First decode the array
$arr = $_POST["json"];
$decarr = json_decode($arr, true);
$count = count($decarr);


for ($x=0; $x < $count; $x++){
$newrec = $decarr; 
$id = $newrec['id']; 
$name = $newrec['name']; 
$id_continent = $newrec['id_continent']; 
$email = $newrec['email']; 
$lastvisit = $newrec['lastvisit']; 
$cdate = $newrec['cdate']; 
$ddate = $newrec['ddate']; 

}

// Create insert array
$values[] = "('".$id."', '".$name."', '".$id_continent."', '".$lastvisit."','".$cdate."','".$ddate."','".$email."' )";  





// Insert the records   

$sql = "INSERT INTO finish (id, name, id_continent, lastvisit,cdate,ddate, email)
VALUES ".implode(',', $values);

$result = mysql_query($sql, $con) or die(mysql_error());  


?>
NewHistoricForm
  • 121
  • 1
  • 8
  • 26