This should really use foreach instead of counting the array, but it could come in handy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php // write many array to a database in one go require('../cmdataconn.inc'); $todo_item = array('dog','cat','fish','frog'); $todo_id = array(3,6,54,23); $howmany = (count($todo_id));// count array for ($i = 0; $i < $howmany; $i++) { echo "<br>Inserted".$i; $query = "INSERT INTO list1 (todo_id,todo_item) VALUES ('$todo_id[$i]', '$todo_item[$i]')"; if ($mysqli->query($query) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $sql->error; } } $rowcount=mysqli_num_rows($query); printf("Result set has %d rows.\n",$rowcount); ?> |