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

Link to comment
Share on other sites

thanks... since I have more than 2 variables, so how should I go about ? how can I put more than one variable in foreach function ?

 

i did try to check php.net but have no idea how to get this work.. your advise will be appreciated... =)

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

i would like to ask if i use combination of html and php, how to make the script right ? I tried this but failed..

 

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

 

please advise

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.