Page 1 of 1

insert multiple selected rows with common ID

Posted: Wed Oct 05, 2011 5:56 am
by ryansmith44
Hi,

I am developing and PHP MySQL application. I have a page which displays records from a MySQL query. each record has a corresponding check box. the user then checks the check boxes they require and clicks next. On clicking next I want the selected rows to be inserted into another table with a common ID.

The concept is similar to that of an order process. slect products and all the products are inserted into an orders table with a common order number.

I am a novice PHP and MySQL guy so I am aware that my code is not 100%. If you could assist in assigning one common ID that would be appreciated.

My Code is currently:
________________________________________________________________________________

Code: Select all

<?php
    mysql_connect("localhost", "username", "password")or die("cannot connect");    
    mysql_select_db("databasename")or die("cannot select DB");
    $sql="SELECT `crtd dept`,`customer`,`case no`,`gross mass` from despgoods_alldetails where transporttypename= 'localpmb'";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
?>
<table border=1>
    <tr>
        <td>
            <form name="form1" method="post">
                <table>
                    <tr>
                        <td>#</td>
                        <td>Dispatch Area</td>						
                        <td>Customer</td>  
                        <td>Case Number</td>
						<td>Weight</td> 
                    </tr>
<?php
    while($rows=mysql_fetch_array($result)){
?>
                    <tr>
                        <td><input type="checkbox" name=check[]  value="<?php echo $rows['case no']; ?>"></td>
                        <td><?php echo $rows['crtd dept']; ?></td>
                        <td><?php echo $rows['customer']; ?></td>
                        <td><?php echo $rows['case no']; ?></td>
                        <td><?php echo $rows['gross mass']; ?></td>
                    </tr>                                   

<?php
    }
?>
                    <tr>
                        <td colspan=3><input name="Next" type="submit" id="Next" value="Next"></td>
                    </tr>
                    <?php
                    
                    
                            
                            $check=$_POST['check'];
                            
                        if($_REQUEST['Next']=='Next'){
 {
                            $sql="INSERT INTO loaddetails (dispatcharea,Customer, casenumber, weight,LOCStatus) 
							SELECT `crtd dept`,Customer,`case no`,`gross mass`,'in Load Creation'
							FROM despgoods_alldetails WHERE `Case No` = '$val'";
                            
                            foreach($check as $key=>$value)
                            {
                            $sql="INSERT INTO loaddetails (dispatcharea,Customer, casenumber, weight,LOCStatus)
							SELECT `crtd dept`,Customer,`case no`,`gross mass`,'in Load Creation'
							FROM despgoods_alldetails WHERE `Case No` = '$value'";
                            $final=mysql_query($sql);
                            if($final)
                            {
                            echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep2.php\">";
                            }                                            } 
                                }
                                }
                    // Check if delete button active, start this
                            
                            

// if successful redirect to php.php
                            
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
________________________________________________________________________________

Thanks for the help,
Ryan

Re: insert multiple selected rows with common ID

Posted: Wed Oct 05, 2011 8:44 pm
by Christopher
So your code is a little confusing because the display code that show the checkboxes also contains the code that inserts the records into the database. You should wrap the whole thing in a big if() to check if the form has been submitted. Then either write records or display the form.