如何在PHP中修复此错误?

时间:2018-02-21 12:13:11

标签: php mysql

<?php
    include("connections.php");
    $total = 0;

    $cart_id = $_GET['cart_id'];

    $record = mysqli_query("SELECT * FROM tbl_cart"); //Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\SAD MenApp\updatecart.php on line 7

    $record_number = mysqli_num_rows($record); //Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\SAD MenApp\updatecart.php on line 8

    if($record_number > 0 ){
        while($row = mysqli_fetch_assoc($record)) {
            $cart_id = $row['cart_id']; 
            $cart_name = $row['cart_name'];
            $cart_price = $row['cart_price'];
            $cart_quantity = $row['cart_quantity'];

            // $price_x_quantity = number_format($row['cart_price'] * $cart_quantity, 2);
            // $total += number_format($row['cart_price'] * $cart_quantity, 2);
    }
        }
        if(isset($_POST['updet'])){
            if(isset($_POST['qty'])){
$qty = $_POST['qty'];
        $qry = "UPDATE tbl_cart SET cart_name = '$cart_name', cart_price = '$cart_price', cart_quantity = '$qty' WHERE cart_id = '$cart_id'";   
        if(mysqli_query($my_conn, $qry)){
            header("location: yourcart.php");
        }else{
            header("location: qwewqewew qwewqewq wqe qwewqe");
        }
    }else{
        echo("ERROR");
    }
            }

?>
  

警告:mysqli_query()需要至少2个参数,1中给出1   第7行的C:\ xampp \ htdocs \ SAD MenApp \ updatecart.php

     

警告:mysqli_num_rows()期望参数1为mysqli_result,   在第8行的C:\ xampp \ htdocs \ SAD MenApp \ updatecart.php中给出的null

2 个答案:

答案 0 :(得分:1)

只需更改此行:

$record = mysqli_query("SELECT * FROM tbl_cart");

要:

$record = mysqli_query($my_conn, "SELECT * FROM tbl_cart");

您需要传递连接对象$my_conn才能使查询生效。 mysqli_query

警告

另外,请使用prepared statements而不是像这样编写查询:

 $qry = "UPDATE tbl_cart SET cart_name = '$cart_name', cart_price = '$cart_price', cart_quantity = '$qty' WHERE cart_id = '$cart_id'";

答案 1 :(得分:-1)

需要传递MySQL连接作为mysqli_query的第一个参数

mysqli_query($conn,$query);