oni-kun Posted December 23, 2009 Share Posted December 23, 2009 I have three arrays (of 43 elements each) sent from an html form: done[], len[], and weight[] How do I use a loop with insert for a query?.. for example: foreach ($_POST['len'] as $v1) { echo $v1; } Will echo all of $_POST['len'][0-43], but how do I loop all three $_POST['done'][0-43], $_POST['weight'][0-43] arrays and put them into one INSERT query?! In the SQL, my structure is "ID, done, length, weight" .. so I just need to somehow loop from 0-43 and insert each value. Quote Link to comment Share on other sites More sharing options...
trq Posted December 23, 2009 Share Posted December 23, 2009 for ($i = 0; $i <=43; $i++) { $sql = " INSERT INTO (done,length,weight ) VALUES ( '" . mysql_real_escape_string($_POST['done'][$i] ."', '" . mysql_real_escape_string($_POST['length'][$i] ."', '" . mysql_real_escape_string($_POST['weight'][$i] . "' "; } Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 23, 2009 Author Share Posted December 23, 2009 You forgot the ")" at the end of each $_POST's! but nonetheless this works perfectly, was confused if I had to use a foreach loop or not, This way is the way I like it, simple Thanks. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 23, 2009 Author Share Posted December 23, 2009 Actually another question. Since I inserted all of them so the SQL table is filled now, I need them to update, not submit more: "UPDATE personal SET weight = $_POST[weight][$i] WHERE ID = $i" Will that work? An example is if I filled in another textbox, as they all will slowly be filled in over time. Just want them to update and populate slowly. Quote Link to comment Share on other sites More sharing options...
venkyphp Posted December 23, 2009 Share Posted December 23, 2009 You are asking about database result or to extract the array elements? we have two different ways as per the requirement.. 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.