Jump to content

looping and mysql update issue


Alicia

Recommended Posts

Hi guys,

 

I am creating a form with few form elements and I want to insert/update all the records in the database after they press submit...

 

currently I am using for loop to accomplish this :

assuming the user choose to insert 5 records, so I use this :

 

for($i=1; $i <6; $i++)

{

//variable used -> ${name.$i}, ${desc.$i} and etc

//variable name will be something like name1, desc1, name2, desc2 and etc

}

 

when I click update and post all the information to another file, how can I insert/update all these records accordingly .. i used the code below but it doesnt work.. any idea how can I get this fixed ?

 

// update records update submission

$incart = $incart + 1;

for($i = 1; $i <$incart ; $i++)

{

$name = ${'p'.$i};

$desc = ${'desc'.$i}

$update[$i]= mysql_query("UPDATE `items` SET `name` = '$name', `desc` = '$desc' WHERE `id` = '$id' AND `pro` = '${'productid'.$i}'") or die(mysql_error());

 

}

 

 

Please advise. thanks.

Link to comment
https://forums.phpfreaks.com/topic/107578-looping-and-mysql-update-issue/
Share on other sites

It's easier to use arrays

 

<?php
if (isset($_POST['btnSub']))
{
foreach ($_POST['name'] as $i => $name)
{
	$desc = $_POST['desc'][$i];                                         // get corresponding desc item

	// INSERT INTO table (name, description) VALUES ('$anme', '$desc');
}
}

?>
<form method='post'>
       <?php 
         	for ($i=1; $i < 6; $i++)
         	{
         		echo "Name <input type='text' name='name[$i]' size='12'><br>";
         		echo "Desc <input type='text' name='desc[$i]' size='12'><br>"; 
         	}
         	echo "<input type='submit' name='tbnSub' value='Submit'>";
       ?>
</form>  

If you actually take the time to look at the sample code I provided you will see it is in two parts.

 

The lower part shows how you should be constructing the form.

 

The upper part shows you how to process the data after the user clicks submit.

Errr...

 

In the format

foreach(array as [key =>] value) {

 

}

 

That means that array is not an array.

 

In other words, $_POST['name'] isn't an array x.x.

 

I can't figure out why it's not though....

 

You're using Barand's code, right?

 

Maybe I'm not seeing something.... 3AM lol

<form id="form1" name="form1" method="post" action="update.php">

<table width="100%" border="0" cellspacing="0" cellpadding="5">

  <tr>

    <td>Name</td>

    <td><strong>

      <input name="<? echo "name[$i]"; ?>" type="text" value="<? echo "$D"; ?>" />

    </strong></td>

  </tr>

  <tr>

    <td>Desc</td>

    <td><strong>

      <textarea name="desc[$i]" cols="60" rows="10"><?  echo "$E" ?>

                            </textarea>

    </strong></td>

  </tr>

</table>

 

</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.