473,320 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3449
Dormilich
8,658 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,658 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,658 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,658 Expert Mod 8TB
just remove the single quotes. it can’t be that complicated, can it?
Nov 12 '13 #9

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

Similar topics

1
by: lawrence | last post by:
I just switched error_reporting to ALL so I could debug my site. I got a huge page full of errors. One of the most common was that in my arrays I'm using undefined offsets and indexes. These still...
2
by: Steven | last post by:
Hi All, I am moving some php code from a Linux machine to a Windows 2000 machine with the code belowe I get the following error : Notice: Undefined offset: 1 in c:\inetpub\wwwroot\test.php on...
4
by: Richard Lawrence | last post by:
Hi there, I'm having a problem with PHP which I'm not sure how to best solve. Here is the code: $fp = fopen("comments.txt", "r"); while(!feof($fp)) { $line = fgets($fp, 1024); list($key,...
6
by: nicy12 | last post by:
Hi! my name is Peter. iam working on the php platform. while trying to run and compile a program i get the undefined offset error iam nto much familiar with this error . Please help me . Thanks in...
9
by: simple12 | last post by:
Hello I have a script which have the facility of entering any code to some part of a webpage. I have some problems with it. When i put some code in the script then their is no error shown. When i...
6
by: tbebest | last post by:
Hi , i have a form and it has A problem: i upload my 3 image and it uploaded in definded path but in insert in db filed there are empty: Notice: Undefined offset: 0 in C:\wamp\www Notice:...
4
by: Kevon | last post by:
Hello, I'm designing a photo gallery for my website, I've followed several tutorials online about how to go about doing this, with great success, I have come up with a set of codes, however I keep...
7
by: Neha Parihar | last post by:
hello all.. I am using luminous php syntax highlighter in my codeigniter project. It is working i.e. it highlights the codes. But an error also encountered -- A PHP Error was encountered...
1
by: Essiej | last post by:
Hi, I'm getting the error: undefined offset 1 on line 5 of cart.tpl and undefined offset 2 on line 6 of cart.tpl. The lines in this file are: 1 <?php 2
3
by: JayEm | last post by:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <?php $table =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.