469,086 Members | 1,141 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

undefined offset in so.php when i am entering the quantity of item

whenever i am entering the quantity of item i got error undefined offset.

i am sending my full code:-

this is so.php for shopping cart:-


Expand|Select|Wrap|Line Numbers
  1. <html> <center><table width="100%" height="300"> <tr width="100%" height="100%"> <td width="70%" height="70%"> <?php
  2. session_start();
  3. $server="localhost";
  4. $user="root";
  5. $password="";
  6. $db="online sabzi mandi";
  7.  
  8.   mysql_connect($server,$user,$password) or die("sorry can't connect to mysql");
  9.   mysql_select_db($db) or die("sorry can't connect to database");
  10.    if(isset($_GET['page'])) {
  11.     $pages=array("pro","cart","11");
  12.  
  13.      if (in_array($_GET['page'],$pages)){
  14.      $_page=$_GET['page'];
  15.      $quantity='$_POST[quantity]';
  16.  
  17.      }else{
  18.      $_page="pro";
  19.  
  20.      }
  21.  
  22.   }
  23.   else{
  24.  
  25.   $_page="pro";
  26.  
  27.  
  28.   }
  29.  
  30.  
  31. ?> <?php require($_page.".php" ); ?> </td> <td width="30%" height="70%"> <center><h1>cart</h1></center> <table> <tr> <td><b>name</b></td> <td><b>item price</b></td> </tr> <?php
  32.        $sql="select * from products where id IN(";
  33.           foreach($_SESSION['cart'] as $id => $value ){
  34.             $sql.=$id.",";
  35.         }
  36.         $sql=substr($sql, 0, -1).")";
  37.         $query=mysql_query($sql);
  38.         //$quantity='$_POST[quantity]';
  39.         $quantity = isset($_POST["quantity"]) ? $_POST["quantity"] : 'No data from $_POST';
  40.         $totalprice=0;
  41.         if(!empty($query)){
  42.         while ($row=mysql_fetch_array($query)) {
  43.           $subtotal=$_SESSION['cart'][$row['id']][$quantity]*$row['price'];
  44.           $totalprice+=$subtotal;
  45.         ?> <tr> <td><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id']][$quantity]?></td> <td><?php echo $_SESSION['cart'][$row['id']][$quantity]*$row['price'] ?></td> </tr> <?php
  46.         }
  47.     }else{
  48.     echo "You need to add some items.your cart is empty";
  49.     echo "<img src='http://bytes.com/images/empty-cart-dark.png' width='150' height='140'></br>";
  50.     }
  51.  
  52.         ?> <b>Total price:</b> <?php echo $totalprice ?><br> <a href="http://bytes.com/so.php?page=cart">Go to Cart</a> </td> </tr> </table> </center> </html>

this is pro.php for product:-

Expand|Select|Wrap|Line Numbers
  1. <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="http://bytes.com/css/style.css"> <script src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script> <script src="http://bytes.com/js/incrementing.js"></script> </head> <?php
  2.  
  3.  if(isset($_GET['action']) && $_GET['action']=="add" ) {
  4.     $id=intval($_GET['id']);
  5.     $quantity='$_POST[quantity]';
  6.     if(isset($_SESSION['cart'][$id])){
  7.      $_SESSION['cart'][$id][$quantity];
  8.  
  9.     }else{
  10.       $sql_s="select * from products where id={$id}";
  11.       $query_s=mysql_query($sql_s);
  12.        if(mysql_num_rows($query_s)!=0){
  13.         $row_s=mysql_fetch_array($query_s);
  14.         $_SESSION['cart'][$row_s['id']]=array(
  15.         "quantity" => $_POST['quantity'] ,
  16.         "price" =>$row_s['price']
  17.         );
  18.  
  19.  
  20.     }else{
  21.     $message="this product id is invalid";
  22.  
  23.     }
  24.   }
  25. }  
  26.  
  27. ?> <table width="100%"> <tr width="100%" height="100%"> <td width="70%" height="70%"> <?php
  28.       if(isset($message)){
  29.  
  30.         echo "$message";
  31.  
  32.       }
  33.     //echo print_r($_SESSION['cart']);
  34.  
  35.     ?> <table width="100%"> <tr> <td>name</td> <td>price</td> <td>Quantity in Kg</td> </tr> <?php
  36.        $sql="select * from products";
  37.        $query=mysql_query($sql);
  38.  
  39.        while($row=mysql_fetch_array($query)){
  40.        ?> <tr> <td><?php echo $row['name'] ?></td> <td><?php echo $row['price'] ?></td> <form method="post" action="so.php?page=pro&action=add&id=<?php echo $row['id'] ?>"> <td class="numbers-row"> <input type="text" name="quantity" id="quantity" value="1"><br> </td> <td class="buttons"> <input type="submit" value="Add to cart" id="submit"> </td> </form> </tr> <?php } ?> </table> </td> </tr> </table>  

this is cart.php for cart:-


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   if(isset($_POST['submit'])) {
  3.     foreach($_POST['quantity'] as $key => $value) {
  4.     if($value==0){
  5.        unset($_SESSION['cart'][$key]);
  6.        }else{
  7.          $_SESSION['cart'][$key]['quantity']=$value;
  8.        }
  9.  
  10.      }
  11.     }
  12.  
  13.  
  14. ?> <h1>view cart</h1> <a href="http://bytes.com/shopping_cart.php?page=products">Go back to product page</a> <form method="post" action="http://bytes.com/shopping_cart.php?page=cart"> <table width="100%" height="70%"> <tr> <th>name</th> <th>quantity in kg</th> <th>price</th> <th>items price</th> </tr> <?php
  15. $sql="select * from products where id IN(";
  16.           foreach($_SESSION['cart'] as $id => $value){
  17.             $sql.=$id.",";
  18.         }
  19.         $sql=substr($sql, 0, -1).")";
  20.         $query=mysql_query($sql);
  21.         $totalprice=0;
  22.         if(!empty($query)){
  23.         while ($row=mysql_fetch_array($query)) {
  24.           $subtotal=$_SESSION['cart'][$row['id']]['quantity']*$row['price'];
  25.           $totalprice+=$subtotal;
  26.         ?> <tr> <td> <?php echo $row['name'] ?></td> <td> <select name="quantity[<?php echo $row['id'] ?>]"  value="<?php echo $_SESSION['cart'][$row['id']]['quantity']?>"/> <option>0</option> <option>0.5</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> </select></td> <td> <?php echo $row['price'] ?></td> <td> <?php echo $_SESSION['cart'][$row['id']]['quantity']*$row['price'] ?></td> </tr> <?php
  27.         }
  28.         }else{
  29.         echo "<h3><i>You need to add some items.your cart is empty</i></h3>";
  30.         }
  31.  
  32. ?> <tr> <td>total price <?php echo $totalprice ?></td> </tr> </table> <button type="submit" name="submit">update cart</button>
  33. ******<a href="http://bytes.com/2.html" target="_blank">Place Order</a> <p>to remove an item set its quantity to 0</p> </form> <?php
  34.  
  35. ?>
Oct 31 '13 #1
8 3094
Dormilich
8,651 Expert Mod 8TB
which is the HTML where you set the quantity? which line points the error to?
Nov 1 '13 #2
every line which have $quantity='$_POST[quantity]' like in line 15,39,45 in so.php line 5,15 in pro.php and line3 in cart.php
Nov 11 '13 #3
please reply as soon as possible
Nov 11 '13 #4
Dormilich
8,651 Expert Mod 8TB
you are aware that variable in single quotes are not parsed? i.e. the content is assigned literally.
Nov 11 '13 #5
I am new in PHP,so can you please correct my code and post it here.
Nov 12 '13 #6
Dormilich
8,651 Expert Mod 8TB
no.

I can help you but I will not do the work for you.

for more information, read PHP’s String chapter.
Nov 12 '13 #7
can you please correct the mistakes and post that line only.
Nov 12 '13 #8
Dormilich
8,651 Expert Mod 8TB
just remove the single quotes. it can’t be that complicated, can it?
Nov 12 '13 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by lawrence | last post: by
2 posts views Thread by Steven | last post: by
4 posts views Thread by Richard Lawrence | last post: by
9 posts views Thread by simple12 | last post: by
4 posts views Thread by Kevon | last post: by
7 posts views Thread by Neha Parihar | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.