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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Inserting into multiple column using foreach loop using PHP

    dear, i am trying to insert innto mutiple fields of a mysql table using php foreach loop.
    please help me as i can update data into all field except one ‘weight’, code is below:

    insert portion:
    PHP Code:
    if(isset($_POST[‘ordersubmit’]))
    {

    if(
    strlen($_SESSION[‘login’])==0)
    {
    header(‘location:login.php’);
    }
    else{

    $quantity=$_POST['quantity'];
    $pdd=$_SESSION['pid'];
    $value=array_combine($pdd,$quantity);



        foreach(
    $value as $qty=> $val34){

    mysql_query(“insert into orders(userId,productId,quantity,weightvalues(’”.$_SESSION[‘id’]."’,’$qty’,’$val34’,’".$_SESSION[‘id’][‘weight’]."’)");

    header(‘location:payment-method_cake.php’);

    }
    }

    The Whole page Code:
    PHP Code:
    <?php session_start(); error_reporting(0); include('includes/config.php'); if(isset($_POST['submit'])){ if(!empty($_SESSION['cart'])){ foreach($_POST['quantity'] as $key => $val){ if($val==0){ unset($_SESSION['cart'][$key]); }else{ $_SESSION['cart'][$key]['quantity']=$val; } } echo ""; } { foreach($_POST['weight'] as $key2 => $val2){ if($val2==0){ unset($_SESSION['cart'][$key2]); }else{ $_SESSION['cart'][$key2]['weight']=$val2; } } echo ""; } } // Code for Remove a Product from Cart if(isset($_POST['remove_code'])) { if(!empty($_SESSION['cart'])){ foreach($_POST['remove_code'] as $key){ unset($_SESSION['cart'][$key]); } echo ""; } } // code for insert product in order table if(isset($_POST['ordersubmit'])) { if(strlen($_SESSION['login'])==0) { header('location:login.php'); } else{ $quantity=$_POST['quantity']; $pdd=$_SESSION['pid']; $value=array_combine($pdd,$quantity); foreach($value as $qty=> $val34){ mysql_query("insert into orders(userId,productId,quantity,weight) values('".$_SESSION['id']."','$qty','$val34','".$_SESSION['id']['weight']."')"); header('location:payment-method_cake.php'); } } } ?>

        <title>Project</title>
        <link rel="stylesheet" href="assets/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/css/main.css">
        <link rel="stylesheet" href="assets/css/owl.carousel.css">
        <link rel="stylesheet" href="assets/css/owl.transitions.css">
        <!--<link rel="stylesheet" href="assets/css/owl.theme.css">-->
        <link href="assets/css/lightbox.css" rel="stylesheet">
        <link rel="stylesheet" href="assets/css/animate.min.css">
        <link rel="stylesheet" href="assets/css/rateit.css">
        <link rel="stylesheet" href="assets/css/bootstrap-select.min.css">

        <!-- Demo Purpose Only. Should be removed in production -->
        <link rel="stylesheet" href="assets/css/config.css">

        <link href="assets/css/green.css" rel="alternate stylesheet" title="Green color">
        <link href="assets/css/blue.css" rel="alternate stylesheet" title="Blue color">
        <link href="assets/css/red.css" rel="alternate stylesheet" title="Red color">
        <link href="assets/css/orange.css" rel="alternate stylesheet" title="Orange color">
        <link href="assets/css/dark-green.css" rel="alternate stylesheet" title="Darkgreen color">
        <!-- Demo Purpose Only. Should be removed in production : END -->

        
        <!-- Icons/Glyphs -->
        <link rel="stylesheet" href="assets/css/font-awesome.min.css">

        <!-- Fonts --> 
        <link href='http://fonts.googleapis.com/css?family=Roboto:300,400,500,700' rel='stylesheet' type='text/css'>
        
        <!-- Favicon -->
        <link rel="shortcut icon" href="assets/images/favicon.ico">

        <!-- HTML5 elements and media queries Support for IE8 : HTML5 shim and Respond.js -->
        <!--[if lt IE 9]>
            <script src="assets/js/html5shiv.js"></script>
            <script src="assets/js/respond.min.js"></script>
        <![endif]-->

    </head>
    <body class="cnt-home">

        

        <!-- ============================================== HEADER ============================================== -->

    <?php include('includes/top-header.php');?> <?php include('includes/main-header_cake.php');?> <?php include('includes/menu-bar.php');?>

        Home
        Shopping Cart

    <?php if(!empty($_SESSION['cart'])){ ?>

                    <th class="cart-qty item">Quantity</th>
                    <th class="cart-qty item">Pounds</th>
                    <th class="cart-sub-total item">Price Per unit</th>
                    <th class="cart-total last-item">Grandtotal</th>
                </tr>
            </thead><!-- /thead -->
            <tfoot>
                <tr>
                    <td colspan="7">
                        <div class="shopping-cart-btn">
                            <span class="">
                                <a href="index.php" class="btn btn-upper btn-primary outer-left-xs">Continue Shopping</a>
                                <input type="submit" name="submit" value="Update shopping cart" class="btn btn-upper btn-primary pull-right outer-right-xs">
                            </span>
                        </div><!-- /.shopping-cart-btn -->
                    </td>
                </tr>
            </tfoot>
            <tbody>

    <?php $pdtid=array(); $sql "SELECT * FROM products WHERE id IN("; foreach($_SESSION['cart'] as $id => $value){ $sql .=$id","; } $sql=substr($sql,0,-1) . ") ORDER BY id ASC"$query mysql_query($sql); $totalprice=0$totalqunty=0; if(!empty($query)){ while($row mysql_fetch_array($query)){ $quantity=$_SESSION['cart'][$row['id']]['quantity']; $weight=$_SESSION['cart'][$row['id']]['weight']; $subtotal$_SESSION['cart'][$row['id']]['quantity']*$_SESSION['cart'][$row['id']]['weight']*$row['productPrice']; $totalprice += $subtotal$_SESSION['qnty']=$totalqunty+=$quantityarray_push($pdtid,$row['id']); //print_r($_SESSION['pid'])=$pdtid;exit; ?>

                <tr>
                    <td class="romove-item"><input type="checkbox" name="remove_code[]" value="<?php echo htmlentities($row['id']);?>" /></td>
                    <td class="cart-image">
                        <a class="entry-thumbnail" href="detail.html">
                            <img src="admin/productimages/<?php echo $row['productName'];?>/<?php echo $row['productImage1'];?>" alt="" width="114" height="146">
                        </a>
                    </td>
                    <td class="cart-product-name-info">
                        <h4 class='cart-product-description'><a href="product-details.php?pid=<?php echo htmlentities($pd=$row['id']);?>" ><?php echo $row['productName'];

    $_SESSION[‘sid’]=$pd;
    ?>




    <?php $rt=mysql_query("select * from productreviews where productId='$pd'"); $num=mysql_num_rows($rt); { ?>

                                <div class="reviews">
                                    ( <?php echo htmlentities($num);?> Reviews )
                                </div>
                                <?php ?>
                            </div>
                        </div><!-- /.row -->
                        
                    </td>
                    <td class="cart-product-quantity">
                        <div class="quant-input">
                                <div class="arrows">
                                  <div class="arrow plus gradient"><span class="ir"><i class="icon fa fa-sort-asc"></i></span></div>
                                  <div class="arrow minus gradient"><span class="ir"><i class="icon fa fa-sort-desc"></i></span></div>
                                </div>
                             <input type="text" value="<?php echo $_SESSION['cart'][$row['id']]['quantity']; ?>" name="quantity[<?php echo $row['id']; ?>]">
                             
                          </div>
                    </td>

                    <td class="cart-product-quantity">
                        <div class="quant-input">
                                <div class="arrows">
                                  <div class="arrow plus gradient"><span class="ir"><i class="icon fa fa-sort-asc"></i></span></div>
                                  <div class="arrow minus gradient"><span class="ir"><i class="icon fa fa-sort-desc"></i></span></div>
                                </div>
                             <input type="text" value="<?php echo $_SESSION['cart'][$row['id']]['weight']; ?>" name="weight[<?php echo $row['id']; ?>]">
                             
                          </div>


                    </td>
                    <td class="cart-product-sub-total"><span class="cart-sub-total-price"><?php echo "Rs"." ".$row['productPrice']; ?>.00</span></td>
                    <td class="cart-product-grand-total"><span class="cart-grand-total-price"><?php echo ($_SESSION['cart'][$row['id']]['quantity']*$_SESSION['cart'][$row['id']]['weight']*$row['productPrice']); ?>.00</span></td>
                </tr>

                

                <?php } }

    $_SESSION[‘pid’]=$pdtid;
    ?>

            </tbody><!-- /tbody -->
        </table><!-- /table -->
        
    </div>

    Remove     Image     Product Name
    Shipping Address
    <?php $qry=mysql_query("select * from users where id='".$_SESSION['id']."'"); while ($rt=mysql_fetch_array($qry)) { echo htmlentities($rt['shippingAddress'])."
    "
    ; echo htmlentities($rt['shippingCity'])."
    "
    ; echo htmlentities($rt['shippingState']); echo htmlentities($rt['shippingPincode']); }

                        
    ?>
        
                        </div>
                    
                    </td>
                </tr>
        </tbody><!-- /tbody -->
    </table><!-- /table -->

    Billing Address
    <?php $qry=mysql_query("select * from users where id='".$_SESSION['id']."'"); while ($rt=mysql_fetch_array($qry)) { echo htmlentities($rt['billingAddress'])."
    "
    ; echo htmlentities($rt['billingCity'])."
    "
    ; echo htmlentities($rt['billingState']); echo htmlentities($rt['billingPincode']); }

                        
    ?>
        
                        </div>
                    
                    </td>
                </tr>
        </tbody><!-- /tbody -->
    </table><!-- /table -->

                    <div class="cart-grand-total">
                        Grand Total<span class="inner-left-md"><?php echo $_SESSION['tp']="$totalprice"".00"?></span>
                    </div>
                </th>
            </tr>
        </thead><!-- /thead -->
        <tbody>
                <tr>
                    <td>
                        <div class="cart-checkout-btn pull-right">
                    
                            
                            <button type="submit" name="ordersubmit" class="btn btn-primary">PROCCED TO CHEKOUT</button>
                        
                        </div>
                    </td>
                </tr>
        </tbody><!-- /tbody -->
    </table>
    <?php } else {

    echo 
    “Your shopping Cart is empty”;
    }
    ?>
    <?php 
    echo include('includes/brands-slider.php');?> <?php include('includes/footer.php');?>

    <script src="assets/js/jquery-1.11.1.min.js"></script>

    <script src="assets/js/bootstrap.min.js"></script>

    <script src="assets/js/bootstrap-hover-dropdown.min.js"></script>
    <script src="assets/js/owl.carousel.min.js"></script>

    <script src="assets/js/echo.min.js"></script>
    <script src="assets/js/jquery.easing-1.3.min.js"></script>
    <script src="assets/js/bootstrap-slider.min.js"></script>
    <script src="assets/js/jquery.rateit.min.js"></script>
    <script type="text/javascript" src="assets/js/lightbox.min.js"></script>
    <script src="assets/js/bootstrap-select.min.js"></script>
    <script src="assets/js/wow.min.js"></script>
    <script src="assets/js/scripts.js"></script>

    <!-- For demo purposes – can be removed on production -->

    <script src="switchstylesheet/switchstylesheet.js"></script>

    <script>
        $(document).ready(function(){ 
            $(".changecolor").switchstylesheet( { seperator:"color"} );
            $('.show-theme-options').click(function(){
    Last edited by FishMonger; Jun 12th, 2019 at 05:41 AM. Reason: Added PHP code tags

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    West Des Moines, Iowa
    Posts
    8,104
    Thanks
    37
    Thanked 1,086 Times in 1,082 Posts
    You should start using SQLi or better PDO for talking to the DB. That way you can start working with PHP versions that are secured better.

    Shouldn't the $_SESSION[‘id’][‘weight’] be a number and not a string? If a number the adding quotes should be dropped.
    And the end of your query is ’)"); That's one too many parentheses. ’); should do the trick.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


 

Tags for this Thread

Posting Permissions

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