-1

orders_vendor.php

6: $sql = mysqli_fetch_assoc(mysqli_query($con,"SELECT * FROM order_master"));
7: $product_id= $sql['product_id'];
8: $added_by = mysqli_fetch_assoc(mysqli_query($con,"SELECT * FROM product WHERE id = '$product_id' AND added_by='".$_SESSION['ADMIN_ID']."'"));
9: $order_row = mysqli_query($con,"select * from order_master where product_id = '".$added_by['id']."'");

Error

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\farm\admin\orders_vendor.php on line 9

It's a multi vendor e-commerce system, so the vendors are supposed to see orders for their products separately on their dashboard, however it's showing that error when I open the orders sections.

Dharman
  • 30,962
  • 25
  • 85
  • 135
LEM tv
  • 1
  • 1
  • That means your second query returns no rows. See [the docs](https://www.php.net/manual/mysqli-result.fetch-assoc.php#refsect1-mysqli-result.fetch-assoc-returnvalues) _"Returns [...] **null** if there are no more rows in the result set"_ – Phil Jul 22 '22 at 00:45
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Jul 22 '22 at 08:55

1 Answers1

-2

Your variable $sql is null. Check this and add a condition to make sure your connection to mysql was successful :

<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

  • 1
    OP does not appear to have any connection errors – Phil Jul 22 '22 at 00:52
  • Oh i see, I didn't know wich line was the 9. – Jacob Proulx Jul 22 '22 at 00:56
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Jul 22 '22 at 08:54