-6

Notice: Undefined index: qty in C:\xampp\htdocs\ecommerce\cart.php on line 130

Notice: Use of undefined constant qty - assumed 'qty' in C:\xampp\htdocs\ecommerce\cart.php on line 137

<?php
if(isset($_POST['update_cart'])){ //line 128
     
     $qty =$_POST['qty'];
     
     $update_qty ="update cart set qty='$qty'";
     $run_qty =mysqli_query($con, $update_qty);
     
     $_SESSION ['qty']=$qty;
     
     $total=$total*qty; //line 137
}
?>
Community
  • 1
  • 1
SoulCake
  • 1
  • 1
  • 2
  • 4
    you forgot $ before qty in line 137 – Vlad Krasovsky Jun 01 '16 at 22:41
  • 1
    Welcome to Stack Overflow! Please read [what this site is about](http://stackoverflow.com/about) and "[How to ask](http://stackoverflow.com/questions/how-to-ask)" before asking a question. (You didn't actually ask a *question*. You posted some code and some error messages.) – rrauenza Jun 01 '16 at 22:45
  • 1
    @VladKrasovsky Post an answer and claim your karma :) – rrauenza Jun 01 '16 at 22:45

2 Answers2

1

you forgot $ before qty in line 137 and seems like $_POST['qty'] is empty

$total=$total*qty;

suppost to be

$total=$total*$qty;

fixed version:

if(isset($_POST['update_cart'])){
     $qty =$_POST['qty'];
     $update_qty ="update cart set qty='$qty'";
     $run_qty =mysqli_query($con, $update_qty);
     $_SESSION ['qty']=$qty;
    $total=$total*&qty;
 }

hope it helps

0

here is the proper way to concat strings. You are missing a $ and assigning a variable for qty is totaly useless.

if (isset($_POST['update_cart']) !==false) {
    $update_qty = 'update cart set qty="' . $_POST['qty'] . '"';
    $run_qty = mysqli_query($con, $update_qty);
    $_SESSION ['qty'] = $_POST['qty'];
    $total = ($total * floatval($_POST['qty']));
}
cpugourou
  • 775
  • 7
  • 11
  • The error message says that `$_POST['qty']` is an undefined index. How will this fix that? – Barmar Jun 01 '16 at 23:50
  • is qty the proper returned value from your ajax ? show your ajax – cpugourou Jun 01 '16 at 23:56
  • Be careful with isset. It will return false if null or undefined. It s mandatory to always check if !==false , the only way to make sure it is true. – cpugourou Jun 02 '16 at 00:00
  • `$_POST` will never contain `false` or `undefined`. It's created automatically from the POST parameters, and everything in it is either a string or an array. – Barmar Jun 02 '16 at 00:02
  • Unless some code in your script has explicitly modified it, e.g. `$_POST['foo'] = null;`. – Barmar Jun 02 '16 at 00:03
  • $_POST by itself yes, but you are checking for an index and the index can be undefined. – cpugourou Jun 02 '16 at 00:08
  • There are three possibilities: `$_POST['update_cart']` is an undefined index, so `isset()` will return `false`. `$_POST['update_cart']` is a string, and `isset()` will return `true`. Or `$_POST['update_cart']` is an array, and `isset()` will return `true`. It will never be the case that the value is undefined or null, so you don't have to worry about those cases. – Barmar Jun 02 '16 at 00:12
  • In this particular case, yes. But I am just trying to explain a general good practice to avoid incorect behavior in many other isset check cases. – cpugourou Jun 02 '16 at 00:14
  • How does using `if (isset($variable) !== false)` solve this problem, anyway? Can you show a case where it acts differently that `if (isset($variable))`? Regardless of what it checks, it always returns either `true` or `false`, so the strict comparison will produce the same result. – Barmar Jun 02 '16 at 00:19
  • main issue is in case the variable equals null. isset will returns false but in many cases null is vastly used in variables. $var=null. isset($var) will returns false. In the same way, if you check for an object, it will always returns false. $var->foo="I exist", isset ( $var->foo) will returns false. – cpugourou Jun 02 '16 at 00:29
  • So to absolutly make sure isset is true, the only way is to check for it: (isset($var) !==false) – cpugourou Jun 02 '16 at 00:31
  • You're wrong: http://ideone.com/1InVrI – Barmar Jun 02 '16 at 00:34
  • Also: http://ideone.com/xlAWiD – Barmar Jun 02 '16 at 00:37
  • In this case yes because the object is considered as a variable but if you do it across a returned class object, it will return false as its not considered as a variable . $foo = new Foo; isset($foo->bar) will returns false . ;-) – cpugourou Jun 02 '16 at 00:39
  • Of course it returns false, because you haven't set it yet. After you do `$foo->bar = 3;` it returns true. – Barmar Jun 02 '16 at 00:52
  • That still doesn't explain why you think it's necessary to write `if(isset($foo->bar) !== false)` instead of just `if(isset($foo->bar))`. There's absolutely no possible difference between them. – Barmar Jun 02 '16 at 00:54
  • That s not what I am saying. Set a class $this->result and test it. – cpugourou Jun 02 '16 at 00:54
  • class Foo { protected $data = array('bar' => null); function __get($p) { if( isset($this->data[$p]) ) return $this->data[$p]; } }. $foo = new Foo;issset($foo->bar) will return false – cpugourou Jun 02 '16 at 00:56
  • If you have `__get` magic method you also need `__isset` magic method if you want to customize how that works. – Barmar Jun 02 '16 at 01:00
  • Sorry man, you are kind but I have to go to bed now. Have a ton of work tomorow. It s just that I use !==false in any cases when I need to check if true. Been like that for me since php exist. :-) – cpugourou Jun 02 '16 at 01:14