phpsql1 Posted June 13, 2007 Share Posted June 13, 2007 I do have the following List: <select name="lectureList" size="5"> <option>aaaaaa</option> <option>bbbbbbbbbbbbb</option> <option>cccccccc</option> <option>ddddddd</option> <option>eeeeeeeee</option> <option>fffffffffffff</option> <option>gggggggggggggg</option> <option>hhhhhhhhhhhhhhh</option> <option>iiiiiiiiiiiiiiiiiiiiiii</option> </select> And here I assign the variables: $lecture = trim($_POST[lecture ]); $prof= trim($_POST[prof]); $lecturetype= trim($_POST[lecturetype]); here I am trying to use foreach to add ALL the values of lecturetype list to a mysql table called lecture foreach($lecturetype as $temptype) { $query = "INSERT INTO lecture(lecture, prof, type) VALUES ('$prof', '$prof','$temptype')"; mysql_query($query) or die("Query failed: " . mysql_error()); } After running the program I got the following error: Warning: Invalid argument supplied for foreach() the explination is that foreach accepts only arrays, and $lecturetype is not an array. I am really stack with this any one have any idea. Quote Link to comment https://forums.phpfreaks.com/topic/55369-problem-with-using-foreach-to-insert-all-the-values-of-a-list-into-a-table/ Share on other sites More sharing options...
kritical Posted June 13, 2007 Share Posted June 13, 2007 Can you provide an example of your actual form? You have a select named "LectureList" however this isn't used anywhere in your PHP code? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/55369-problem-with-using-foreach-to-insert-all-the-values-of-a-list-into-a-table/#findComment-273648 Share on other sites More sharing options...
quickstopman Posted June 13, 2007 Share Posted June 13, 2007 the why its not working is because $lecturetype is not an array and foreach takes apart arrays and makes them into lists what you would wanna do is $lecture = trim($_POST[lecture ]); $prof= trim($_POST[prof]); $lecturetype= trim($_POST[lecturetype]); $lecturelist = array($lecture, $prof, $lecturetype); foreach($lecturelist as $temptype) { $query = "INSERT INTO lecture(lecture, prof, type) VALUES ('$prof', '$prof','$temptype')"; mysql_query($query) or die("Query failed: " . mysql_error()); } and that should work Quote Link to comment https://forums.phpfreaks.com/topic/55369-problem-with-using-foreach-to-insert-all-the-values-of-a-list-into-a-table/#findComment-273649 Share on other sites More sharing options...
phpsql1 Posted June 13, 2007 Author Share Posted June 13, 2007 I assigned the lecturetype List the the php variable $lecturetype, in the hope that it will be considered as an array. <form action="submit.php" name="lecturefrm" method="post"> <table width="400" align="center" border="0" cellpadding="0" cellspacing="6"> <tr> <td style="color: #808080;">Lecture:</td> <td><input type="text" name="lecture" style="background-color: #cccccc; font-size: 16px; font-size: 16px" size="30" maxlength="45" ></td> </tr> <tr> <td style="color: #808080;">Prof: </td> <td><input type="text" name="Prof" style="background-color: #cccccc; font-size: 16px" size="30" maxlength="60"> </tr> <tr> <td>Lecture type:</td> <td style="color: #808080;"> <select name="lectureList" size="5"> <option>aaaaaa</option> <option>bbbbbbbbbbbbb</option> <option>cccccccc</option> <option>ddddddd</option> <option>eeeeeeeee</option> <option>fffffffffffff</option> <option>gggggggggggggg</option> <option>hhhhhhhhhhhhhhh</option> <option>iiiiiiiiiiiiiiiiiiiiiii</option> </select> </td> <tr> </table> </form> this is pretty much what I do have, I did not post the submit button, because I think it is not that important. Hope this hepls Again What I trying to do is to insert all the values in the list below into a mysql table. if there is any other approach please enlighten me. Quote Link to comment https://forums.phpfreaks.com/topic/55369-problem-with-using-foreach-to-insert-all-the-values-of-a-list-into-a-table/#findComment-273654 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.