SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 39
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How do I loop through a form and post to database?

    Hi,
    I have a form with multiple tables, and I need to post the data into a single database table. Look at attached images.
    tables.pngform.jpg

    Here is a code example of how my form is generated:
    PHP Code:
    <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='100 Series'"// 100 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 100;
                
    $rowcounter 0
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
            </div>
            <div class="left">
            <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='150 Series'"// 150 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 150;
                
    $rowcounter 0;
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>150 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
            </div>
    Inserting the data in the order_table is simple enough, because it is not repeating information. The data from the above tables (there are more than these two), will all be inserted into the same table order_details_table.

    How do I loop through these tables, and insert the data in the database table???

    thanks

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I understand the question, you want to loop through each series to insert the data in a MySQL table. If so, use arrays. Add square brackets to the input fields names (using heredoc style):
    PHP Code:
    name="{$series}perpallet[{$rowcounter}]" 

    PHP Code:
    $rowcounter 0;  
    echo 
    '<table class="styled-table">'
    echo 
    '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>'
    while(
    $row mysql_fetch_array($result)){ 
        
    $rowcounter++; 
        
    $output = <<<DATADATA
        <tr><td></td> 
        <td><input type="text" name="
    {$series}perpallet[{$rowcounter}]" id="{$series}perpallet{$rowcounter}" style="width:50px;" value="{$row['per_pallet']}" readonly /></td> 
        <td><input type="text" name="
    {$series}quantity[{$rowcounter}]" id="{$series}quantity{$rowcounter}" style="width:50px;" value="0" onChange="getTotal({$series},{$rowcounter});" /></td> 
        <td><input type="text" name="
    {$series}pallets[{$rowcounter}]" id="{$series}pallets{$rowcounter}" style="width:50px;" value="0" class="pallets" readonly /></td> 
        <td><input type="text" name="
    {$series}extras[{$rowcounter}]" id="{$series}extras{$rowcounter}" style="width:50px;" value="0" class="extras" readonly /></td> 
        </tr>
    DATADATA;
        echo 
    $output;

    echo 
    '</table>'
    Denny Schlesinger
    web services

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry because I'm a newbie in PHP and doing this using the forum and google searches.

    So once they are arrays, I just use simple insert statements to insert the data? Don't I have to loop through the html tables to grab the information? Do I use simple insert for every html table individually?

    thanks for your help

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ROUBOS View Post
    Sorry because I'm a newbie in PHP and doing this using the forum and google searches.

    So once they are arrays, I just use simple insert statements to insert the data? Don't I have to loop through the html tables to grab the information? Do I use simple insert for every html table individually?

    thanks for your help
    After you add the square brackets to the name tag, add
    PHP Code:
    print_r ($_POST); 
    to the top of your action page. Then fill out a test form and submit it. The print_r() function will show you what the form returns. With that in hand you can plan the sql statements. Basically you'll be doing the following:
    PHP Code:
    foreach($_POST['name'] AS $key=>$value) {
        
    $sql "insert into.....";
        
    $result mysql_query($sql) or die("didn't work");

    Let me know how it goes.
    Denny Schlesinger
    web services

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for your help.
    I'm getting an error "Parse error: syntax error, unexpected '<<' (T_SL) in C:\xampp\htdocs\hb_bots\bots\order-form.php on line 170"

    Do I need to do something for the heredoc style to be picked up? Using XAMPP locally on my machine for this.

    appreciate your help

  6. #6
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ROUBOS View Post
    Hi, thanks for your help.
    I'm getting an error "Parse error: syntax error, unexpected '<<' (T_SL) in C:\xampp\htdocs\hb_bots\bots\order-form.php on line 170"

    Do I need to do something for the heredoc style to be picked up? Using XAMPP locally on my machine for this.

    appreciate your help
    heredoc should work with XAMPP as far as I know. Can you post the code that's giving you problems? Maybe you have extra spaces after the identifier, that's a common problem.
    Denny Schlesinger
    web services

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $output = <<<DATADATA
    <tr><td></td>
    <td><input type="text" name="{$series}perpallet[{$rowcounter}]" id="{$series}perpallet{$rowcounter}" style="width:50px;" value="{$row['per_pallet']}" readonly /></td>
    <td><input type="text" name="{$series}quantity[{$rowcounter}]" id="{$series}quantity{$rowcounter}" style="width:50px;" value="0" onChange="getTotal({$series},{$rowcounter});" /></td>
    <td><input type="text" name="{$series}pallets[{$rowcounter}]" id="{$series}pallets{$rowcounter}" style="width:50px;" value="0" class="pallets" readonly /></td>
    <td><input type="text" name="{$series}extras[{$rowcounter}]" id="{$series}extras{$rowcounter}" style="width:50px;" value="0" class="extras" readonly /></td>
    </tr>
    DATADATA;
    echo $output;

  8. #8
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You have a space after "<<<DATADATA" before the newline.
    Denny Schlesinger
    web services

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that. Getting this output:
    PHP Code:
    Array ( [companyname] => [deliveryaddress] => [orderdate] => [deliverydate] => [status] => Pending [checked] => No [loadedby] => [sumpallets] => [sumextras] => 118 [makeup] => [grandtotal] => [submit] => Submit Order [100perpallet] => Array ( [1] => 180 [2] => 240 [3] => 360 [4] => 576 [5] => 144 [6] => 288 [7] => 280 [8] => 560 [9] => 360 [10] => 480 [11] => 720 [12] => 1152 ) [100quantity] => Array ( [1] => 185 [2] => 264 [3] => 368 [4] => 657 [5] => 144 [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) [100pallets] => Array ( [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) [100extras] => Array ( [1] => [2] => 24 [3] => [4] => 81 [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) [150perpallet1] => 144 [150quantity1] => [150pallets1] => [150extras1] => [150perpallet2] => 160 [150quantity2] => [150pallets2] => [150extras2] => [150perpallet3] => 240 [150quantity3] => [150pallets3] => [150extras3] => [150perpallet4] => 384 [150quantity4] => [150pallets4] => [150extras4] => [150perpallet5] => 144 [150quantity5] => [150pallets5] => [150extras5] => [150perpallet6] => 144 [150quantity6] => [150pallets6] => [150extras6] => [150perpallet7] => 135 [150quantity7] => [150pallets7] => [150extras7] => [150perpallet8] => 120 [150quantity8] => [150pallets8] => [150extras8] => [150perpallet9] => 240 [150quantity9] => [150pallets9] => [150extras9] => [150perpallet10] => 320 [150quantity10] => [150pallets10] => [150extras10] => [150perpallet11] => 480 [150quantity11] => [150pallets11] => [150extras11] => [150perpallet12] => 768 [150quantity12] => [150pallets12] => [150extras12] => [150perpallet13] => 288 [150quantity13] => [150pallets13] => [150extras13] => [200perpallet1] => 108 [200quantity1] => [200pallets1] => [200extras1] => [200perpallet2] => 120 [200quantity2] => [200pallets2] => [200extras2] => [200perpallet3] => 180 [200quantity3] => [200pallets3] => [200extras3] => [200perpallet4] => 288 [200quantity4] => [200pallets4] => [200extras4] => [200perpallet5] => 90 [200quantity5] => [200pallets5] => [200extras5] => [200perpallet6] => 90 [200quantity6] => [200pallets6] => [200extras6] => [200perpallet7] => 108 [200quantity7] => [200pallets7] => [200extras7] => [200perpallet8] => 108 [200quantity8] => [200pallets8] => [200extras8] => [200perpallet9] => 108 [200quantity9] => [200pallets9] => [200extras9] => [200perpallet10] => 260 [200quantity10] => [200pallets10] => [200extras10] => [200perpallet11] => 288 [200quantity11] => [200pallets11] => [200extras11] => [200perpallet12] => 108 [200quantity12] => [200pallets12] => [200extras12] => [200perpallet13] => 108 [200quantity13] => [200pallets13] => [200extras13] => [200perpallet14] => 120 [200quantity14] => [200pallets14] => [200extras14] => [200perpallet15] => 240 [200quantity15] => [200pallets15] => [200extras15] => [200perpallet16] => 108 [200quantity16] => [200pallets16] => [200extras16] => [200perpallet17] => 180 [200quantity17] => [200pallets17] => [200extras17] => [200perpallet18] => 180 [200quantity18] => [200pallets18] => [200extras18] => [200perpallet19] => 240 [200quantity19] => [200pallets19] => [200extras19] => [200perpallet20] => 360 [200quantity20] => [200pallets20] => [200extras20] => [200perpallet21] => 576 [200quantity21] => [200pallets21] => [200extras21] => [200perpallet22] => 180 [200quantity22] => [200pallets22] => [200extras22] => [200perpallet23] => 360 [200quantity23] => [200pallets23] => [200extras23] => [200perpallet24] => 108 [200quantity24] => [200pallets24] => [200extras24] => [300perpallet1] => 72 [300quantity1] => [300pallets1] => [300extras1] => [300perpallet2] => 96 [300quantity2] => [300pallets2] => [300extras2] => [300perpallet3] => 120 [300quantity3] => [300pallets3] => [300extras3] => [300perpallet4] => 72 [300quantity4] => [300pallets4] => [300extras4] => [300perpallet5] => 72 [300quantity5] => [300pallets5] => [300extras5] => [300perpallet6] => 72 [300quantity6] => [300pallets6] => [300extras6] => [300perpallet7] => 144 [300quantity7] => [300pallets7] => [300extras7] => [300perpallet8] => 192 [300quantity8] => [300pallets8] => [300extras8] => [300perpallet9] => 240 [300quantity9] => [300pallets9] => [300extras9] => [900perpallet1] => 108 [900quantity1] => [900pallets1] => [900extras1] => [900perpallet2] => 180 [900quantity2] => [900pallets2] => [900extras2] => [900perpallet3] => 180 [900quantity3] => [900pallets3] => [900extras3] => 

    So now I'll use the code you sent me:
    PHP Code:
    foreach($_POST['name'] AS $key=>$value) {
        
    $sql "insert into.....";
        
    $result mysql_query($sql) or die("didn't work");

    to insert the data into the tables. So $key is what will be inserted into the MySQL table as values? And this piece of code will insert the array as rows in the mysql table?

    Just trying to understand it, cause I need to insert some data into one table, and some in another.

    thanks

  10. #10
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The array data you posted does not look like what I expected you'd get in the $_POST variable. How did you print it out?

    Did you use
    PHP Code:
    print_r ($_POST); 
    Your print is no help in getting organized.

    The print should look more like this (a form I'm working on). The cell 'name' should be another array, the rest are just simple cells

    Code:
    Array
    (
        [to] => boatyard
        [name] => pipo
        [email] => pipo@home.com
        [subject] => Test
        [comment] => Test
        [telephone] => 123456
        [fax] => 654321
        [yacht_name] => Hot Rod
        [flag] => France
        [hull] => Monohull
        [power] => Motorsailer
        [day] => 21
        [month] => 8
        [year] => 2014
        [units] => Meters
        [length] => 24
        [beam] => 8
        [draft] => 2
        [action] => Revisar
    )
    Denny Schlesinger
    web services

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes I used the following:

    HTML Code:
    <form name="frmorder" action="insert-order.php" method="post">
    for the form post, and the file insert-order.php has the following in it:

    PHP Code:
    <?php
    print_r 
    ($_POST); 
    ?>
    I notice that the print_r statement is printing all the outpost data, even the once not in the <<<DATATA

    Here is my entire page before the <<<DATADATA and the brackets you advised me to use:
    PHP Code:
    <?php
    include('db.php')
    ?>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>HB - Block Order Tracking System</title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <script type="text/javascript">

        function getTotal(srs,idx) { //Calculates the number of pallets needed and the extra blocks left over
            
            var pidx = srs+'perpallet'+idx;
            var qidx = srs+'quantity'+idx;
            var xidx = srs+'pallets'+idx;
            var eidx = srs+'extras'+idx;

            
            if (!document.getElementById(pidx)) { return false; }
            if (!document.getElementById(qidx)) { return false; }
            if (!document.getElementById(xidx)) { return false; }
            if (!document.getElementById(eidx)) { return false; }
            
            var p = document.getElementById(pidx).value;
            var q = document.getElementById(qidx).value;
            
            if ((p == '') || isNaN(p)) { return false; }
            if ((q == '') || isNaN(q)) { return false; }
            
            q = parseInt(q, 10); // parses a string and returns an integer
            
            var total = Math.floor(q / p); //gets total of pallets and rounds down
            var e = Math.abs(total * p - q); //gets extra blocks and converts to possitive integer
            
            total = parseInt(total, 10);
            e = parseInt(e, 10);
            
            document.getElementById(xidx).value = total;
            document.getElementById(eidx).value = e;
            getSumPallets();
            getSumExtras();
        }
        
        function getSumPallets(){

            var items = document.getElementsByClassName("pallets");
            var itemCount = items.length;
            var total = 0;
            for(var i = 0; i < itemCount; i++){
                total = total +  parseInt(items[i].value);
            }
            document.getElementById('sumpallets').value = total;
        }
        function getSumExtras(){

            var items = document.getElementsByClassName("extras");
            var itemCount = items.length;
            var total = 0;
            for(var i = 0; i < itemCount; i++){
                total = total +  parseInt(items[i].value);
            }
            document.getElementById('sumextras').value = total;
        }
        
        function getGrandTotal(){
            var sump = document.getElementById('sumpallets').value;
            var mup = document.getElementById('makeup').value;
            var gtotal = parseInt(sump) + parseInt(mup)
            
            document.getElementById('grandtotal').value =  parseInt(gtotal);
        }
            
    </script>
    </head>
    <body>
    <img class="header-img" src="header-bg.png" alt="header" />
    <div id="wrap">
        <form name="frmorder" action="insert-order.php" method="post">
            <div class="left">
                <table class="styled-table">
                    <tr>
                        <th>Company Name</th><th>Delivery Address</th>
                    </tr>
                    <tr>
                        <td>
                            <?php
                                $query 
    mysql_query("SELECT company_name FROM client_table ORDER BY company_name");
                                echo 
    '<select name="companyname">';
                                echo 
    '<option value="" selected="selected"></option>';
                                while (
    $r mysql_fetch_array($query)) {
                                    echo 
    '<option value="'.$r['company_name'].'">'.$r['company_name']."</option>";
                                }
                                echo 
    "</select>";
                            
    ?>
                        </td>
                        <td><input type="text" name="deliveryaddress" value="" /></td>
                    </tr>
                </table>
            </div>
            <div class="left">
                <table class="styled-table">
                    <tr>
                        <th>Order Date</th><th>Delivery Date</th>
                    </tr>
                    <tr>
                        <td><input type="date" name="orderdate" value="" /></td>
                        <td><input type="date" name="deliverydate" value="" /></td>
                    </tr>
                </table>
            </div>

                    <div class="left">
                <table class="styled-table">
                <tr>
                    <th>Status</th><th>Checked</th><th>Loaded by</th>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="status" value="Pending" checked>Pending<br>
                        <input type="radio" name="status" value="Completed">Completed
                    </td>
                    <td>
                        <input type="radio" name="checked" value="Yes">Yes
                        <input type="radio" name="checked" value="No" checked>No
                    </td>
                    <td>
                        <input type="text" name="loadedby" value="" />
                    </td>
                </tr>
                </table>
            </div>
            <div class="left clear">
                <table class="styled-table">
                <tr>
                    <th>Pallets</th><td><input type="text" name="sumpallets" id="sumpallets" value="" readonly /></td>    
                </tr>
                <tr>
                    <th>Extras</th><td><input type="text" name="sumextras" id="sumextras" value="" readonly /></td>    
                </tr>
                <tr>
                    <th>Makeup</th><td><input type="text" name="makeup" id="makeup" value="" onChange="getGrandTotal();" /></td>    
                </tr>
                <tr>
                    <th>Total Pallets</th><td><input type="text" name="grandtotal" id="grandtotal" value="" readonly /></td>    
                </tr>
                </table>
            </div>
            <div class="left clear" style="margin-top: 15px;">
                <table>
                    <tr>
                        <td><input type="submit" name="submit" value="Submit Order" /></td>
                        <td><input type="button" value="Reset Form" onClick="this.form.reset()" /></td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align:center;"><input type="button" name="back" value="Back" onClick="location.href='index.html'" /></td>
                    </tr>
                </table>
            </div>
            
            <div class="left clear">
            <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='100 Series'"// 100 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 100;
                
    $rowcounter 0
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
            </div>
            <div class="left">
            <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='150 Series'"// 150 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 150;
                
    $rowcounter 0;
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>150 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
            </div>    
            <div class="left">
            <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='200 Series'"// 200 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 200;
                
    $rowcounter 0;
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>200 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly  /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
            </div>    
            <div class="left">
            <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='300 Series'"// 300 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 300;
                
    $rowcounter 0;
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>300 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
            </div>
            <div class="left">
            <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='Split Face'"// SPLIT FACE SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 900//used a int instead of string eg. splitface , cause of the function parameter
                
    $rowcounter 0;
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>Split Face</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly/></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0"  class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
            </div>

    </form>
    </div>
    </body>
    </html>

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getting errors when using

    PHP Code:
    foreach($_POST['name'] AS $key=>$value) {
        
    $sql "INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
        VALUES
        ('
    $_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[grandtotal]')";
        
    $result mysql_query($sql) or die("didn't work");
    }
    ?> 
    Notice: Undefined index: name in C:\xampp\htdocs\hb_bots\bots\insert-order.php on line 8

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\hb_bots\bots\insert-order.php on line 8

  13. #13
    SitePoint Enthusiast rajeev13's Avatar
    Join Date
    Nov 2012
    Location
    New Delhi,India
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ROUBOS View Post
    getting errors when using

    PHP Code:
    foreach($_POST['name'] AS $key=>$value) {
        
    $sql "INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
        VALUES
        ('
    $_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[grandtotal]')";
        
    $result mysql_query($sql) or die("didn't work");
    }
    ?> 
    Notice: Undefined index: name in C:\xampp\htdocs\hb_bots\bots\insert-order.php on line 8

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\hb_bots\bots\insert-order.php on line 8

    sorry but i dont see any input type whose name is "name" in your page.
    there is no array "name".
    like u have $_POST['companyname'],'$_POST[deliveryaddress]','$_POST[orderdate] and others there is not having any $_POST['name'].

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Confused now
    is there another way to get data from this:
    PHP Code:
    <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='150 Series'"// 150 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 150;
                
    $rowcounter 0;
                echo 
    '<table class="styled-table">';
                echo 
    '<tr><th>150 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>';
                while(
    $row mysql_fetch_array($result)){
                    
    $rowcounter++;
                    echo 
    '<tr><td>'.$row['type'].'</td>
                    <td><input type="text" name="'
    .$series.'perpallet'.$rowcounter.'" id="'.$series.'perpallet'.$rowcounter.'" style="width:50px;" value="'.$row['per_pallet'].'" readonly /></td>
                    <td><input type="text" name="'
    .$series.'quantity'.$rowcounter.'" id="'.$series.'quantity'.$rowcounter.'" style="width:50px;" value="0" onChange="getTotal('.$series.','.$rowcounter.');" /></td>
                    <td><input type="text" name="'
    .$series.'pallets'.$rowcounter.'" id="'.$series.'pallets'.$rowcounter.'" style="width:50px;" value="0" class="pallets" readonly /></td>
                    <td><input type="text" name="'
    .$series.'extras'.$rowcounter.'" id="'.$series.'extras'.$rowcounter.'" style="width:50px;" value="0" class="extras" readonly /></td>
                    </tr>'
    ;
                }
                echo 
    '</table>';
            
    ?>
    and insert it into a table?

  15. #15
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK now with this php:
    PHP Code:
    <?php
                $sql 
    "SELECT * FROM blocks_table WHERE series='100 Series'"// 100 SERIES --------------------------------------------------------------------
                
    $result mysql_query($sql)or die(mysql_error());
                
    $series 100;
                
    $rowcounter 0;  
    echo 
    '<table class="styled-table">'
    echo 
    '<tr><th>100 Series</th><th>Per Pallet</th><th>Quantity</th><th>Pallets</th><th>Extras</th></tr>'
    while(
    $row mysql_fetch_array($result)){ 
        
    $rowcounter++; 
        
    $output = <<<DATADATA
        <tr><td>{$row["type"]}</td></td>
        <td><input type="text" name="
    {$series}perpallet[{$rowcounter}]" id="{$series}perpallet{$rowcounter}" style="width:50px;" value="{$row['per_pallet']}" readonly /></td> 
        <td><input type="text" name="
    {$series}quantity[{$rowcounter}]" id="{$series}quantity{$rowcounter}" style="width:50px;" value="0" onChange="getTotal({$series},{$rowcounter});" /></td> 
        <td><input type="text" name="
    {$series}pallets[{$rowcounter}]" id="{$series}pallets{$rowcounter}" style="width:50px;" value="0" class="pallets" readonly /></td> 
        <td><input type="text" name="
    {$series}extras[{$rowcounter}]" id="{$series}extras{$rowcounter}" style="width:50px;" value="0" class="extras" readonly /></td> 
        </tr>
    DATADATA;
        echo 
    $output;

    echo 
    '</table>';
            
    ?>
    and the form calling another file which has the following:
    PHP Code:
    <?php
    print_r 
    ($_POST); 
    ?> 


    <?php

    foreach($_POST AS $key=>$value) {
        
    $sql "INSERT INTO order_table (company_name, delivery_address, order_date, delivery_date, status, hb_checked, loaded_by, pallets, extra_blocks, extra_pallets, total_pallets)
        VALUES
        ('
    $_POST[companyname]','$_POST[deliveryaddress]','$_POST[orderdate]','$_POST[deliverydate]','$_POST[status]','$_POST[checked]','$_POST[loadedby]','$_POST[sumpallets]','$_POST[sumextras]','$_POST[grandtotal]')";
        
    $result mysql_query($sql) or die("didn't work");
    }
    ?>
    I get the following. It does not insert the data:
    PHP Code:
    Array ( [companyname] => NACA [deliveryaddress] => Stuart Park [orderdate] => 2013-04-18 [deliverydate] => 2013-05-16 [status] => Completed [checked] => Yes [loadedby] => MANOLIS [sumpallets] => [sumextras] => 624 [makeup] => [grandtotal] => [submit] => Submit Order [100perpallet] => Array ( [1] => 180 [2] => 240 [3] => 360 [4] => 576 [5] => 144 [6] => 288 [7] => 280 [8] => 560 [9] => 360 [10] => 480 [11] => 720 [12] => 1152 ) [100quantity] => Array ( [1] => 654 [2] => 154 [3] => 356 [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) [100pallets] => Array ( [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) [100extras] => Array ( [1] => 114 [2] => 154 [3] => 356 [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) ) didn't work 

  16. #16
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    The array data you posted does not look like what I expected you'd get in the $_POST variable. How did you print it out?

    Did you use
    PHP Code:
    print_r ($_POST); 
    Your print is no help in getting organized.

    The print should look more like this (a form I'm working on). The cell 'name' should be another array, the rest are just simple cells

    Code:
    Array
    (
        [to] => boatyard
        [name] => pipo
        [email] => pipo@home.com
        [subject] => Test
        [comment] => Test
        [telephone] => 123456
        [fax] => 654321
        [yacht_name] => Hot Rod
        [flag] => France
        [hull] => Monohull
        [power] => Motorsailer
        [day] => 21
        [month] => 8
        [year] => 2014
        [units] => Meters
        [length] => 24
        [beam] => 8
        [draft] => 2
        [action] => Revisar
    )
    with this:
    PHP Code:
    echo '<pre style="text-align: left;">' print_r($_POSTtrue) . '</pre>'
    I get:
    PHP Code:
    Array
    (
        [
    companyname] => InfoShell
        
    [deliveryaddress] => Kavlia street
        
    [orderdate] => 2013-04-18
        
    [deliverydate] => 2013-05-23
        
    [status] => Pending
        
    [checked] => No
        
    [loadedby] => Kavlifei
        
    [sumpallets] => 3
        
    [sumextras] => 187
        
    [makeup] => 1
        
    [grandtotal] => 4
        
    [submit] => Submit Order
        
    [100perpallet] => Array
            (
                [
    1] => 180
                
    [2] => 240
                
    [3] => 360
                
    [4] => 576
                
    [5] => 144
                
    [6] => 288
                
    [7] => 280
                
    [8] => 560
                
    [9] => 360
                
    [10] => 480
                
    [11] => 720
                
    [12] => 1152
            
    )

        [
    100quantity] => Array
            (
                [
    1] => 354
                
    [2] => 245
                
    [3] => 368
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

        [
    100pallets] => Array
            (
                [
    1] => 1
                
    [2] => 1
                
    [3] => 1
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

        [
    100extras] => Array
            (
                [
    1] => 174
                
    [2] => 5
                
    [3] => 8
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

    )
    didn't work 
    So now trying to insert values to the table with no luck.
    PHP Code:
    foreach($_POST[] AS $key=>$value) {
        
    $sql="INSERT INTO order_table ('company_name') VALUES ('$_POST[companyname]')";
        
    $result mysql_query($sql) or die("didn't work");

    this is not working. Not sure what to put in foreach($_POST[" in here"]

  17. #17
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok say I do this:
    PHP Code:
    foreach($_POST['100quantity'] AS $key=>$value) {
        
    $sql="INSERT INTO order_details_table ('block_quantity') VALUES ('$_POST[blockquantity]')";
        
    $result mysql_query($sql) or die("didn't work");

    the value to insert into the table does not have the name blockquantity in the print_r($_POST). So how do I do it? This value is what is in the form.

  18. #18
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can insert the values for these:
    PHP Code:
    Array
    (
        [
    companyname] => InfoShell
        
    [deliveryaddress] => safasfasf
        
    [orderdate] => 2013-04-08
        
    [deliverydate] => 2013-04-23
        
    [status] => Pending
        
    [checked] => No
        
    [loadedby] => afsfsfsf
        
    [sumpallets] => 2
        
    [sumextras] => 351
        
    [makeup] => 2
        
    [grandtotal] => 4
        
    [submit] => Submit Order 
    The problem I have is with looping and inserting the following in a different table:
    PHP Code:
    [100type] => Array
            (
                [
    1] => 10.01
                
    [2] => 10.02
                
    [3] => 10.03
                
    [4] => 10.04
                
    [5] => 10.31
                
    [6] => 10.83
                
    [7] => 10.702
                
    [8] => 10.772
                
    [9] => 10.71
                
    [10] => 10.72
                
    [11] => 10.73
                
    [12] => 10.74
            
    )

        [
    100perpallet] => Array
            (
                [
    1] => 180
                
    [2] => 240
                
    [3] => 360
                
    [4] => 576
                
    [5] => 144
                
    [6] => 288
                
    [7] => 280
                
    [8] => 560
                
    [9] => 360
                
    [10] => 480
                
    [11] => 720
                
    [12] => 1152
            
    )

        [
    100quantity] => Array
            (
                [
    1] => 234
                
    [2] => 324
                
    [3] => 213
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

        [
    100pallets] => Array
            (
                [
    1] => 1
                
    [2] => 1
                
    [3] => 0
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            
    )

        [
    100extras] => Array
            (
                [
    1] => 54
                
    [2] => 84
                
    [3] => 213
                
    [4] => 0
                
    [5] => 0
                
    [6] => 0
                
    [7] => 0
                
    [8] => 0
                
    [9] => 0
                
    [10] => 0
                
    [11] => 0
                
    [12] => 0
            


  19. #19
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    To insert the values you would need something like this. It takes the values for the first product, then the second one, and so on until it loops through the 12 products
    PHP Code:
    <?php
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO table
               (field1, field2, field3, field4, field5)
               VALUES ('
    {$value}',
                       '
    {$_POST['100perpallet'][$key]}',
                       '
    {$_POST['100quantity'][$key]}',
                       '
    {$_POST['100pallets'][$key]}',
                       '
    {$_POST['100extras'][$key]}'           
                      )"
        
    $result mysl_query($sql) or die('Could not insert data');
    }
    ?>
    Denny Schlesinger
    web services

  20. #20
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    must be doing something wrong.. keep getting "Could not insert data".

    this is what I've tried:
    PHP Code:
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (block_type,block_quantity)
                          VALUES ('
    {$value}',
                       '
    {$_POST['100type'][$key]}',
                       '
    {$_POST['100quantity'][$key]}'          
                      )"
    ;
        
    $result mysql_query($sql) or die('Could not insert data');
    }

    ?> 
    the table I need to enter values in has 4 fields
    id
    order_id
    block_type
    block_quantity

    Do I need to insert a value for the id and order_id? and insert NULL?

  21. #21
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    That query define 2 columns but 3 values. It looks like the order_id needs to be added as the first column if that is the proper value for $value. Furthermore, I don't think you need to quote the order_id value because it is *probably an integer data type column. I say that because it sounds like a foreign key to what would be an auto increment primary key. Though I could be wrong. So just pointing that out.

    PHP Code:
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (order_id,block_type,block_quantity)
                          VALUES (
    {$value},
                       '
    {$_POST['100type'][$key]}',
                       '
    {$_POST['100quantity'][$key]}'          
                      )"
    ;
        
    $result mysql_query($sql) or die('Could not insert data');
    }

    ?> 
    Also, please look into preventing SQL injection. Right now this query is susceptible to a SQL injection attack. Ideally variable binding and prepared statements should be used to prevent SQL injection. Though the standard MySQL adapter does not support that easily. If this is a single isolated script I would highly recommend looking into an adaptor that provides an interface to easily use prepared statements with variable binding. Otherwise, the values can be escaped using mysql_real_escape_string. Though using mysql_real_escape_string is not the preferred nor most absolute method to prevent SQL injection. Again, if this is a single isolated script than consider using mysqli or pdo instead of the standard mysql functions. Otherwise, look into updating the antire application to use a more modern adaptor that supports prepared statements with variable binding.
    The only code I hate more than my own is everyone else's.

  22. #22
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still cannot insert data

    order_id is a foreign key. below is what i tried with all columns.
    id
    order_id - FK
    block_type
    block_quantity
    block_pallets
    block_extras

    PHP Code:
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (id,order_id,block_type,block_quantity,block_pallets,block_extras)
                          VALUES (null,
    {$value},
                        '
    {$_POST['100type'][$key]}',
                        '
    {$_POST['100quantity'][$key]}',
                '
    {$_POST['100pallets'][$key]}',
                '
    {$_POST['100extras'][$key]}'                     
                      )"
    ;
        
    $result mysql_query($sql) or die('Could not insert data');

    thanks for the advice on security. I should look into it.

  23. #23
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    What I would recommend than is to print the SQL statement and run it directly in MySQL though a GUI client or the terminal.

    PHP Code:
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (id,order_id,block_type,block_quantity,block_pallets,block_extras)
                          VALUES (null,
    {$value},
                        '
    {$_POST['100type'][$key]}',
                        '
    {$_POST['100quantity'][$key]}',
                '
    {$_POST['100pallets'][$key]}',
                '
    {$_POST['100extras'][$key]}'                     
                      )"
    ;

    echo 
    "<p>$sql</p>"// copy and paste this query into your MySQL client of choice and use the error to figure out what needs to be done

        
    $result mysql_query($sql) or die('Could not insert data');

    The only code I hate more than my own is everyone else's.

  24. #24
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    What I would recommend than is to print the SQL statement and run it directly in MySQL though a GUI client or the terminal.

    PHP Code:
    foreach($_POST['100type'] AS $key=>$value) {
        
    $sql "INSERT INTO order_details_table
               (id,order_id,block_type,block_quantity,block_pallets,block_extras)
                          VALUES (null,
    {$value},
                        '
    {$_POST['100type'][$key]}',
                        '
    {$_POST['100quantity'][$key]}',
                '
    {$_POST['100pallets'][$key]}',
                '
    {$_POST['100extras'][$key]}'                     
                      )"
    ;

    echo 
    "<p>$sql</p>"// copy and paste this query into your MySQL client of choice and use the error to figure out what needs to be done

        
    $result mysql_query($sql) or die('Could not insert data');

    When a query errors or is not functioning in the way intended the best thing to do is print it out and run it directly in MySQL. That will provide a good indication as to the whether the query is malformed or the issue is application side. In some cases just printing it might reveal details like missing values that are expected to exist yet don't.
    The only code I hate more than my own is everyone else's.

  25. #25
    SitePoint Enthusiast
    Join Date
    Apr 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the foreign key is causing it
    #1452 - Cannot add or update a child row: a foreign key constraint fails (`bots`.`order_details_table`, CONSTRAINT `order_details_table_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `order_table` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)


    how do I copy the id from the first table to the order_id in the second table? that's the foreign key that needs to be populated. They are all in one form.
    some data are inputed in one table and some in the other


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •