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="images[]" multiple/>
< input type="text" name="description" />
</form>
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.
Yes, this is how you should do it: a "products" table and a "products_images" table. The later should have a foreign key set: "product_id" column should reference the "id" in "products" table. And both tables must have the "id" columns as primary keys.
I hope you will understand the code:
Just test the code as it is, first, so that you see what it does. Of course, after you create the tables as I did (see the create table syntaxes below).
Good luck.
Create table syntaxes
config.php
PDO solution
You already have it...
MySQLi solution (object-oriented style)
connection.php
addProduct.php
getProduct.php