wesmith4 Posted April 24, 2014 Share Posted April 24, 2014 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> =========================== Link to comment Share on other sites More sharing options...
wesmith4 Posted April 25, 2014 Author Share Posted April 25, 2014 Let me share a little more informatiom: Let me provide a little more information: I find the following six records in the table 20140420Attend after running the script and selecting only three records. Why are the additional three records with person_id being inserted into the table? Everytime I run the script, there is inserted into the table three additional records with person_id=0. Can anyone help me understand why the three additional records with id of zero are added? Any help would be apprecialted. Wes Smith Link to comment Share on other sites More sharing options...
HartleySan Posted April 25, 2014 Share Posted April 25, 2014 What does your POST array look like? Have you tried running the queries directly on the DB to see what happens? Link to comment Share on other sites More sharing options...
wesmith4 Posted April 25, 2014 Author Share Posted April 25, 2014 You can see how the script operates for the next several hours by clicking here. This link will not work for very long. Wes Smith Link to comment Share on other sites More sharing options...
HartleySan Posted April 25, 2014 Share Posted April 25, 2014 I just tried to go to it, and it didn't work. Link to comment Share on other sites More sharing options...
wesmith4 Posted April 25, 2014 Author Share Posted April 25, 2014 Please try this link: learningpreferences.org/atest4.php Sorry for my errors. Link to comment Share on other sites More sharing options...
HartleySan Posted April 26, 2014 Share Posted April 26, 2014 Have you tried running a query like the following directly on the DB? INSERT INTO 20140420Attend (person_id) VALUES (199); Link to comment Share on other sites More sharing options...
wesmith4 Posted April 26, 2014 Author Share Posted April 26, 2014 Yes, and it worked just fine. Here is the script to test if running a query to directly insert into the table: ========================== <?php 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(); } mysqli_query($dbc, "INSERT INTO 20140420Attend(person_id) VALUES(199)"); ?> ============= The result was exactly one record was inserted for person_id = 199. Thank you for looking into this with me. Link to comment Share on other sites More sharing options...
HartleySan Posted April 26, 2014 Share Posted April 26, 2014 So problem solved, no? Link to comment Share on other sites More sharing options...
StephenM Posted April 26, 2014 Share Posted April 26, 2014 Hi there, I'm not sure if this is the source of the problem but you could check. What happens if you make the following change on line 3: <?php if ($_POST) { $myoptions=$_POST['options'][0]; Link to comment Share on other sites More sharing options...
Recommended Posts