I've got this code:
if (isset($_POST['save'])) {
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
$new_description = str_replace("'", "\'", $description);
$cat_name = $_POST['dropdown'];
$cat_id = mysqli_query($db, "SELECT cat_id FROM category where cat_name = '$cat_name';");
mysqli_query($db, "INSERT INTO products (item_name, item_price, item_cat_id, item_descr) VALUES ('$name', '$price', '$cat_id', '$new_description')");
$_SESSION['message'] = "Le produit a été ajouté avec succès!";
header('location: admin.php');
}
I wanted to fetch the category id using the category name and use it to set the value of the item_cat_id column in the products table.
But when I click save I get this error instead:
Fatal error: Uncaught Error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\jst\server.php:23
Stack trace:
#0 {main} thrown in C:\xampp\htdocs\jst\server.php on line 23