Hi poeple!
I'am facing a serious problem with PHP.
I have a table with two column's.
In the first one there is generaited names of animals from database.
Beside names in the second columns of the table I have an select option dropdown list generaited from database with some values to chose like mammals, fish, insekts etc.
If we say there is 5 animals in the name column then it will pop up beside them on the other column of the table 5
the same select options with their values. I mean five select options in different rows but they are the same with the same values offcourse. My question is How do I collect those values from five different rows in array and then INSERT them to database? For animals id's I did like this,
To save them all populated id's of those 5 animals in array and insert them to database table with name "all_animals" I did
$sql_animals = "SELECT * FROM anilams WHERE group_id = 1 "; // grup_id means if the animals is from africa, Asia ....
$result = mysql_query($sql_animals);
while ($row = mysql_fetch_assoc($result)){
$animal_array[] = $row['animal_id'];
}
foreach($animal_array as $value)
{
$sql_stu = "INSERT INTO all_animals (animal_id) VALUES ('$value')";
mysql_query($sql_stu) OR die(mysql_error());
}
And all id's inserted to the table. But When I try to do the same with select options like this:
print '<select name="kindOfAnimal[]">';
$sql_animalType = "SELECT * FROM kindOfAnimal ";
$rst = mysql_query($sql_animalType);
while ($row = mysql_fetch_array($rst)){
print '<option value="'.$row['animalType_id'].'">'.$row['animalType_name'].'</option>';
$Type_array[] = $row['animalType_id'];
foreach($Type_array as $Type_value)
{
$sql_TypeOF = "INSERT INTO all_animals (animalType_id) VALUES ('$Type_value')";
mysql_query(sql_TypeOF) OR die(mysql_error());
// I tried even like below…..
print '<option value="'.$row['animalType_id'].'">'.$row['animalType_name'].'</option>';
// I tried even to echo of COUNTS($Type_array)…. Only one rows shows
}
}
print '</select>';
And inserts nothig. Only 0. But if I delete the bracket after the name of the select option.. I mean <select name="kindOfAnimal[]"> and writing instead <select name="kindOfAnimal"> then value of the last select option inserts but not thos others. I want actualy INSERT both id's of animals and kindOfAnimal to table "all_animals" in there by coding
$sql = "INSERT INTO all_animals (animal_id, animalType_id ) VALUES ('$value', '$Type_value')";
mysql_query(sql_TypeOF) OR die(mysql_error());
But as I said before all animal_id inserting but from select option side only the value of the last one inserts.
I hope you have the hole picture of the problem...
And Pls help.... How can I collect all values of the select options those who are in different rows of the table and INSERT them to database. Thank you for your time.