multidimensional array and mysql insert issue

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]
$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;

}

[/php]

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]

							<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 “

<input type=“text” name=“insertkpi[][value]” maxlength=“3”/>”;
}
else {
echo “<font color=“green”>Submitted.”;
}
echo “”;

} ?>

[/php]

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

if you add [php]print_r($_POST[‘insertkpi’]);[/php] after the loop what do you get?

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]$counter=0;[/php]

and this is the section of code for the table:
[php]

<?php echo $resultc['name']; ?> <?php echo $day; ?> <?php echo $mth; ?> <?php echo $yrlytotal; ?> <?php echo $kpires['value']; ?> <?php echo $rowa['value']; ?> <?php echo $rowb['value']; ?> <?php if ($kpires['value'] < 1) { ?><?php } else { ?>Submitted.<?php } ?> <?php $counter++; [/php]

Great! Glad you got it working!

Sponsor our Newsletter | Privacy Policy | Terms of Service