Good evening lovely people,
I'm having problems with my query. The query should insert each product which is entered on the page. My table products has 4 fields: id, product_name, quantity and price.
The HTML looks a bit like this:
<form action="" method="post">
Product name: <input type="text" name="product_name[]" value="" />
Quantity: <input type="text" name="quantity[]" value="" />
Price: <input type="text" name="price[]" value="" /> <br /> <br />
<input type="submit" name="submit" value="Insert new products" />
</form>
I want my users to be able to insert new products into our database table products. By the way, I've created a javascript function which adds more fields to the form so my employees do not have to submit the form every time he or she wants to add a new product, but instead just add as many fields he or she needs to submit. Hopefully you'll know what I am talking about :)
Since I am not an expert in PHP coding, then I've tried on my own, which in my case has not worked particularly well. Of course I've been reading some articles on the web about querying array data, but with no luck, sadly.
So far, the PHP code looks like this:
<?
if(isset($_POST['submit'])){
$product_name = addslashes($_POST['product_name']);
$quantity = addslashes($_POST['product_name']);
$price = addslashes($_POST['product_name']);
$db->query("INSERT INTO products (product_name, quantity, price) VALUES ('".$product_name."', '".$quantity."', '".$price."')");
}
?>
If my employees have to insert each product one by one this had not been a problem. But since they can add more fields to the form then it becomes a problem.
Hopefully there is a logical explanation, but I can not figure it out on my own. And of course I am aware that I have not escaped my variables but this code is just for testing purposes. Have a good evening folks :)
addslashes
is a quite weak form of protection against SQL Injection. – kapa Feb 13 '14 at 21:54$_POST
data is an array - you'll need to iterate over it, so best to look upfor()
orforeach()
– kero Feb 13 '14 at 21:55