Search the Community
Showing results for tags 'insert into mysql'.
-
I have a php script that uses <select> to display all people in a MySQL table, and then want to insert the id of all persons selected into another MySQL table. One use of this would be a way to "select" those people present. The script works, storing record ids into a seperate table. But it also records an id of "0" in addition to the record id. So every selected record is recorded with its unique id, but another record is recorded in the table with = zero! So if 4 records are selected, there will be 4 records inserted with the correct id, and 4 addition records created with id of zero. I have included the php script below, and request assistance to determine what I am doing wrong. Thank you. Wes Smith ============================================= <?php if ($_POST) { $myoptions=$_POST['options']; $selecteditems=count($myoptions); echo '<pre>'; echo htmlspecialchars(print_r($_POST,true)); echo '<pre>'; echo "Number in attendance is " . $selecteditems ; } require ('includes/mysqli_connect.php'); // Check to see if the connection failed. if (mysqli_connect_errno()) { echo "Failed to connect to MySQ:" . mysqli_connect_error(); die(); } ?> <form action="" method="post"> <select multiple name="options[]" size="20"> <?php $query = "SELECT person_id,CONCAT(last_name, ', ',first_name) AS full_name FROM Persons"; $result = mysqli_query($dbc,$query); while($row = mysqli_fetch_array($result)) { ?> <option value = "<?php echo $row['person_id'];?>"><?php echo $row['full_name']?></option> <?php } ?> </select> <input type="submit" value="submit me!" /> <?php mysqli_query($dbc, "INSERT INTO 20140420Attend(person_id) VALUES('$myoptions[0])')"); mysqli_query($dbc, "INSERT INTO 20140420Attend(person_id) VALUES('$myoptions[1])')"); mysqli_query($dbc, "INSERT INTO 20140420Attend(person_id) VALUES('$myoptions[2])')"); mysqli_query($dbc, "INSERT INTO 20140420Attend(person_id) VALUES('$myoptions[3])')"); ?> </form> ===========================