Author Topic: multidimensional array and mysql insert issue  (Read 249 times)

dajesterofdeath

  • New Member
  • *
  • Posts: 5
  • Karma: 1
    • View Profile
multidimensional array and mysql insert issue
« on: May 22, 2012, 09:56:41 PM »
Hi Guys,
New to the forum, and still learning PHP. first time i've had to deal with MD arrays, and getting a bit stuck.
my query is generating INSERT INTO 4_kpiresults (kpi, kpi_id, date, value) VALUES (, 4, 23/05/2012, )(PD, , 23/05/2012, )(, , 23/05/2012, 3)(, 5, 23/05/2012, )(PN, , 23/05/2012, )(, , 23/05/2012, 4)

but as you can see it is showing incorrectly. It should be showing the following
INSERT INTO 4_kpiresults (kpi, kpi_id, date, value) VALUES (PD, 4, 23/05/2012, 3)(PN, 5, 23/05/2012,4)

I have a suspicion it is to do with the foreach section, but im at a loss as to were to go from here.

My code for the MD array and sql query is:
PHP Code: [Select]

$date1 
date("d/m/Y");

	
if (isset(
$_POST['mode'],$_POST['insertkpi']) && $_POST['mode']=='insert') {
$values = array();
foreach (
$_POST['insertkpi'] as $value) {

        
$id1   mysql_real_escape_string($value['id']);
    
$name1   mysql_real_escape_string($value['name']);
    
$value1  mysql_real_escape_string($value['value']);
    
$values[] = "($name1$id1$date1$value1)"// quoted value, not the raw value
echo $values;
}

$query_values implode(''$values);

$query "INSERT INTO $value3 (kpi, kpi_id, date, value) VALUES $query_values";
echo 
$query;

	
}


Here is part of my code for my table, it is in a while loop as the table is created from data from 2 different mysql tables:

PHP Code: [Select]

	
	
	
	
	
	
	
<
tr>
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
<
td><input type=\"hidden\" name=\"insertkpi[][id]\" maxlength=\"3\" size=\"1\" value=\"".$kpiid"\"/><input type=\"hidden\" name=\"insertkpi[][name]\" maxlength=\"3\" size=\"1\" value=\"".$resultc['name']. "\"/><strong>".$resultc['name']. "</strong></td>
	
	
	
	
	
	
	
	
<td><strong>"
.$day."</strong></td>
	
	
	
	
	
	
	
	
<td><strong>"
.$mth."</strong></td>
	
	
	
	
	
	
	
	
<td><strong>"
.$yrlytotal."</strong></td>
	
	
	
	
	
	
	
	
<td><strong>"
.$kpires['value']."</strong></td>
	
	
	
	
	
	
	
	
<td><strong>"
.$rowa['value']."</strong></td>
	
	
	
	
	
	
	
	
<td><strong>"
.$rowb['value']."</strong></td>";

if (
$kpires['value'] < 1) {
echo 
"<td width = '50'><input type=\"text\" name=\"insertkpi[][value]\" maxlength=\"3\"/></td>";
}
else {
echo 
"<td width = '50'><font color=\"green\">Submitted.</font></td>";
}
echo 
"</tr>";

?>
</table>


my table works find, however its just the MD array to query i need to fix

Sabmin

  • Senior Member
  • ****
  • Posts: 154
  • Karma: 4
    • View Profile
Re: multidimensional array and mysql insert issue
« Reply #1 on: May 23, 2012, 05:17:01 PM »
if you add
PHP Code: [Select]
print_r($_POST['insertkpi']); after the loop what do you get?

dajesterofdeath

  • New Member
  • *
  • Posts: 5
  • Karma: 1
    • View Profile
Re: multidimensional array and mysql insert issue
« Reply #2 on: May 23, 2012, 06:41:03 PM »
Thanks for the  reply, i have now worked out where i went wrong. it was an issue with the array, i included a counter and then did

name="insertkpi[<?php echo $counter; ?>][id]"

in my tables.

this way the arrays had a common reference to link to.

i have this outside my while loop:

PHP Code: [Select]
$counter=0;

and this is the section of code for the table:
PHP Code: [Select]

<tr>
	
	
	
	
	
	
	
	
	
	
<
td><input type="hidden" name="insertkpi[<?php echo $counter; ?>][id]" maxlength="3" size="1" value="<?php echo $kpiid; ?>" /><input type="hidden" name="insertkpi[<?php echo $counter; ?>][name]" maxlength="3" size="1" value="<?php echo $resultc['name']; ?>" /><strong><?php echo $resultc['name']; ?></strong></td>
	
	
	
	
	
	
	
	
	
	
<td><strong><?php echo $day?></strong></td>
	
	
	
	
	
	
	
	
	
	
<td><strong><?php echo $mth?></strong></td>
	
	
	
	
	
	
	
	
	
	
<td><strong><?php echo $yrlytotal?></strong></td>
	
	
	
	
	
	
	
	
	
	
<td><strong><?php echo $kpires['value']; ?></strong></td>
	
	
	
	
	
	
	
	
	
	
<td><strong><?php echo $rowa['value']; ?></strong></td>
	
	
	
	
	
	
	
	
	
	
<td><strong><?php echo $rowb['value']; ?></strong></td>
	
	
	
	
	
	
	
	
	
	
<td width='50'>
	
	
	
	
	
	
	
	
	
	
<?php
	
	
	
	
	
	
	
	
	
	
if (
$kpires['value'] < 1) { ?><input type="text" name="insertkpi[<?php echo $counter?>][value]" maxlength="3" /><?php }
	
	
	
	
	
	
	
	
	
	
else { 
?><font color="green">Submitted.</font><?php }
	
	
	
	
	
	
	
	
	
	
?>
	
	
	
	
	
	
	
	
	
	
</td>
	
	
	
	
	
	
	
	
	
</tr>
	
	
	
	
	
	
	
	
	
<?php
	
	
	
	
	
	
	
	
	
$counter++;



Sabmin

  • Senior Member
  • ****
  • Posts: 154
  • Karma: 4
    • View Profile
Re: multidimensional array and mysql insert issue
« Reply #3 on: May 24, 2012, 11:28:12 AM »
Great! Glad you got it working!