PDA

View Full Version : Trying to insert multiple rows from array


Alith7
08-03-2009, 05:31 PM
I have this insert statement:

if ($totalRows_getInk1==0) {
$iic1 = 0;
foreach ($_POST['Ink1Data'] as $key => $value) {
extract($value);
$insertInk1 = sprintf("INSERT INTO InkSide1 (JobID, InkID, Ink1Desc) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['JobID'], "int"),
GetSQLValueString($InkID, "int"),
GetSQLValueString($InkDesc, "text"));
mysql_query($insertInk1, $geQuote) or die(mysql_error());
}
}
else {
foreach ($_POST['Ink1Data'] as $key => $value) {
extract($value);
if ($InkID1!=NULL) {
$insertInk1 = sprintf("UPDATE InkSide1 SET JobID=%s, InkID=%s, Ink1Desc=%s WHERE Ink1ID=%s",
GetSQLValueString($_POST['JobID'], "int"),
GetSQLValueString($InkID, "int"),
GetSQLValueString($InkDesc, "text"),
GetSQLValueString($InkID1, "int"));
mysql_query($insertInk1, $geQuote) or die(mysql_error());
}
else {
$insertInk1 = sprintf("INSERT INTO InkSide1 (JobID, InkID, Ink1Desc) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['JobID'], "int"),
GetSQLValueString($InkID, "int"),
GetSQLValueString($InkDesc, "text"));
mysql_query($insertInk1, $geQuote) or die(mysql_error());
}
}
}

and this is the form script that feeds it:
if ($totalRows_getInk1==0) {
echo "<div id='ink11'>
<p>Ink:
<select name='Ink1Data[1][InkID]' class='dropMenu' onchange='addInkOpt(1,2)'>
<option value='NULL'></option>";
do {
echo "<option value='".$row_inkList['InkID']."'>".$row_inkList['InkDesc']."</option>";
}
while ($row_inkList = mysql_fetch_assoc($inkList));
$rows = mysql_num_rows($inkList);
if($rows > 0) {
mysql_data_seek($inkList, 0);
$row_inkList = mysql_fetch_assoc($inkList);
}
echo "</select> Desc:
<input name='Ink1Data[1][InkDesc]' type='text' class='inkdesc' maxlength='25' />
</p>
</div>"; }
else {
$cink1=0;
do {
$cink1++;
$jsVar1=$cink1+1;
echo "<div id='ink1".$cink1."'>
<p>Ink:
<select name='Ink1Data[".$cink1."][InkID]' class='dropMenu' onchange='addInkOpt(1,".$jsVar1.")'>
<option value='NULL'></option>";
do {
echo "<option value='".$row_inkList['InkID']."'";
if (!(strcmp($row_inkList['InkID'], $row_getInk1['InkID']))) {echo "selected=\"selected\"";}
echo ">".$row_inkList['InkDesc']."</option>";
}
while ($row_inkList = mysql_fetch_assoc($inkList));
$rows = mysql_num_rows($inkList);
if($rows > 0) {
mysql_data_seek($inkList, 0);
$row_inkList = mysql_fetch_assoc($inkList);
}
echo "</select>Desc:
<input name='Ink1Data[".$cink1."][InkDesc]' type='text' class='inkdesc' maxlength='25' value='".$row_getInk1['Ink1Desc']."' />
<input type=hidden name='Ink1Data[".$cink1."][InkID1]' value=".$row_getInk1['Ink1ID']." /></p></div>"; }
while ($row_getInk1 = mysql_fetch_assoc($getInk1));
$rows = mysql_num_rows($getInk1);
if($rows > 0) {
mysql_data_seek($getInk1, 0);
$row_getInk1 = mysql_fetch_assoc($getInk1);
} }


It inserts and updates the first row just fine, but it doesn't insert the 2nd or 3rd row.

I've been fighting with this for a while and I can't seem to figure out what I'm doing wrong.

This was my test script, and it echoed the whole array no problem.
if (!isset($_POST['submit']))
{
echo '<form action="" method="post">
<p>Ink Type: <select name="data[1][InkID]">
<option value="Spot">Spot</option>
<option value="Process">Process</option>
<option value="Varnish">Varnish</option>
</select><br />
Ink Desc: <input name="data[1][Ink1Desc]" /></p>
<p>Ink Type: <select name="data[2][InkID]">
<option value="Spot">Spot</option>
<option value="Process">Process</option>
<option value="Varnish">Varnish</option>
</select><br />
Ink Desc: <input name="data[2][Ink1Desc]" /></p>
<p>Ink Type: <select name="data[3][InkID]">
<option value="Spot">Spot</option>
<option value="Process">Process</option>
<option value="Varnish">Varnish</option>
</select><br />
Ink Desc: <input name="data[3][Ink1Desc]" /></p>
<input type="submit" name="submit" value="submit">
</form>
';
}
else
{
$JobID='2010';
$x= 0;
foreach ($_POST['data'] as $key => $value) {
extract($value);
$x++;
echo "<p>For Job ID #$JobID Ink $x has InkID $InkID, and Ink Description $Ink1Desc </p>";
}
}

If someone could please help me with this I would appreciate it.
I wish I had another coder here to look over my code when it's being difficult, but I don't, so I appreciate all the help I've been getting here on the forums!:thumbsup:

Alith7
08-03-2009, 06:04 PM
ok, so on further playing, I manually entered a few more rows of Data.

it echos all the rows on load into the form, and on submit will update ALL rows, but it will not insert any new rows.

totally new inserts are still only inserting 1 row.

tomws
08-04-2009, 12:30 AM
At the risk of sounding like one of those "here's an unrelated code critique" people, I'm confused by the logic in the top code block and can't figure out why it's structured like that. Wouldn't this do the same thing?
if ($totalRows_getInk1==0) {
$iic1 = 0;
}
foreach ($_POST['Ink1Data'] as $key => $value) {
extract($value);
if ($InkID1!=NULL) {
$insertInk1 = sprintf("UPDATE InkSide1 SET JobID=%s, InkID=%s, Ink1Desc=%s WHERE Ink1ID=%s",
GetSQLValueString($_POST['JobID'], "int"),
GetSQLValueString($InkID, "int"),
GetSQLValueString($InkDesc, "text"),
GetSQLValueString($InkID1, "int"));
}
else {
$insertInk1 = sprintf("INSERT INTO InkSide1 (JobID, InkID, Ink1Desc) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['JobID'], "int"),
GetSQLValueString($InkID, "int"),
GetSQLValueString($InkDesc, "text"));
}
mysql_query($insertInk1, $geQuote) or die(mysql_error());
}

No, it shouldn't fix your problem, but it makes me feel better. :D

Seriously, though, I can't see from what you've provided why it will INSERT only once.

Alith7
08-04-2009, 02:45 PM
thank you! That does make a lot more sense. I was more focused on getting it to work than streamlining the code.

oh, and I did figure out the reason it wasn't working. The JS script that was inserting more divs as needed wasn't formatted correctly (I had forgot to go back and change it for the array). I spent most of my day picking apart a piece of code that was working just fine, while not looking at the real problem. I hate days like that.

The other problem was that I didn't have the variable InkID1 identified for new inserts, it was causing a problem with the IF statement. so I added <input type='hidden' name='Ink1Data[1][InkID1]' value='' /> to new ones and it all worked just fine.

Alith7
08-04-2009, 02:51 PM
Here's the final code block. Thank you again for your help!
foreach ($_POST['Ink1Data'] as $key => $value) {
extract($value);
if ($InkID1==NULL) {
$insertInk1 = sprintf("INSERT INTO InkSide1 (JobID, InkID, Ink1Desc) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['JobID'], "int"),
GetSQLValueString($InkID, "int"),
GetSQLValueString($InkDesc, "text"));
}
else {
$insertInk1 = sprintf("UPDATE InkSide1 SET JobID=%s, InkID=%s, Ink1Desc=%s WHERE Ink1ID=%s",
GetSQLValueString($_POST['JobID'], "int"),
GetSQLValueString($InkID, "int"),
GetSQLValueString($InkDesc, "text"),
GetSQLValueString($InkID1, "int"));
}
mysql_query($insertInk1, $geQuote) or die(mysql_error());
}