0

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Hustlermt
  • 1
  • 3
  • Please provide the Apache error log and if exists the `.htaccess` file(s). – Pinke Helga Jan 02 '23 at 22:50
  • https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php – ADyson Jan 02 '23 at 23:05
  • 1
    `It's also running to the last else statement`... you mean that `else{ $_SESSION['error'] = 'Error adding product!'; }` is executed? If so, then it simply means that `if(isset($_POST['add'])){` was false. But I don't know how you can distinguish that from this: `catch(PDOException $e){ $_SESSION['error'] = 'Error adding product!'; }` being executed, since they both simply result in the same vague error message. If you're going to catch an exception, you also need to _log_ it, so you can debug and prevent it happening again. Right now you're throwing away the useful info from `$e` – ADyson Jan 02 '23 at 23:07
  • Thank you let me try adding the error log – Hustlermt Jan 03 '23 at 06:34
  • @PinkeHelga am getting this 03-Jan-2023 06:40:38 UTC] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/arwdasdt/public_html/admin/category_add.php:1) in /home/arwdasdt/public_html/admin/includes/session.php on line 6 – Hustlermt Jan 03 '23 at 08:30
  • Ok. So what part of the error don't you understand? It's easy to google it and get explanations. We can't see what happens in Session.php, but presumably it's trying to set a HTTP response header, _after_ some response body output has already been produced. That isn't possible, due to how HTTP works, hence the error. It tells you that the output was started in category_add.php on line 1, so have you had a look there? – ADyson Jan 03 '23 at 09:20
  • As mentioned there is some output before a `header` call. This could be plain HTML outside `` tag (even a blank char), `echo` or a PHP warning output. – Pinke Helga Jan 05 '23 at 07:23
  • 1
    `output started at /home/arwdasdt/public_html/admin/category_add.php:1` -> line 1, sounds like the file does not start with ` – Pinke Helga Jan 05 '23 at 07:29
  • Thank you , it was a simple typo as you mentioned – Hustlermt Jan 06 '23 at 08:38

0 Answers0