Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

How insert multiple rows in mysql table with php array?

Codes here !

Moderators: macek, egami, gesf

How insert multiple rows in mysql table with php array?

Postby jchoudja » Sat Mar 22, 2014 6:07 am

I have an order form with will contain about 15 rows of same type of data on my exemple, I included only 4 rows. I want to use php to insert all rows in 1 table at once. All rows on the form are available to be filled up but not all should be filled. Problem I am getting is: If the first row only is filled the query will insert 1 row in the table. If all rows are filled, query will insert 4 rows in the table. Problem came when a row is skipped. Example Row 1 is filled row2 is empty row3 is filled row4 is filled. all fields on the first row will be inserted in the table but it will insert only first field ('name') of row3 and row4 event if the remaining field are not empty. Also, if the first row is empty, only first fields on the filled row be saved.

here are my codes

Entry form (custompc_form2.php)
Code: Select all
<?php
    include '../subs/pcpartsdrop.php';
    connect();

?>

<!DOCTYPE html>
<html>
<head></head>
<body>

<form action="../subs/custompcorder2.php/" method="post" id="form">

<div id="orderwrap">
    <div id="orderheather">
    <select id="platform" name="platform">
        <option selected="selected" disabled="disabled">Select the platform</option>
        <option value="Intel">Intel</option>
        <option value="AMD">AMD</option>
    </select>
    </div>

    <div id="orderbody">

        <p>name<select id="name" name="name[]">
                    <option selected="selected" disabled="disabled">Choose hard drive</option>
            <?php  query() ?>
            </select>
                    quantity1: <input type="text" id="quantity" name="quantity[]"/> price1: <input id="name-data" type="text" name="price[]"/></p>

                    <p>name<select id="name" name="name[]">
                    <option selected="selected" disabled="disabled">Choose memory</option>
            <?php  query2() ?>
            </select>
                    quantity: <input type="text" id="quantity" name="quantity[]"/> price1: <input id="name-data" type="text" name="price[]"/></p>

                    <p>name<select id="name" name="name[]">
                    <option selected="selected" disabled="disabled">Choose monitor</option>
            <?php  query3() ?>
            </select>
                    quantity: <input type="text" id="quantity" name="quantity[]"/> price1: <input id="name-data" type="text" name="price[]"/></p>

        <p>name<select id="name" name="name[]">
                    <option selected="selected" disabled="disabled">Choose mouse</option>
            <?php  query4() ?>
            </select>
                    quantity: <input type="text" id="quantity" name="quantity[]"/> price1: <input id="name-data" type="text" name="price[]"/></p>

        <input id="submit" type="submit" value="Submit Order" name="submission"/>
    </div>
</div> 
</form>


Insertion form (custompcorder2.php)
I want this query to go thru all rows in the entry form and collect all data in each row if if has data but if it doesn't, move to the next row until the end.

Code: Select all
<?php
include '../db/connect.php';

$row_data = array();
foreach($_POST['name'] as $row=>$name) {
        $name=mysqli_real_escape_string($con,$name);
        $quty=mysqli_real_escape_string($con,($_POST['quantity'][$row]));
        $price=mysqli_real_escape_string($con,($_POST['price'][$row]));



    $row_data[] = "('$name', '$quty', '$price')";
}
if (!empty($row_data)) {
    $sql = 'INSERT INTO oz2ts_custompc_details(part_id, quantity, price) VALUES '.implode(',', $row_data);
    $result = mysqli_query($con, $sql ) or die(mysqli_error($con));

    if ($result)
    echo 'Successful inserts: ' . mysqli_affected_rows($con);
    else
    echo 'query failed' ;
}

?>


Functions feeding the drop-down list on the entry form (pcpartsdrop.php)
Code: Select all
<?php
require_once '../db/connect1.php';


function connect(){
    mysql_connect(DB_HOST,DB_USER,DB_PASS) or die('Could not connect to database ' .mysql_error());
            mysql_select_db(DB_NAME);
}

function close(){
    mysql_close();
    }

function query(){
$myData=mysql_query("SELECT * FROM oz2ts_mijoshop_product");
while($record=mysql_fetch_array($myData)){
    echo'<option value="'.$record['product_id'].'">'.$record['model'].'</option>';
    }
}

function query2(){
$myData=mysql_query("SELECT * FROM oz2ts_mijoshop_product");
while($record=mysql_fetch_array($myData)){
    echo'<option value="'.$record['product_id'].'">'.$record['model'].'</option>';
    }

   }
jchoudja
New php-forum User
New php-forum User
 
Posts: 2
Joined: Wed Mar 05, 2014 7:11 am

Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.