user2957365
user2957365

Reputation: 197

PHP - blank page after executing mysqli INSERT query with data from HTML form

I'm new to PHP. I'm currently working on adding data into a MySQL database table 'products' from data entered into an HTML form (product name, price, category etc.) I wrote something earlier that did work, but it gave me a warning. I changed something in the code to correct the warning (I don't remember exactly what I changed), and now all I'm getting is a blank page when I click on the submit button on the form.

I'm at a complete loss. I have been struggling over this for the past few hours. I looked at a lot of similar questions, but I still don't see/understand what's going wrong in my code. Note: $con is the database connection, which has already been established.

<?php 

// Parse the form data and put it into variables to add into the system
if (isset($_POST['product_name'])) {

  $product_name = mysqli_real_escape_string($con,$_POST['product_name']);
    $price = mysqli_real_escape_string($con,$_POST['price']);
    $category = mysqli_real_escape_string($con,$_POST['category']);
    $subcategory = mysqli_real_escape_string($con,$_POST['subcategory']);
    $details = mysqli_real_escape_string($con,$_POST['details']);

  // See if the product name that you're trying to add is a match to another existing product
  // If it is a match, then echo out that you're trying to add a duplicate product and exit
    $sql = mysqli_query($con,"SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysqli_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'It appears that a product by that name already exists. To go back, <a href="https://project.cs.cf.ac.uk/V.Bhatia/coursework/storeadmin/inventory_list.php">click here.</a>';
        exit();
    }

  // If the product match check showed that the product doesn't exist in the database, add the product
    $sql = mysqli_query($con,"INSERT INTO products (product_name, price, details, category, subcategory) 
        VALUES('$product_name','$price','$details','$category','$subcategory'") or die (mysql_error());

  // This is the picture ID. Putting it in a variable "pid". Pictures are connected to products by their ID. A product of id "1" will have a picture "1.jpg"
  $pid = mysql_insert_id();

  // Place image in the folder
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "https://project.cs.cf.ac.uk/V.Bhatia/coursework/images/$newname");

  // Come back to the inventory_list page after the adding process is done and exit the script
  header("location: inventory_list.php"); 
    exit();
}
?>

This is the HTML form:

<h3>&darr; Add New Inventory Item Form &darr;</h3>
    <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
    <table width="90%" border="0" cellspacing="0" cellpadding="6">
      <tr>
        <td width="20%" align="right">Product Name</td>
        <td width="80%"><label>
          <input name="product_name" type="text" id="product_name" size="64" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Price</td>
        <td><label>
          $
          <input name="price" type="text" id="price" size="12" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Category</td>
        <td><label>
          <select name="category" id="category">
          <option value="Clothing">Clothing</option>
          </select>
        </label></td>
      </tr>
      <tr>
        <td align="right">Subcategory</td>
        <td><select name="subcategory" id="subcategory">
        <option value=""></option>
          <option value="Hats">Hats</option>
          <option value="Pants">Pants</option>
          <option value="Shirts">Shirts</option>
          </select></td>
      </tr>
      <tr>
        <td align="right">Product Details</td>
        <td><label>
          <textarea name="details" id="details" cols="64" rows="5"></textarea>
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Image</td>
        <td><label>
          <input type="file" name="fileField" id="fileField" />
        </label></td>
      </tr>      
      <tr>
        <td>&nbsp;</td>
        <td><label>
          <input type="submit" name="button" id="button" value="Add This Item Now" />
        </label></td>
      </tr>
    </table>
    </form>

Otherwise, the database connection works fine. If I add entries manually with phpMyAdmin, then I'm able to query the database and echo a list on my web page. I apologise for the lengthy code, but I'm literally out of ideas and I hope someone can read this and help.

Upvotes: 0

Views: 2953

Answers (2)

Xtophe
Xtophe

Reputation: 2277

What is this supposed to do?

header("location: inventory_list.php");

Are you sure the error happens before or is it possible your inventory_list.php comes out blank if you don't pass any variable?

Upvotes: 0

Scony
Scony

Reputation: 4138

// If the product match check showed that the product doesn't exist in the database, add the product
  $sql = mysqli_query($con,"INSERT INTO products (product_name, price, details, category, subcategory) 
      VALUES('$product_name','$price','$details','$category','$subcategory'") or die (mysql_error());

You miss closing bracket ')' in SQL code.

error_reporting(-1);

On the begining of code should help.

Upvotes: 1

Related Questions