0

I'm having trouble fixing my errors, the functions are working fine but i need to get rid of the errors

i have this following errors:

Warning: Illegal string offset 'userID' in C:\xampp\htdocs\checkout.php on line 15

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\checkout.php on line 15

Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\checkout.php on line 19

and heres my code:

CHECKOUT.PHP

<?php
// include database configuration file
include 'dbConfig.php';
include 'login.php';
// initializ shopping cart class
include 'Cart.php';
$cart = new Cart;

// redirect to home if cart is empty
if($cart->total_items() <= 0){
header("Location: index.php");
}

// set customer ID in session
$_SESSION['sessCustomerID'] = $sessData['userID']; //this is the ID for the logged in user

// get customer details by session customer ID
$query = $db->query("SELECT * FROM users WHERE id =".$_SESSION['sessCustomerID']);
$custRow = $query->fetch_assoc(); 
?>

LOGIN.PHP

<?php
 session_start();
$sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:'';
if(!empty($sessData['status']['msg'])){
    $statusMsg = $sessData['status']['msg'];
    $statusMsgType = $sessData['status']['type'];
    unset($_SESSION['sessData']['status']);
}
?>
<div class="container">
    <?php
        if(!empty($sessData['userLoggedIn']) && !empty($sessData['userID'])){
        include 'user.php';
        $user = new User();
        $conditions['where'] = array(
            'id' => $sessData['userID'],
        );
        $conditions['return_type'] = 'single';
        $userData = $user->getRows($conditions);
?>

DBCONFIG.PHP

<?php
//DB details
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'dbblair';

//Create connection and select DB
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

if ($db->connect_error) {
    die("Unable to connect database: " . $db->connect_error);
} 
?>
Community
  • 1
  • 1
nealnugu
  • 1
  • 2

1 Answers1

2

Line 15:

$_SESSION['sessCustomerID'] = $sessData['userID'];

Both error messages that refer to this line are very clear.

Warning: Illegal string offset 'userID' in C:\xampp\htdocs\checkout.php on line 15

$sessData is a string (this is what the error says and line #3 of login.php confirms this statement).

Individual string characters can be accessed using the square bracket syntax (similar to arrays) but the offset must be an integer. The offset is a string in your code, and this is what the error says (a string is not a legal value for the offset).

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\checkout.php on line 15

Because it expects an integer for the offset and you use a string instead, the interpreter converts the string to number and the result is 0. The message says that the offset 0 does not exist in the string (and this is correct, as $sessData is ''). As a result, $_SESSION['sessCustomerID'] is initialized with the empty string.

For PHP the two messages above are just a warning and a notice (i.e. the script can continue) but in fact they reveal a serious error in your code.

The string offset and the way you use $sessData in login.php tell that $sessData must always be an array. It's unexplainable why do you set it to an empty string. Line #3 of login.php should read:

$sessData = !empty($_SESSION['sessData']) ? $_SESSION['sessData'] : array();

Lines 18 and 19:

$query = $db->query("SELECT * FROM users WHERE id =".$_SESSION['sessCustomerID']);
$custRow = $query->fetch_assoc(); 

The error:

Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\checkout.php on line 19

Information about this error was asked and answered dozen of times on [so]. I won't repeat the answer here.

mysqli::query() returns FALSE when the query has syntax errors or refers to object that does not exist in the database. In this case, the query ends with WHERE id= because $_SESSION['sessCustomerID'] is empty, as explained above.

But $_SESSION['sessCustomerID'] is still empty on the first page load, even if you fix the initialization of $sessData. Apart from using prepared statements (see the linked answer for details), you should not issue a query to the database if you know in advance you won't get any result (this happens when $_SESSION['sessCustomerID'] is empty).

axiac
  • 68,258
  • 9
  • 99
  • 134
  • my problem now is Undefined index: userID on line15 – nealnugu Oct 19 '17 at 06:31
  • Step away from the code and make a plan. When the user comes for the first time on the site, `$sessData` is empty. This is a special case and treat it in a special way. – axiac Oct 19 '17 at 06:38
  • i just started learning about php im so sorry. it still confuses me and i wanna know exact answers in my own code so i can use it as reference for next time. – nealnugu Oct 19 '17 at 07:38
  • The official [PHP documentation](http://php.net/manual/en/) is the best source of answers for PHP questions. Asking on [so] also helps but many times you'll get opinions, not facts. The facts are presented in the documentation. Also, 90% of the questions you may have on this stage of learning PHP were already answered on [so]. [Search](/search) for them before asking; you save time this way. – axiac Oct 19 '17 at 07:55