Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2011
    Posts
    95
    Thanks
    13
    Thanked 1 Time in 1 Post

    how to insert multiple rows into mysql

    Atm i have almost complete the insert page just 2 things left and need some help.


    my insert page looks like

    PHP Code:
    <?php
    session_start
    ();
    $productid=$_SESSION['productid'] ;
    $pricetotal=$_SESSION['pricetotal'] ;
    $price=$_SESSION['price'] ;
    $cartTotal=$_SESSION['cartTotal'] ;
    $quantity=$_SESSION['quantity'] ;
    $MM_Username $_SESSION['MM_Username'];
    $quantity=$_SESSION['quantity'];
     
     
    $con mysql_connect("localhost","root","");
     if (!
    $con)
       {
       die(
    'Could not connect: ' mysql_error());
       }
     
    mysql_select_db("mypc"$con);
     
    $sql1="INSERT INTO sales(address, paymethod, creditcard,cardtype,username,cartTotal,phone) VALUES
    ('$_POST[address]','$_POST[RadioGroup1]','$_POST[pass]','$_POST[type]','$MM_Username','$cartTotal','$_POST[phone]')"
    ;
    if (!
    mysql_query($sql1,$con))
       {
       die(
    'Error: ' mysql_error());
       } 
       
    $sql2="INSERT INTO orders(unitprice,pricetotal,quantity,productid ) VALUES
    ('$price','$pricetotal','$quantity','$productid')"
    ;
    if (!
    mysql_query($sql2,$con))
       {
       die(
    'Error: ' mysql_error());
       } 
       
       
    mysql_close($con)
     
    ?>

    1--> Atm sql2 insert only the last product's id at the table.
    I need to insert all products chosen by the user.
    If for example made 10 selections then i insert all 10 rows into mysql

    2--> I want to display a message to him like "the processing was successfull" but in the same page. Atm when i insert the products i see a blank page or a message which i removed.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Use a loop.

  • #3
    New Coder
    Join Date
    Apr 2011
    Posts
    95
    Thanks
    13
    Thanked 1 Time in 1 Post
    I tried this but i get an error on forech... any suggestions?

    PHP Code:
    foreach ($_SESSION['productid'] as $value)
       {
     echo  
    $sql2="INSERT INTO orders(unitprice,pricetotal,quantity,productid ) VALUES
    ('$price','$pricetotal','$quantity','$value')"
    ;
    if (!
    mysql_query($sql2,$con))
       {
       die(
    'Error: ' mysql_error());
       } 
       } 

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    95
    Thanks
    13
    Thanked 1 Time in 1 Post
    Ok i have managed to insert into the table equal products like products the user chose, but though my data is not all correct i working on it know if u have any suggestions tnx!!

    PHP Code:
     foreach ($_SESSION['cart_array'] as $value)   {
    $sql2="INSERT INTO orders(unitprice,pricetotal,quantity,productid,username ) VALUES
    ('$price','$pricetotal','$quantity','$productid','$MM_Username')"
    ;
    if (!
    mysql_query($sql2,$con))
       {
       die(
    'Error: ' mysql_error());
       } 
       } 

    And my cart

    PHP Code:
    $cartOutput="";
    $cartTotal="";
    if(!isset(
    $_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
        
    $cartOutput="<h2 align='center'>your shopping cart is empty</h2>";
    }else{
        
    $i=0;
        foreach(
    $_SESSION["cart_array"]as $each_item){
            
    $item_id=$each_item['item_id'];
            
    $sql=mysql_query("SELECT productid,productname,price,image FROM products WHERE productid='$item_id' LIMIT 1");
            while(
    $rowmysql_fetch_array($sql)){
                
    $productname=$row['productname'];
                
    $price=$row['price'];
                
    $image=$row['image'];
                
    $productid=$row['productid'];
            }
            
    $pricetotal=number_format($price*$each_item['quantity'],2,".","");
            
    $cartTotal=number_format($pricetotal+$cartTotal,2,".","");
            
            
    $cartOutput.="<tr>";
            
    $cartOutput.='<td><img src="images/site pic 2/mysql/'.$image.'" width="45" height="45" /></td>';
            
    $cartOutput.='<td>'$productid.'</td>';
            
    $cartOutput.='<td>'$productname.'</td>';
            
    $cartOutput.='<td>$'$price.'</td>';
            
    $cartOutput.='<td><form action="mycart.php" method="post">
            <input name="quantity" type="text" value="'
    $each_item['quantity'].'" size="1" maxlength="2" />
            <input name="adjustBtn'
    .$item_id.'" type="submit" value="change" />
            <input name="item_to_adjust" type="hidden" value="'
    .$item_id.'" />
            </form></td>'
    ;
            
    //$cartOutput.='<td>'. $each_item['quantity'].'</td>';
            
    $cartOutput.='<td>$'$pricetotal.'</td>';
            
    $cartOutput.='<td><form action="mycart.php" method="post">
            <input name="deleteBtn'
    .$item_id.'" type="submit" value="X" />
            <input name="index_to_remove" type="hidden" value="'
    .$i.'" />
            </form>
            </td>'
    ;
            
    $cartOutput.='</tr>';
            
    $i++; 


  •  

    Posting Permissions

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