I am creating a basic E-Commerce Site using PHP and MySQL which I have recently started learning. I have a specific problem. Duplicate Products are being added to the database whenever I add two products of the same name. Is there anyway to avoid the same and instead increment the digit for quantity.
else{
$product_image = addslashes(file_get_contents($_FILES['fileField']['tmp_name']));
include_once("../scripts/connect.php");
$sql = mysql_query("INSERT IGNORE INTO products(name, brand, category, sizes, description, price, quantity, product_image, date_added) VALUES('$product_name','$product_brand','$product_category','$product_sizes','$product_description','$product_price','$product_quantity','$product_image', now())");
$id = mysql_insert_id();
$msg = "Success!";
}
Using the above code in production is not recommended as it is vulnerable to sql injection. try using UNIQUE
constraint from database or use:
alter table products add unique index(name, brand, category, sizes, description, price, quantity, product_image, date_added);
tips:
alter the table by adding UNIQUE constraint
I don't know your table structure. Add some logic to update the quantity.
Do not use product names for the unique field . Add a new db field: SKU -- it stands for 'stock keeping unit'.
It would be tempting to use the id field of the database because you can set it up to be auto-incrementing but i would strongly suggest not doing that. many merchants will already have their own SKUs for their products so you want to be able to input the SKU value along with the other product details. and merchant SKUs often have letters in them so make the db field varchar.
also suggest you sign up for amazon seller central and read how they do "Parent" and "Child" products. it might seem overly complicated at first but it will save you so much time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.