I am trying to implement a table lock on my database for a sample shopping website hosted locally.
The following is a sample of my index.php:
case "add":
if(!empty($_POST["quantity"])) {
if(!isset($timestamp)){
$timestamp = time();
$lock = $db_handle->runQuery("LOCK TABLE orders_f write, prod_f read");
}
$productByCode = $db_handle->runQuery("SELECT * FROM prod_f WHERE code='" . $_GET["code"] . "'");
$itemArray = array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"], 'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],array_keys($_SESSION["cart_item"]))) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k) {
if(empty($_SESSION["cart_item"][$k]["quantity"])) {
$_SESSION["cart_item"][$k]["quantity"] = 0;
}
$_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"];
}
}
} else {
$_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
Where the table lock has been enforced. I get the following error when I try to add in the cart, which is the source of the case statement above:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in path\dbcontroller.php on line 24 Here is a snippet of my dbcontroller.php:
function runQuery($query) {
$result = mysqli_query($this->conn,$query);
while($row=mysqli_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
else
return false;
}
I've been searching all night on Youtube and on Stackoverflow but none of the options seem to be working. It is on an Apache
server using Wamp on a 64 bit Windows 10
machine. Please, any suggestions are welcomed.