-2

how to solve Notice: Undefined index: cart_id in C:\xampp\htdocs\istore\cart.php on line 6

<?php
  require_once $_SERVER['DOCUMENT_ROOT'].'/istore/core/init.php';
  include 'includes/category_head.php';
  include 'includes/cart-header.php';

  if($cart_id != ''){
   
    $cartQ = $db->query("SELECT * FROM cart WHERE id = '{$cart_id}'");
    $result = mysqli_fetch_assoc($cartQ);
    $items = json_decode($result['items'],true);
    $i = 1;
    $sub_total = 0;
    $item_count = 0;

  }
 ?>

Someone please help me to fix it.

susan
  • 23
  • 4
  • Where is line number 6 in your code??? – Saty Aug 05 '16 at 08:18
  • if($cart_id != ''){ – susan Aug 05 '16 at 08:18
  • Where you define `$cart_id`??? – Saty Aug 05 '16 at 08:19
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST` or `$_GET` data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Aug 05 '16 at 08:22
  • What's also odd is the mixing and matching of object-oriented style for `mysqli`, which I'd strongly recommend, with the old PHP 4 procedural style. The OO version is usually much more compact, reads better, and avoids a lot of simple mistakes caused by forgetting to supply the database or statement handle. – tadman Aug 05 '16 at 08:23
  • @Saty how to define $cart_id variable ? – susan Aug 05 '16 at 08:31

2 Answers2

2

The problem is, that your variable $cart_id is not defined.

Change your if-statement to the following:

if(isset($cart_id) && $cart_id != '')

With this, you first check if the variable is set, and if it's set you check if it isn't empty.

Documentation on isset: http://php.net/manual/en/function.isset.php

UeliDeSchwert
  • 1,146
  • 10
  • 23
  • where does it come from? You define a variable by assigning a value to it. like this: `$my_var = 5`. Now, `$my_var` is assigned. You just have to assign some value to `$cart_id`. – UeliDeSchwert Aug 05 '16 at 08:31
0

Try any one of the given two way:

if(isset($cart_id) && $cart_id != '')

or

if(@$cart_id != '')

The above code is don't show the warning

Maths RkBala
  • 2,207
  • 3
  • 18
  • 21