error: how to insert values into mysql using loop?


#1

hi there,

I have this code allow user to enter age and gender of children and then insert them to DB. when I echo the variables. I can get all values from user because user can enter more than one child. However, when I want to insert into DB, it will insert only the data of last children

ex:
if user inter two children one with age: 3 gender M and second age 5 gender F, the second values only will be inserted
[php] $age=$_POST[‘age’];
$gender=$_POST[‘gender’];

for($i=0;$i<count($gender);$i++)
{
if($age[$i]!="" && $gender[$i]!="")
{
echo $age[$i];
echo $gender[$i];
$quiry = “INSERT INTO children(age , gender)VALUES(’$age[$i]’ , ‘$gender[$i]’)”;
}
}

 $result = mysqli_query($connection, $quiry);

//echo $result;
echo “

”;
if (!$result)
{
die("Query Faile".  mysqli_errno($connection));   

}[/php]


#2

You can check out my website for sample code on how to insert form array data into a database.
https://galaxyinternet.us/php-insert-form-array/