Add a product with details (name,quantity,multiple images) and retrieve them in another page

Suppose I have a product table with attributes id, name, quantity, image, description. and I have another table image with attributes id, productId, imagepath.

and I have a form like this

    <form method="post" action=""> 
      < input type="text" name="name" /> 
      < input type="text" name="quantity" /> 
      < input type="file" name="image[]"  multiple/> 
      < input type="text" name="description" /> 
     < input type="submit" name="create" /> 

I just started learning PHP and I am not very proficient in using php and mysql but I really wish to know how can I add a product with details(name, quantity, description) and two or more images to my database and Later on in another page retrieve the same product with details and the two or more images that belongs to this particular product.

I don’t really know if creating a product and an image table (the image table will get the productId and store imagepath for each upload) is the best way to solve it.
So I wish to find an easy or reliable way to do it and If you can, for example, illustrate with an example it will be of great help to me.
Thanks in advance.

Hi kamseudouglas welcome to the forum

Some of the magic happens in the file the form’s action attribute value points to.

When the form is submitted there will be a $_SERVER['REQUEST_METHOD'] with a value of whatever the form’s method attribute value is. In your case “POST”.

So PHP can test like

if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) { 
// do stuff 

The form will send along a $_POST array with its keys being input tag names. eg.


After validating and sanitizing the values, they can be inserted into a database.

The file input is a bit different from other form inputs but similar.

In any case, as you are starting out, I recommend you start with bare bones simple code at first and add to it as you gain familiarity and understanding. For example, create a PHP file that has only the skeleton of an HTML page with a single input in a form, validate the HTML, have it submit to itself, put PHP that turns on error reporting at the beginning of the file and PHP code that displays the input’s value.

Give that a try and see how you do. If you have problems, don’t hesitate to post them here for help with them.

Hi actually I have learned how to add a product with one image to by viewing online tutorials but I was wondering what if I have 3 or 5 images for one product, how can I add it to the same product Id and retrieve it. That’s what I can’t get to solve.

but if it helps here is the what I learned with adding just one image to a product.

Here is the code just above the form to reference it to the php folder

		$ad = new Ad();
        if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['create'])){
        $insertAd = $ad->adInsert($_POST, $_FILES);

	public function productInsert($data, $file){
		$name = mysqli_real_escape_string($this->db->link, $data['name']);
		$quantity = mysqli_real_escape_string($this->db->link, $data['quantity']);
		$description = mysqli_real_escape_string($this->db->link, $data['description']);

	  $permited  = array('jpg', 'jpeg', 'png', 'gif');
	    $file_name = $file['image']['name'];
	    $file_size = $file['image']['size'];
	    $file_temp = $file['image']['tmp_name'];

	    $div = explode('.', $file_name);
	    $file_ext = strtolower(end($div));
	    $unique_image = substr(md5(time()), 0, 10).'.'.$file_ext;
	    $uploaded_image = "../uploads/".$unique_image;

	    if($name == ""  || $quantity == "" || $description == ""){
	    	$msg = "<span class='error'>Fields must not be empty!</span>";
			return $msg;

	    } elseif ($file_size >1048567) {
	    	 echo "<span class='error'>Image Size should be less then 1MB!

	    } elseif (in_array($file_ext, $permited) === false) {
	     	echo "<span class='error'>You can upload only:-".implode(', ', $permited)."</span>";

	    } else{
	    	move_uploaded_file($file_temp, $uploaded_image);
	    	$query = "INSERT INTO products(name, image, quantity,description) VALUES('$name','$uploaded_image','$quantity','$description')";
	    	$inserted_row = $this->db->insert($query);

						$msg = "<span class='success'> Your Offer is Added Successfully. </span>";
						return $msg;
					} else{
						$msg = "<span class='error'> Sorry! Your offer is not added! Try again later. </span>";
						return $msg;

How can I modify this to be able to add 2 or more images to one single product (product Id)

It sounds like you may need to improve your database architecture so that it’s normalized or maybe ALTER the products table.

What is the schema for the products table? i.e. what does this query return?


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.