Suppose I have a product table with attributes id, name, quantity, image, description. and I have another table image with attributes id, productId, imagepath.
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.
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