$strSPQuery = "SELECT * FROM `san_pham` where ma_san_pham=".$arrMa[$i];
    $resultSp= mysqli_query($conn,$strSPQuery);
    $rowsp = mysqli_fetch_array($resultSp);
    $tong +=  $rowsp["gia"]*$arrSl[$i];
<?php 

# Heading Here #

session_start();
//lay session ve
$arrMa = $_SESSION["arrMa"];
$arrSl = $_SESSION["arrSl"];
include("include/open.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table border="1" cellspacing="0px">
    <tr>
        <td>Ma sp</td>
        <td>Ten</td>
        <td>Anh</td>
        <td>So luong</td>
        <td>Don gia</td>
        <td>Thanh tien</td>
    </tr>
    <?php 
    $tong = 0;
    for($i=0;$i<count($arrMa);$i++)
    {
        $strSPQuery = "SELECT * FROM `san_pham` where ma_san_pham=".$arrMa[$i];
        $resultSp= mysqli_query($conn,$strSPQuery);
        $rowsp = mysqli_fetch_array($resultSp);
        $tong +=  $rowsp["gia"]*$arrSl[$i];
    ?>
    <tr>
        <td><?php echo $arrMa[$i]; ?></td>
        <td><?php echo $rowsp["ten_san_pham"]; ?></td>
        <td><?php echo $rowsp["anh"]; ?></td>
        <td><input type="text" value="<?php echo $arrSl[$i]; ?>"/></td>
        <td><?php echo $rowsp["gia"]; ?></td>
        <td><?php echo $rowsp["gia"]*$arrSl[$i]; ?></td>
    </tr>
    <?php 
    }
    ?>
    <tr>
        <td colspan="5">Tong</td>
        <td><?php echo $tong; ?></td>
    </tr>
</table>
<br />
<a href="index.php">Continue mua hang</a>
<br />
<a href="thongtinmuahang.php">Mua Hang</a>
<?php 
include("include/close.php");
?>
</body>
</html>

Based on the code provided, line 31 in the error message corresponds to line 34 in the code snippet posted here. Basically the error message means that it can't process mysqli_fetch_array() because the variable passed in is a boolean instead of a MySQLi result. The reason this would happen would be because mysqli_query() returns FALSE, meaning the query failed. The query would fail because either the table san_pham doesn't exist, doesn't have a column ma_san_pham, or, more likely, because you didn't escape the value of $arrMa[$i]!! ALWAYS escape anything passed into queries to prevent MySQL injection errors.

To solve this, there are two things you need to do:

  1. Wrap $arrMa[$i] in mysqli_real_escape_string() as so: https://www.php.net/manual/en/mysqli.real-escape-string.php
  2. Ensure that the value of $arrMa[$i] exists and is valid
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.