Jump to content
Larry Ullman's Book Forums

Inserting Into Mysql Using Php And <Select>


Recommended Posts

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

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

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

 Share

×
×
  • Create New...