-2

It's me again, hello. So I figured out my first problem, sorry for posting too much but I need a lil' help with this one. I keep having these errors:

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp64\www\inventory\inventory\tableedit.php on lines 167, 169, 252, 253,284, 285 etc

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\wamp64\www\inventory\inventory\tableedit.php on lines 167, 169, 252, 253,284, 285 etc

Notice: Undefined index: from in C:\wamp64\www\inventory\inventory\tableedit.php on lines 282, 283 etc

To be honest, I think this whole page is a mess, I'm not really sure why but it seems like it works with the older version Mysql but since I got the deprecated errors, I changed everything to Mysqli but now I got these errors.

1.

< ? php
$da=date("Y-m-d");
$sql=mysqli_query("SELECT * FROM inventory");
$i=1;
while($row=mysqli_fetch_array($sql))
{
$id=$row['id'];
$date=$row['date'];
$item=$row['item'];
$qtyleft=$row['qtyleft'];
$qty_sold=$row['qty_sold'];
$price=$row['price'];
$sales=$row['sales'];

if($i%2)
{
? >


2.

$result1 = mysqli_query("SELECT sum(sales) FROM sales where date='$da'");
while($row = mysqli_fetch_array($result1))


3.

 <?php
  $a=$_POST['from'];
  $b=$_POST['to'];
    $result1 = mysqli_query("SELECT sum(sales) FROM sales where date BETWEEN '$a' AND '$b'");
    while($row = mysqli_fetch_array($result1))
    {
        $rrr=$row['sum(sales)'];
        echo formatMoney($rrr, true);
     }

    ?>

I do really appreciate any answers, suggestions or opinions. Thank you so much for your time! Peace out.

PS: Please don't mark this as duplicate, the problems are different.

Magisch
  • 7,312
  • 9
  • 36
  • 52
Alexa
  • 2,201
  • 1
  • 10
  • 15

1 Answers1

0

Answer:

mysqli_* expects two parameters. You should include your connection to the first parameter.

$sql=mysqli_query($connection, "SELECT * FROM inventory");
/* JUST REPLACE THE NECESSARY CONNECTION VARIABLE ABOVE */

Do the same with your other mysqli_query():

$result1 = mysqli_query($connection, "SELECT sum(sales) FROM sales where date='$da'");

Just make sure that you also use mysqli_* extension to establish your database connection.

$connection = new mysqli("Host", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */

/* CHECK CONNECTION */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

Recommendation:

Since you are using mysqli_* extension already, why not use prepared statement?

Your first set of code (1):

<?php

    $da = date("Y-m-d");
    $i=1;

    $stmt = $connection->prepare("SELECT id, date, item, qtyleft, qty_sold, price, sales FROM inventory");
    $stmt->execute();
    $stmt->bind_result($id, $date, $item, $qtyleft, $qty_sold, $price, $sales);
    while($stmt->fetch()){
        if($i%2){
            /*** YOUR ACTION HERE ***/
        }
    }
    $stmt->close();

?>

On your second set of code (2):

$stmt = $con->prepare("SELECT sum(sales) FROM sales WHERE date = ?");
$stmt->bind_param("s", $da);
$stmt->execute(); /* EXECUTE QUERY */
$stmt->bind_result($sum); /* GET THE RESULT; BIND THE SINGLE RESULT TO THIS VARIABLE; SUM OF sales COLUMN */
$stmt->fetch(); /* FETCH RESULT */
$stmt->close();

On your third set of code (3):

<?php

    $stmt = $con->prepare("SELECT sum(sales) FROM sales WHERE date BETWEEN ? AND ?");
    $stmt->bind_param("ss", $_POST["from"], $_POST["to"]);
    $stmt->execute();
    $stmt->bind_result($rrr);
    $stmt->fetch();
    $stmt->close();

    echo formatMoney($rrr, true);

?>
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49