Jump to content

problem with using foreach to insert all the values of a list into a table.


phpsql1

Recommended Posts

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.

 

 

 

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

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.

 

Archived

This topic is now archived and is closed to further replies.

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