abs0lut Posted May 4, 2008 Share Posted May 4, 2008 <?php foreach(($niqid as $ids) && ($_POST['cost'] as $pids)) { $sql = mysql_query("INSERT INTO ctable (name, cost, date, post) VALUES ('$ids', '$pids', '$date', '$post')") or die (mysql_error()); } ?> please help me... Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 4, 2008 Share Posted May 4, 2008 Do $niqid and $_POST['cost'] have the same number of elements? And are they numerically indexed consecutively from 0? <?php for ($i=0; $i<count($niqid); $i++) { $query = "INSERT INTO ctable (name, cost, date, post) VALUES ('".$niqid[$i]."', '".$_POST['cost'][$i]."', '$date', '$post')"; $sql = mysql_query($query) or die (mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
abs0lut Posted May 4, 2008 Author Share Posted May 4, 2008 Do $niqid and $_POST['cost'] have the same number of elements? And are they numerically indexed consecutively from 0? <?php for ($i=0; $i<count($niqid); $i++) { $query = "INSERT INTO ctable (name, cost, date, post) VALUES ('".$niqid[$i]."', '".$_POST['cost'][$i]."', '$date', '$post')"; $sql = mysql_query($query) or die (mysql_error()); } ?> how will I know if $_POST['cost'] and $niqid have the same number of elements? Quote Link to comment Share on other sites More sharing options...
helraizer Posted May 4, 2008 Share Posted May 4, 2008 Is $_POST['cost'] actually an array? You could try it like this <?php foreach($niqid as $ids) { foreach($_POST['cost'] as $pids) { $sql = mysql_query("INSERT INTO ctable (name, cost, date, post) VALUES ('$ids', '$pids', '$date', '$post')") or die (mysql_error()); } } ?> Sam Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 4, 2008 Share Posted May 4, 2008 how will I know if $_POST['cost'] and $niqid have the same number of elements? You, as the developer, should know where your variables are coming from, what they "should" contain, and their format. So, why are you asking me? If they do not have the same number of elements then I don't have a clue what you are tring to accomplish in the code you posted - which is syntactically incorrect. It appeared to me you were trying to iterrate through both arrays simultaneously an run those queries. For example if the first array contains 1, 2, & 3 and the second array contains A, B, & C you would want queries using the combinations 1A, 2B, & 3C. However, if you want to do a matrix between the two arrays (1A, 1B, 1C, 2A, 2B, 2C, 3A, 3B, & 3C) then that is something different (which I think helraizer's code addresses). Since you didn't state what you are trying to do, I made a guess. I put this in my sig for a reason: The quality of the responses received is directly porportional to the quality of the question asked. Quote Link to comment Share on other sites More sharing options...
abs0lut Posted May 5, 2008 Author Share Posted May 5, 2008 thank you very much, mjdamato now I know Quote Link to comment Share on other sites More sharing options...
helraizer Posted May 6, 2008 Share Posted May 6, 2008 I'm still not sure/convinced that $_POST['cost'] can be an array though. I could be wrong, but it's a single posted value, surely? Therefore foreach will have no effect on it. Although you aren't recieving an error for it, so I could well be wrong. Is it? Sam Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 6, 2008 Share Posted May 6, 2008 I'm still not sure/convinced that $_POST['cost'] can be an array though. I could be wrong, but it's a single posted value, surely? Therefore foreach will have no effect on it. Although you aren't recieving an error for it, so I could well be wrong. Is it? Sam Yes you can have POST values that are arrays - just give multiple fields the same array name. Here's an example of a form where an array would be returned: <input type="text" name="cost[]"> <input type="text" name="cost[]"> <input type="text" name="cost[]"> Quote Link to comment Share on other sites More sharing options...
helraizer Posted May 6, 2008 Share Posted May 6, 2008 Ah ok. You learn something new everyday. 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.