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....

How It Works

Get an answer in three easy steps. Here's how it works...

Ask Your Question

1. Ask Your Question

Enter your PHP question at the top of this page and click Get An Answer.

Pick Your Priority

2. Pick Your Priority

Tell us how quickly you want your PHP question answered.

Get An Answer

3. Get An Answer

Connect with your programmer via online chat or telephone call.

Answer

Customer

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:


<?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

Posted
Naeem Mahmood
Programmer

Thank you for your question. I'll do my best to assist you.

I check your code in details and if i got you, you are trying to insert this all data by clicking on next to another table like a order table.and you want to do it using common id.
like look at code bellow.here you just need to create a table where you want to insert this all data.this is very simple.and i suggest you should create first filed with the name of 'ID' and that should be auto-increment.for example if someone click on next and all values should be goes to your created table and id will be auto-increment for each new entry.
in this way data will be inserted into new table(whatever name you can change).

if($_REQUEST['Next']=='Next'){
                            $sql="INSERT INTO ordertable (id, crtd dept, customer,case no, gross mass) values('','$crtd dept','$customer','$case_no','$gross_mass')";

                            $final=mysql_query($sql);
                            if($final)
                            {
                            echo "<meta http-equiv=\"refresh\" content=\"0;URL=planlocalpmbstep2.php\">";
                            }                                            
                                }

Now like you said that you want to use this with common id.so you just need to save common id in a variable.
like

$common_id="you id value";

and insert this value to table instead of simple ID.

I hope this answers your question. If it does, please click the Accept Answer button below the answer. If you have any follow-up questions, please post your comments below this answer.

Thanks again.
Naeem Rana

Posted
gyanendra
Programmer

Hello Ryan,

This is very common problem when you deal with order processing application .. normally in this case we create 2 table .. main and details .. we first insert data in main table and get the last generated key and use that key as common id and insert data in detail table ... following below is code
If you have an AUTO_INCREMENT column in your table, when you insert a new row, it’s automatically assigned a new number.

$result = mysql_query(“INSERT INTO Maintable (id, name) values (null,’John’)”);

Now you need a number which is created last to store in details table use this

$result = mysql_query(“INSERT INTO detailTable (id2, address) VALUES (LAST_INSERT_ID(), ‘222 Someplace Lane’)”);

This will insert the AUTOINCREMENT number from maintable to detailtable. (So long as detailtable don’t have an AUTOINCREMENT column.) This is because the lastid is specific for your connection (script) and is the same until another AUTOINCREMENT number is generated.

If you have more than one record, use loop to insert data in details table ..

I hope this answers your question. If it does, please click the Accept Answer button below the answer. If you have any follow-up questions, please post your comments below this answer.

Regards
CreativeBug

Posted
Matthew Pomar
Customer Support

Ryan,

Do either of these two answers from our Experts help you? If so, can you accept the best one? This is how our Experts get credit for their efforts.

Regards,
Matthew Pomar

Posted
Customer

Hi to all,

none of these solution are 100% what I am looking for. I'll rephrase.

If I have the output of the MySQL query as in original code, the user selects the rows they wish to process. On clicking submit these selected rows (multiple rows) need to processed and the selected rows in the mysql table updated with the same ID.

for instance

Table

ID ProductName Selected check box
1 Mash

3 Gravy selected

15 Bread selected

17 Milk

22 Honey selected

now on clciking next the selected items should be given the same order id as below:

ID ProductName OrderID
1 Mash

3 Gravy 1000123

15 Bread 1000123

17 Milk

22 Honey 1000123

This can be acheived by either updating the exisitng table or by inserting into a new table.

Thanks,
Ryan

Posted
Customer

Hi to all,

none of these solution are 100% what I am looking for. I'll rephrase.

If I have the output of the MySQL query as in original code, the user selects the rows they wish to process. On clicking submit these selected rows (multiple rows) need to processed and the selected rows in the mysql table updated with the same ID.

for instance

Table

ID ProductName Selected check box
1 Mash

3 Gravy selected

15 Bread selected

17 Milk

22 Honey selected

now on clciking next the selected items should be given the same order id as below:

ID ProductName OrderID
1 Mash

3 Gravy 1000123

15 Bread 1000123

17 Milk

22 Honey 1000123

This can be acheived by either updating the exisitng table or by inserting into a new table.

Thanks,
Ryan

Posted
Naeem Mahmood
Programmer

Good day Ryan,

like you are saying that this can be acheived by either updating the exisitng table or by inserting into a new table.so for this you just need to create a new table and after that you can insert your data into that table(this is my suggestion, you can use whatever method you like).
and on the click this will goes into table.that's all.

Thanks,
Naeem Rana

Posted
Naeem Mahmood
Programmer

Ryan,

If you have any follow-up questions, please post your comments below this answer.
we really appreciate your co-operation with our forum.

Naeem

Posted

quoteTestimonialsquote

About ExpertHelp

ExpertHelp is changing the way you connect with service professionals.

Whether you have a quick question while preparing your taxes, troubleshooting a computer problem, or need to hire an attorney, ExpertHelp is the most convenient and affordable way to connect with the right service professional to get the job done.

ExpertHelp has been in business since 2011, is an A+ Rated Better Business Bureau accredited member, and offers a 100% satisfaction guarantee on every question you ask!

More PHP Questions...

Ask Your PHP Question & Get An Answer Now!