0

insert into didn't work .

I have tried mysqli connection and it doesn't work.

Check code please and tell me whats wrong.

<?php
require_once "DBController.php";

class Rate extends DBController
{

function getAllPost()
{
    $query = "SELECT * FROM storedproducts";

    $postResult = $this->getDBResult($query);
    return $postResult;
}

    public function AddRating($rating, $id) {

    $query = $this->conn->prepare("insert into stored_rating ( rating , stored_id) VALUES (?,?)");
   $query->execute([$rating, $id,]);

}


public function getRateAverage($id) {
$query = $this->conn->prepare("SELECT AVG(rating) FROM `stored_rating` WHERE stored_id=?");
$params = array(
        array(
            "param_type" => "i",
            "param_value" => $id
        )
    );    
    $this->bindParams($query,$params);
    $query->->execute();
    $rsult->fetchColumn();
     return $rsult;   

}
// $query = "insert into stored_rating ( rating , stored_id) VALUES (?,?)";
//    $statement->execute([$rating, $id,]);

function updateRatingCount($rating, $id)
{
    $query = "UPDATE storedproducts SET  rating = ? WHERE ID_Stored= ?";

    $params = array(
        array(
            "param_type" => "i",
            "param_value" => $rating
        ),
        array(
            "param_type" => "i",
            "param_value" => $id
        )
    );
    this->updateDB(updateDB,$params);

    }
}
Kuya
  • 7,280
  • 4
  • 19
  • 31
  • Are you using `mysqli` or `PDO`? since `execute()` with array param only available with PDO – catcon Jul 19 '19 at 01:46

1 Answers1

0

mysqli_statement->execute() method does not work with an array parameter, it's only available with PDO. You have to bind your parameter first, then execute.

$query = $this->conn->prepare("insert into stored_rating ( rating , stored_id) VALUES (?,?)");
if ($query) { // Remember to check for return value, sometime prepare statment return false.
    $query->bind_param("ss", $rating, $id);
    $query->execute();
}
catcon
  • 1,295
  • 1
  • 9
  • 18
  • require_once("Rate.php"); $rate = new Rate(); $rate->AddRating($_POST["rating"],$_POST["id"]); $avg_rating=$rate->getRateAverage($_POST["id"]); $rate->updateRatingCount($avg_rating, $_POST["id"])} didn't work when it only updateRatingCount function request it's work – Antar ALKAMEL Jul 19 '19 at 02:11