I have this code returning a blank page or error 500 when i try to add product.
It's also running to the last else statement on the code proving my querys are being skipped.
I am running this code on a live server cpanel to be specific.
On my local machine, am using xampp and it's perfectly working well.
<?php
include 'includes/session.php';
include 'includes/slugify.php';
if(isset($_POST['add'])){
$name = $_POST['name'];
$slug = slugify($name);
$category = $_POST['category'];
$price = $_POST['price'];
$description = $_POST['description'];
$filename = $_FILES['photo']['name'];
$conn = $pdo->open();
$stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM products WHERE slug=:slug GROUP BY id=:id");
$stmt->execute(['slug'=>$slug]);
$row = $stmt->fetch();
if($row['numrows'] > 0){
$_SESSION['error'] = 'Product already exist';
}
else{
if(!empty($filename)){
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$new_filename = $slug.'.'.$ext;
move_uploaded_file($_FILES['photo']['tmp_name'], '../images/'.$new_filename);
}
else{
$new_filename = '';
}
try{
$stmt = $conn->prepare("INSERT INTO products (category_id, name, description, slug, price, photo) VALUES (:category, :name, :description, :slug, :price, :photo)");
$stmt->execute(['category'=>$category, 'name'=>$name, 'description'=>$description, 'slug'=>$slug, 'price'=>$price, 'photo'=>$new_filename]);
$_SESSION['success'] = 'User added successfully';
}
catch(PDOException $e){
$_SESSION['error'] = 'Error adding product!';
}
}
$pdo->close();
}
else{
$_SESSION['error'] = 'Error adding product!';
}
header('location: products.php');
?>
I am expecting php to run the select query first then insert into database.
So are there changes on my live server that need to be done since the code fully works on xampp offline bt returns error on live server cpanel.