php - Class object in PDO not working with SQL Update
Get the solution ↓↓↓I have a problem. I don't know why the function updateProduct in the class Product is not working. I did use the updateProduct function to delete one product in the 'products' table, but it didn't work. Can someone explain it to me? Thank you
Ps: I am sure that in my 'products' table, there is a product with the productID is 10, but when I did run the updateProduct, the name of the product with the productID is 10 is not updated
db.php
<?php
class report_error{
function http_error($message)
{
header("Content-type: text/plain");
die($message);
}
}
class connection_db{
private $hostname = "localhost";
private $username = "root";
private $password = "";
private $dbname = "name";
public $conn = NULL;
public function connection(){
try
{
$this->conn = new PDO("mysql:host=$this->hostname;dbname=$this->dbname", $this->username, $this->password);
if($this->conn != null){echo "sucessfully connected";}
}
catch(PDOException $e)
{
$error_out = new report_error();
$error_out->http_error("500 Internal Server Error\n\n"."There was a SQL error:\n\n" . $e->getMessage());
}
return $this->conn;
}
}
?>
product.php
<?php
class Product{
private $categoryID;
private $ProductCode;
private $ProductName;
private $listPrice;
public function updateProduct($prodID, $prodName, $conn){
$query='Update products Set productName=:productName Where productID=:productID';
$q=$conn->prepare($query);
$q->bindValue(':productName',$prodName);
$q->bindValue(':productID',$prodID);
$q->execute();
}
}
?>
test_function.php
<?php
require 'db.php';
require 'product.php';
$db = new connection_db();
$conn = $db->connection();
$prod=new Product();
//updateProduct
$prod->updateProduct(10, 'Ludwig 4-pieces Drum Set with Cymbals', $conn);
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.