Hello,

Happy new year guys..

I am trying to find a way to Upload and Display multiple images for one product on an ecommerce website that I am working on!

At the moment I can only upload one image for each product.

My current code is bellow:

This is upload.php

<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
include "../config/connect_to_mysql.php";
?>
<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {

    $product_name = mysql_real_escape_string($_POST['product_name']);
    $price = mysql_real_escape_string($_POST['price']);
        $quantity = mysql_real_escape_string($_POST['quantity']);
    $category = mysql_real_escape_string($_POST['category']);
    $details = mysql_real_escape_string($_POST['details']);
    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="add.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    $sql = mysql_query("INSERT INTO products (product_name, price, quantity, details, category, date_added) 
        VALUES('$product_name','$price','$quantity','$details','$category',now())") or die (mysql_error());
     $pid = mysql_insert_id();
    // Place image in the folder 
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
    header("location: add.php"); 
    exit();
}
?>

<div id='wrapper'>
    <span id='about'>Welcome Admin</span>
    <div id='header'>
      <ul id='nav'>
     <li><a href='index.php'><span></span>Dashboard</a></li> 
           <li  class='active'><a href='upload.php'><span></span>Add Products</a></li> 
        <li><a href='logout.php'><span></span>Logout</a></li> 
        </ul>
    </div>
    <div id='content'>
        <div>
          <p><div align="left" style="margin-left:24px;">
      <h2>Inventory list</h2>
      <?php echo $product_list; ?>
    </div></p>
          <p><form action="add.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">Quantity</td>
        <td><label>
        <input name="quantity" type="text" id="quantity" size="12" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Category</td>
        <td><label>
<select name="category" id="category">
          <option value="Shoes">Shoes</option>
          <option value="Boots">Boots</option>
          </select>
        </label></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></p>
        </div>
    </div>
    <div id='footer'></div>
</div>

This is display.php

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
    // Connect to the MySQL database  
    include "config/connect.php"; 
    $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
    // Use this var to check to see if this ID exists, if yes then get the product 
    // details, if no then exit this script and give message why
    $sql = "SELECT * FROM products WHERE id='$id' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $productCount = mysqli_num_rows($query); // count the output amount
    if ($productCount > 0) {
        // get all the product details
            while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $product_name = $row["product_name"];
             $price = $row["price"];
             $details = $row["details"];
             $quantity = $row["quantity"];
             $category = $row["category"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
         }

    } else {
        echo "That item does not exist.";
        exit();
    }

} else {
    echo "Data to render this page is missing.";
    exit();
}

?>

  <div class="product-list" style="background-color:#FFF;">
<table width="900" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="300" rowspan="5" align="right" valign="top" style="padding-top:10px;"><img src="inventory_images/<?php echo $id; ?>.jpg" width="300" height="450" alt="<?php echo $product_name; ?>" /></td>
    <td width="126" height="106">&nbsp;</td>
    <td width="274"><h3 style="font-family:Times New Roman; font-size:1.8em;"><?php echo $product_name; ?></h3></td>
  </tr>
  <tr>
    <td height="120">&nbsp;</td>
    <td><?php echo $details; ?></td>
  </tr>
  <tr>
    <td height="110">&nbsp;</td>
    <td style="font-family:Times New Roman; font-size:1.8em;">Price: £<?php echo $price; ?></td>
  </tr>
    <tr>
    <td height="50">&nbsp;</td>
    <td style="font-family:Times New Roman; font-size:1.8em;">Quantity Left: <?php echo $quantity; ?></td>
  </tr>
  <tr>
    <td height="121" >&nbsp;</td>
    <td valign="middle" style="border-top:solid 2px #000;">      <form id="form1" name="form1" method="post" action="cart.php">
        <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
        <input type="submit" name="button" id="button" class="addtoCart" value="Add to Shopping Cart" />
      </form>
      </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>  




  </div>

could someone please help me out with this issue?

Thanks in advance

Recommended Answers

Have you checked the docs already?

Jump to Post

JP, uhh... maybe open your own thread on this.. been dead for over 4 years.

The short answer is wat anas.man suggested; Use that, get multiple images in the file input, post to your server and var_dump($_POST) and see what's in there.

Jump to Post

All 5 Replies

Have you checked the docs already?

yes, i have and nothing makes sense on php docs. EVER..

u can use <input type="file" multiple="multiple" />

Hey!! did you solve your problem ? I currently have the same problem. Any help?

JP, uhh... maybe open your own thread on this.. been dead for over 4 years.

The short answer is wat anas.man suggested; Use that, get multiple images in the file input, post to your server and var_dump($_POST) and see what's in there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.