Skip to content
Advertisement

Script not running and I can’t find out why

I’m new to PHP and HTML, and I’m building a small CMS with those 2 languages. Not sure what I’m missing here, I’m sure it’s beginner level, but for no reason, the script below stopped working, meaning the site won’t load.

Here’s the code:

<?php

session_start();

include_once('../includes/connections.php');

if (isset($_SESSION['logged_in'])) {

    if (isset($_POST['p_name'], $_POST['price'], $_POST['desc'])) {

        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);        
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

        if (empty($imageFileType)) {
            $uploadOk = 0;
        } else {
        
            if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
                echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }
        }

        $name = $_POST['p_name'];
        $price = $_POST['price'];
        $desc = nl2br($_POST['desc']);

        if (empty($name) or empty($price) or empty($desc) or $uploadOk == 0) {
            $error = 'All fields are required! Also check you have uploaded an image!';
        } else {

            //PAGE NOT LOADING FOR SOME REASON
            $new = "uploads/". $name . ".png";
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                rename($target_file, $new);
                echo "Successful upload.";
            } else {
                echo "Sorry, there was an error uploading your file.";
                break;
            }

            $query = $pdo->prepare("INSERT INTO Peripherals (periph_name, periph_price, periph_description, periph_timestamp) VALUES (?, ?, ?, ?)");

            $query->bindValue(1, $name);
            $query->bindValue(2, $price);
            $query->bindValue(3, $desc);
            $query->bindValue(4, time());

            $query->execute();

        }

    }
?>

    <html>

        <head>
            <title>This is my HTML page</title>
        </head>

        <body style="background-color:yellow;">

            <h2><strong> Add new products: </strong></h2>

             <?php if (isset($error)) { ?>
                <strong> <small style = "color:aa0000;"> <?php echo $error; ?> </small></strong>
            <?php } ?>

            <form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data">
                <input type="text" name="p_name" placeholder="Product Name"> <br> <br>
                <input type="number" step="any" name="price" placeholder="Product Price"/> <br> <br>
                <textarea rows="10" cols="50" name="desc" placeholder="Description"> </textarea> <br> <br>
                Select image to upload:
                <input type="file" name="fileToUpload" id="fileToUpload">
                <input type="submit" value="Submit">
            </form>
            <a href="index.php"> Admin Page. </a>

        </body>

    </hmtl>

<?php
} else {
    header('Location:index.php');
}

?>

The connections.php file is just a linker to a database, and in order to access this site, you enter some credentials, hence the session_start();. Can someone help me really quick? 😀

Advertisement

Answer

I learnt from Zachary‘s comment that there were error logs:

Is there anything in the error logs relating to this?

I found the logs and saw that the issue was the break; command that I had in this block:

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
   rename($target_file, $new);
   echo "Successful upload.";
} else {
 echo "Sorry, there was an error uploading your file.";
 break;
}

The issue was that the break command is used to exit a loop of some kind. Since it wasn’t in any kind of loop, the script would break down.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement