Alicia Posted May 28, 2008 Share Posted May 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2008 Share Posted May 28, 2008 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> Quote Link to comment Share on other sites More sharing options...
Alicia Posted May 28, 2008 Author Share Posted May 28, 2008 thanks. I am okay with the form part but not the mysql update part after they click submit.. anyone can assist ? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2008 Share Posted May 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
Alicia Posted May 28, 2008 Author Share Posted May 28, 2008 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... =) Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2008 Share Posted May 28, 2008 you treat the other items the same way as the "desc" item of course $otheritem = $_POST['otheritem'][$i]; Quote Link to comment Share on other sites More sharing options...
Alicia Posted May 28, 2008 Author Share Posted May 28, 2008 so I dont have to repeat or edit anything on this part : foreach ($_POST['name'] as $i => $name) and all I have to do is add the new variable inside the function ? please advise Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 28, 2008 Share Posted May 28, 2008 Yes -- assuming there is one for each of the names too. Why not give it a try and see if you break it? Quote Link to comment Share on other sites More sharing options...
Alicia Posted May 28, 2008 Author Share Posted May 28, 2008 I tried and get this error : Warning: Invalid argument supplied for foreach() what is that mean ? Quote Link to comment Share on other sites More sharing options...
corbin Posted May 28, 2008 Share Posted May 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
Alicia Posted May 28, 2008 Author Share Posted May 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted May 28, 2008 Share Posted May 28, 2008 post the code you have between your < form > and < /form > tags Quote Link to comment Share on other sites More sharing options...
Alicia Posted May 29, 2008 Author Share Posted May 29, 2008 <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> Quote Link to comment Share on other sites More sharing options...
Barand Posted May 29, 2008 Share Posted May 29, 2008 If that's your form code, what happened to the other variables? 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 ? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 29, 2008 Share Posted May 29, 2008 I also do not see any loop -- you must have your for loop inside your form tags, otherwise you'll be recreating the form lots of times. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.