php - insert data to table in for loop -


here's table

id | total  | adult | kid | babies 

here's data inserted

total = 3, adult = 2, kid = 1, babies = 0

it should inserted

id | total  | adult | kid | babies  1 |     3  |     1 |   0 |      0   2 |     3  |     1 |   0 |      0  3 |     3  |     0 |   1 |      0 

how can insert data above structure ?

i tried

for ($i=0; $i <$total ; $i++)  {  $query = "insert trip(total, adult, kid, babies)  values (     '".$total."',     '".$adult."',     '".$kid."',     '".$babies."'     )"; $mysqli->query($query); } 

it inserting same data. how can insert data 1 given above ?

you need have 3 different loops, 1 adults, 1 kids , 1 babies:

//prepare insert statement. $mysqli->prepare = "insert trip(total, adult, kid, babies) values (?, ?, ?, ?)";  //bind variables. $mysqli->bind_param('iiii', $total, $flagadult, $flagkid, $flagbaby);  //insert adults. $flagadult = 1; $flagkid = 0; $flagbaby = 0; ($i=0; $i<$adult; $i++)  {      $mysqli->execute(); }  //insert kids. $flagadult = 0; $flagkid = 1; $flagbaby = 0; ($i=0; $i<$kid; $i++)  {      $mysqli->execute(); }  //insert babies. $flagadult = 0; $flagkid = 0; $flagbaby = 1; ($i=0; $i<$babies; $i++)  {      $mysqli->execute(); } 

your code vulnerable sql injection attacks, changed use bind_param, see documentation here , here.

i asumed data integers (since there 0 , 1), if in fact want use strings reason need change 'iiii' (meaning 4 integers) 'ssss' (meaning 4 strings).


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -