Insert all the values in a row based on a selected checkbox - php

I have the following table that fetches data from the database:
while($row = mysql_fetch_array($result))
{
echo '
<tr>
<td width="320">
<input type="checkbox" name="product_ID[]" value="'.$row['Product_ID'].'" />'.$row['Product_description'].'<br />
</td>
<td width="50">
<input type="text" name="product_cost[]" value="'.$row['Product_Retail_cost'].'" maxlength="3" size="3" />
</td>
<td width="50">
<input type="text" name="product_quantity[]" value="1" maxlength="3" size="1" />
</td>
</tr>';
}
What I would like to do, is insert the row into a table, making sure to insert any updated values from the two text boxes. The insert looks like this:
$product_ID = $_POST['product_ID'];
$product_cost = $_POST['product_cost'];
$product_quantity = $_POST['product_quantity'];
for ($i=0; $i<sizeof($product_ID);$i++)
{
$query="INSERT INTO mjj_ordered_products
(`Order_ID`,
`Product_ID`,
`Product_Quantity`,
`Product_Price`)
VALUES ((SELECT MAX(Order_ID) AS Order_ID FROM `mjj_orders`),
'".$product_ID[$i]."',
'".$product_quantity[$i]."',
'".$product_cost[$i]."')";
mysql_query($query) or die (mysql_error());
}
But while this inserts the correct Product_ID, the remaining two values inserted do not correspond to the selected checkbox, but rather by looping through the entire list.
The question: how do I insert the other 2 values associated to the checkbox into the databse?

Assuming the Product_ID is unique, you can pass it as the index in all arrays like
<input type="text" name="product_cost['.$row['Product_ID'].']" value="'.$row['Product_Retail_cost'].'" maxlength="3" size="3" />
And then do something like
foreach($product_ID as $id)
{
$query="INSERT INTO mjj_ordered_products
(`Order_ID`,
`Product_ID`,
`Product_Quantity`,
`Product_Price`)
VALUES ((SELECT MAX(Order_ID) AS Order_ID FROM `mjj_orders`),
'".$product_ID[$id]."',
'".$product_quantity[$id]."',
'".$product_cost[$id]."')";
mysql_query($query) or die (mysql_error());
}

Related

Auto increment employee ID

I just want to add an employee and after that, the employee ID will also increment. Also that textbox must be disabled
Also here's my code. All I want is to auto increment my employee ID when I add a new employee. I hope everyone will help me. Thank you in advance. :)
<center>
<form class="contact_form" action="#" method="post">
<h2>Register New Employee</h2>
<br/>
<table>
<tr>
<td><label for="emp">Emp ID:</label></td>
<td><input type="text" name="emp" placeholder="Emp ID" required /></td>
</tr>
<tr>
<td><label for="name">Name:</label></td>
<td><input type="text" name="fname" placeholder="First Name" required />
<input type="text" name="lname" placeholder="Last Name" required /></td>
</tr>
<tr>
<td><label for="address">Address:</label></td>
<td><input type="text" name="address" placeholder="Full Address" required /></td>
</tr>
<tr>
<td><label for="contact">Contact:</label></td>
<td><input type="text" name="contact" placeholder="Contact Number" required /></td>
</tr>
<tr>
<td><label for="type">Type:</label></td>
<td><select name="type" id="type">
<option>Type of Employee</option>
<option>Contractual</option>
<option>Regular</option>
</select>
</td>
</tr>
<tr>
<td><label for="salary">Salary:</label></td>
<td><input type="text" name="salary" placeholder="Emp Salary" required /></td>
</tr>
</table>
<br/>
<button class="submit" name="submit" type="submit" onclick="message()">Submit</button>
<button class="reset" name="reset" type="reset">Clear</button>
</form>
<?php
if (isset($_POST['submit'])) {
include 'alqdb.php';
$emp=$_POST['emp'];
$fname= $_POST['fname'];
$lname=$_POST['lname'];
$address=$_POST['address'];
$contact=$_POST['contact'];
$type=$_POST['type'];
$salary=$_POST['salary'];
mysqli_query($con, "INSERT INTO employee (EmpID,EmpFName,EmpLName,EmpAddress,ContactNumber,TypeofEmployee,Salary)
VALUES ('$emp','$fname','$lname','$address','$contact','$type','$salary')");
}
?>
</center>
</body>
<script language="javascript">
function message() {
alert("Successfully added!");
}
</script>
If you want to have an EmpID like EMP001
this code will work:
public function autoincemp()
{
global $value2;
$query = "SELECT empid from tbemployee order by empid desc LIMIT 1";
$stmt = $this->db->prepare($query);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$value2 = $row['empid'];
$value2 = substr($value2, 3, 5);
$value2 = (int) $value2 + 1;
$value2 = "EMP" . sprintf('%04s', $value2);
$value = $value2;
return $value;
} else {
$value2 = "EMP0001";
$value = $value2;
return $value;
}
}
You could modify your database to include AUTO_INCREMENT in EmpID.
You can disable input by doing <input type="text" name="emp" placeholder="Emp ID" required disabled />
Place AUTO_INCREMENT attribute to emp_id column in your DB. Remove that EMP ID field from your UI.
The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows. Means, AUTO_INCREMENT automatically insert new incremented id for that field on each INSERT query
Reference
You can hide Emp ID code and change input type="text" to input type="hidden" and remove required in your form.
Now just use AUTO_INCREMENT for EmpId in your table.
It's late but you can do this,
$id = 1; //Your last record Id + 1
str_pad($id, 3, "0", STR_PAD_LEFT);
Hope this helps someone.
Reference
REATE TABLE dbo.tblUsers
(ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
UserID AS 'UID' + RIGHT('00000000' + CAST(ID AS VARCHAR(8)), 8) PERSISTED,
.... your other columns here....
)
Now, every time you insert a row into tblUsers without specifying values for ID or UserID:
INSERT INTO dbo.tblUsersCol1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)

Insert into | on duplicate key update to add new row

I'm trying to update a table, with the ability of adding new rows.
The script is updating the values, but not inserting new rows. When I add a row, it updates the first one.
This is my table:
TABLE `tbl_orderdetail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`quantity` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`discount` int(11) DEFAULT NULL,
`amount` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
And this is my update code:
<?php
if (isset($_POST['update'])) {
$size = count($_POST['quantity']);
$size = count($_POST['product_name']);
$size = count($_POST['price']);
$size = count($_POST['discount']);
$size = count($_POST['amount']);
$i = 0;
while ($i<$size) {
$quantity= $_POST['quantity'][$i];
$product_name= $_POST['product_name'][$i];
$price= $_POST['price'][$i];
$discount= $_POST['discount'][$i];
$amount= $_POST['amount'][$i];
$id= $_POST['id'][$i];
$order_id= $_POST['order_id'][$i];
$query = ("INSERT INTO tbl_orderdetail (id, order_id, quantity, product_name, price, discount, amount )
VALUES ('$id', '$order_id', '$quantity', '$product_name', '$price', '$discount', '$amount' )
ON DUPLICATE KEY UPDATE quantity = '$quantity', product_name = '$product_name', price = '$price', discount = '$discount', amount = '$amount'");
mysql_query($query) or die(mysql_error());
++$i;
}
header("Location: $PHP_SELF");
mysql_close();
}
?>
Here's my form:
<form method="post" action="">
<div class="box-body">
<table class="table table-bordered table-hover">
<?php
$sql = "SELECT * FROM tbl_orderdetail WHERE order_id=$id";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
?>
<thead>
<th>No</th>
<th>Qtde</th>
<th>Descrição</th>
<th>Unitário</th>
<th>Desc.(%)</th>
<th>Valor</th>
<th><input type="button" value="+" id="add" class="btn btn-primary"></th>
</thead>
<tbody id="orderdetail" class="detail">
<?php
while ($rows = mysql_fetch_array($result)){
?>
<tr>
<input type="hidden" name="id[]" value="<?php echo $rows['id']; ?>" >
<td width="2%" class="no">1</td>
<td width="10%"><input type="text" id="quantity" class="form-control quantity" name="quantity[]" value="<?php echo $rows['quantity']; ?>"></td>
<td width="60%"><input type="text" id="product_name" class="form-control product_name" name="product_name[]" value="<?php echo $rows['product_name']; ?>"></td>
<td width="8%"><input type="text" id="price" class="form-control price" name="price[]" value="<?php echo $rows['price']; ?>"></td>
<td width="4%"><input type="text" id="discount" class="form-control discount" name="discount[]" value="<?php echo $rows['discount']; ?>"></td>
<td width="10%"><input type="text" id="amount" class="form-control amount" name="amount[]" value="<?php echo $rows['amount']; ?>"></td>
<td width="6%"><a href="#" class="remove">Excluir</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<th></th>
<th></th>
<th></th>
<th></th>
<th style="text-align:center;" >Total R$</th>
<th style="text-align:center;" class="total">0</th>
</tfoot>
</table>
<input type="submit" class="btn btn-primary" name="update" id="update" value="Salvar">
</form>
First: DO NOT use php's mysql library, use mysqli or PDO instead
Second: Your code is vulnerable to sql injection - best fix would be to use prepared statement (I'd advice again to look into PDO). Never ever trust anything that might be coming from a user (i.e all the POST, GET stuff).
Third: for development always display all errors, warnings, notices etc (You might have seen at least the deprecated warning for the mysql library - if working with php >= 5.5 - and the careless use of $_POST values)
Fourth: I'm sorry to say that your code makes not much of a sense to me:
1.)
$size = count($_POST['quantity']);
$size = count($_POST['product_name']);
// ... etc ...
$size = count($_POST['amount']);
Here you are only taking the size of amount into account. so all the lines with $size = before the last one are senseless.
2.) In the while loop we find lines similar to those:
$id= $_POST['id'][$i];
and $i is incremented until it's the size of $_POST['amount']. What if the size of $_POST['id'] is not the same as the $_POST['amount']?
3.) You are using auto_increment for the primary key but inserting the id manually. I'd remove the auto_increment here.
So if I'd guess I would try echoing $query and check if either the while-loop is not running as often as it should or if the id values are somehow strange.
== Edit ==
I just saw your html output right now. As J.Han pointed out: your script does exactly what it was said that it should do:
You are reading all the results from the database and after editing and submitting it the new values should overwrite the old ones.
If you want to use that script with insert you have to set the id to one that is not yet in your database (i.e. set a value of zero in html and catch that in the php part to generate a valid new id)

php save data into two rows at a time in database?

I have a form like this
<form class="product-data" action="">
<table>
<tr class="data-row">
<td>
<input type="number" name="finance[a][source_unit]" >
</td>
<td >
<input type="number" name="finance[a][target_unit]">
</td>
<td>
<input type="number" name="finance[a][client_price]">
</td>
<td>
<input type="number" name="finance[a][client_salary]" >
</td>
</tr>
<tr class="data-row">
<td>
<input type="number" name="finance[b][source_unit]" >
</td>
<td >
<input type="number" name="finance[b][target_unit]">
</td>
<td>
<input type="number" name="finance[b][client_price]">
</td>
<td>
<input type="number" name="finance[b][client_salary]" >
</td>
</tr>
</table>
</form>
here you can see I have two rows. One for a and another for b. Now I want to save them in the database with two rows. One for the a and another for b at a same time. When I am doing print_r(finance). with this code
$finances = $_POST['finance'];
print_r($finances);
Its showing me an array like this
Array
(
[a] => Array
(
[source_unit] => 3213
[target_unit] => 657654322343
[client_price] => 5435.00
[client_salary] => 897.00
)
[a] => Array
(
[source_units] => 67656565
[target_units] => 43243
[client_price] => 23432.00
[client_salary] => 6546.00
)
)
Now can someone tell me how to save them in each row. I have my database table is like this and the data should be saved like this
Id, product_type, source_unit, target_unit, client_price, lient_salary
1 A 3213 657654322343 5435 897
2 B 67656565 43243 23432 6546
I have two solutions for you. Once that is tailored for this scenario only:
$f = $_POST['finance'];
// insert first row
$query = "INSERT INTO `table` VALUES (NULL, 'A', {$f['a']['source_unit']}, {$f['a']['target_units']}, {$f['a']['client_price']}, {$f['a']['client_salary']})";
mysql_query($query);
// insert second row
$query = "INSERT INTO `table` VALUES (NULL, 'B', {$f['b']['source_unit']}, {$f['b']['target_units']}, {$f['b']['client_price']}, {$f['b']['client_salary']})";
mysql_query($query);
or if you have it more universal (for multiple rows):
$f = $_POST['finance'];
foreach($f as $key => $item) {
// assign key of the letter as value to insert
$letter = strtoupper($key);
// insert a row
$query = "INSERT INTO `table` VALUES (NULL, '{$letter}', {$item['source_unit']}, {$item['target_units']}, {$item['client_price']}, {$item['client_salary']})";
mysql_query($query);
}
loop thru your array and either insert or update accordingly.
foreach($finances as $key => $data)
{
//based on the $key if value exists in database update else insert
echo $key.'<br />';
echo $data['source_unit'].'<br />';
echo $data['target_unit'].'<br />';
echo '<hr />';
}

how insert the multiple checkbox values into database using php

By this code i can able insert the checkbox values to database. But i need add one column in phpmyadmin and in that column i need store the values like, if i select 2 checkbox, i need store that ckeckbox values in one column in another column i need store for selected checkbox as YES and unselected values as NO. But see i want both column
here is my code
<input type="checkbox" name="checkbox[]" value="Home" id="pageHome" onChange="toggleVisibility('home');" /><label for="pageHome"> Home</label><img id="home" src="images/icon-tick.png" style="visibility:hidden"/><br/>
<input name="checkbox[]" value="About_us" id="page_Aboutus" type="checkbox" onChange="toggleVisibility('aboutus');"><label for="page_Aboutus"> About Us</label><img id="aboutus" src="images/icon-tick.png" style="visibility:hidden" /><br/>
<input name="checkbox[]" value="Services" id="pageServices" type="checkbox" onChange="toggleVisibility('services');"><label for="pageServices"> Services</label><img id="services" src="images/icon-tick.png" style="visibility:hidden" /><br/>
<input name="checkbox[]" value="Products" id="pageProducts" type="checkbox" onChange="toggleVisibility('products');"><label for="pageProducts"> Products</label><img id="products" src="images/icon-tick.png" style="visibility:hidden"/><br/><br>
<input name="checkbox[]" value="Enquiry" id="pageEnquiry" type="checkbox" onChange="toggleVisibility('enquiry');"><label for="pageEnquiry"> Enquiry</label><img id="enquiry" src="images/icon-tick.png" style="visibility:hidden"/><br/><br>
<input name="checkbox[]" value="Contact_us" id="pageContact" type="checkbox" onChange="toggleVisibility('Contact');"><label for="pageContact">Contact Us</label><img id="Contact" src="images/icon-tick.png" style="visibility:hidden" /><br/>
php code
$required_pages = implode(',', $_REQUEST['checkBox']);
$sql="insert into request_quote(customer_name,organisation,phone_num,email,country,state,city,zipcode,project_type,website_url,website_purpose,website_keyword,Competitors,sample_websites,no_of_updation,required_pages,additional_page,other_details)
values('$customer_name','$organisation','$phone_num','$email','$country','$state','$city','$zipcode','$project_type','$website_url','$website_purpose','$website_keyword','$Competitors','$sample_websites','$no_of_updation','$required_pages','$additional_page','$other_details')";
mysql_query($sql) or die(mysql_error());
You can add a value='YES' attribute to each of your checkboxes.
Then make an array where you assume no checkbox has been checked. Because when we iterate later, only the checked checkboxes will be sent via $_REQUEST.
$checkBoxes['customer_name']="NO";
$checkBoxes['organisation']="NO";
$checkBoxes['phone_num']="NO";
$checkBoxes['email']="NO";
$checkBoxes['country']="NO";
$checkBoxes['state']="NO";
$checkBoxes['city']="NO";
$checkBoxes['zipcode']="NO";
$checkBoxes['project_type']="NO";
$checkBoxes['website_url']="NO";
$checkBoxes['website_purpose']="NO";
$checkBoxes['website_keyword']="NO";
$checkBoxes['Competitors']="NO";
$checkBoxes['sample_websites']="NO";
$checkBoxes['no_of_updation']="NO";
$checkBoxes['required_pages']="NO";
$checkBoxes['additional_page']="NO";
$checkBoxes['other_details']="NO";
// Only the checked checkboxes wil be itterated below.
// This will mean the $value would be YES no matter what.
// So I write it literal for SQL-Injection-security
foreach ($_REQUEST['checkbox'] as $checkboxName=>$value){
$checkBoxes[$checkBoxName]="YES";
}
And in your SQL-Query, replace the variables with items from the array.
I.E.
$sql="insert into request_quote(customer_name)
values('".$checkBoxes['customer_name']."')";
Security tip:
Also, directly inserting user input can be a huge security risk. Use mysql_real_escape_string($value) on all user-input-values you plan to use in a SQL-query.
You should look into $_POST. W3Schools will help you out: http://www.w3schools.com/php/php_forms.asp.
You have to use 3 tables, and relationate each other
table 1 -> customers (id, name, etc)
table 2 -> required_pages (id, name) - here you will add your options from checkbox (Home, About_us, etc)
table 3 -> customers_required_pages (id, customer_id, required_page_id)
When saving data, you will have to save sequentialy:
Your customer:
$sql = "INSERT INTO customers (field1, field2, fieldn...)"
$qry = mysql_query($sql);
Retrieve your customer Id:
$sql = "SELECT LAST_INSERT_ID() AS customerId";
$qry = mysql_query($sql);
$customerId = mysql_fetch_assoc($qry);
Save the relationship between customers and required_pages
<?php
foreach ($_REQUEST['checkBox'] as $oneCheckbox => $checkboxValue) {
$sql = "INSERT INTO customers_required_pages (customer_id, required_page_id) VALUES ({$customerId['customerId']}, '{$checkboxValue}')";
}
?>
When retrieving your data, you will select values from table2 using left join with table3, this way you will be able to know wich values where or not selected from the checkbox list
$sql = "SELECT rp.id, rp.name, IF(crp.customer_id IS NULL, 'NO', 'YES') AS checked FROM required_pages rp LEFT JOIN customers_required_pages crp ON rp.id = crp.required_page_id";
As an extra, consider use MySQLi or PDO instead of mysql as your database library, since the use of mysql lib has been discouraged
I was having the problem to store the multiple checkbox into the mysql table, but now it's working fine with this code:
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<?php
require("config.php");
?>
<?php
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$gender = $_POST['gender'];
$hobbies = implode(",",$_POST['hobbies']);
echo $hobbies;
$country = $_POST['country'];
// echo $hobbies;
//foreach($hobbies as $hobbies1){
//echo $hobbies1;
//$implode = implode(',',$hobbies1);
$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
values('$fname','$lname','$gender','$hobbies','$country')";
//}
//$sql = "INSERT INTO `userinfo`(`fname`,`lname`,`gender`,`hobbies`,`country`)
// values('$fname','$lname','$gender','$hobbies1','$country')";
$query = mysql_query($sql) or die(mysql_error());
if
($query){
echo "<h1>Data inserted .. Happy coding</h1>";
}else{
echo "<h1>Please check the problem</h1>";
}
}
//}
?>
<form name="registration" method="post" action="">
<table border="1" cellpadding="2" cellspacing="2" align="center">
<tr>
<td>First Name:</td><td><input type="text" name="fname" required=""></td>
</tr>
<tr>
<td>Last Name:</td><td><input type="text" name="lname" required=""></td>
</tr>
<tr>
<td>Gender:</td><td><input type="radio" name="gender" value="male" required="">Male <input type="radio" name="gender" value="female" >Female</td>
</tr>
<tr>
<td>Hobbies:</td><td><input type="checkbox" name="hobbies[]" value="tennis">Tennis <br>
<input type="checkbox" name="hobbies[]" value="cricket">Cricket <br>
<input type="checkbox" name="hobbies[]" value="dance">Dance7 <br>
<input type="checkbox" name="hobbies[]" value="sketching">Sketching
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<select name="country">
<option >----select----</option>
<option>India</option>
<option>Pakistan</option>
<option>UAE</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Register me"></td>
</tr>
</table>
</form>
</body>
</html>

Couldn't get value from while loop in php

as you see in this code i made a table by selecting models from table in my database..
however i posted the return of the select query to be like the primary column for this table and put it into the while loop so it keeps generating rows till the models which came with the select query be finished
now i got a a problem when i'm trying to get this models in a $_Post[''] supergloble it keeps send me only the last value it gets from the loop
my question is how to get each and every value from the this loop to use it in a single insert query in my DB?
and sorry for the bad English :S !!
<form class="form-signin" action="<?php $_SERVER['PHP_SELF'];?>" method="Post">
<?php
$models = mysql_query("SELECT `Model_Name` FROM `models` WHERE `Brand` = 20");
while($row = mysql_fetch_array($models))
{
echo '
<tr>
<td><input type="text" name="mode[]" value="'.$row['Model_Name'].'"></td>
<td><input type="text" name="sellout[]" value=""></td>
<td><input type="text" name="shelfshare[]" value=""></td>
<td><input type="text" name="price[]" value=""></td>
<td><input type="text" name="Shortage[]" value=""></td>
<td><input type="text" name="Inventory[]" value=""></td>
</tr>
';
}
?>
</form>
the inserting script
$date = date("Y-m-d");
foreach($_POST['mode'] as $key => $mode){
$sellout = $_POST['sellout'][$key];
$shelfshare = $_POST['shelfshare'][$key];
$price = $_POST['price'][$key];
$shortage = $_POST['shortage'][$key];
$inventory = $_POST['inventory'][$key];
mysql_query("INSERT INTO `smartdailyreport`(`SFO_Code`, `Model`, `Sell_Out`, `Shelf_Share`, `Price`, `Shortage`, `Inventory`, `Date`) VALUES ('".mysql_real_escape_string($_SESSION['idd'])."','".mysql_real_escape_string($mode)."','".mysql_real_escape_string($sellout)."','".mysql_real_escape_string($shelfshare)."','".mysql_real_escape_string($price)."','".mysql_real_escape_string($shortage)."','".mysql_real_escape_string($inventory)."','".mysql_real_escape_string($date)."')") or die(mysql_error());
}
Make the name of those inputs an array :
<tr>
<td><div class="col3" align="center"><input type="text" name="mode[]" class="form-control" value="'.$row['Model_Name'].'"></div></td>
<td><div class="col3" align="center"><input type="text" name="sellout[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="shelfshare[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="price[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="Shortage[]" class="form-control" value=""></div></td>
<td><div class="col3" align="center"><input type="text" name="Inventory[]" class="form-control" value=""></div></td>
</tr>
Then when you process the form:
foreach($_POST['mode'] as $key=>$mode){
$thisIsOne = $_POST['mode'][$key];
$alsoThisOne = $_POST['sellout'][$key];
etc...
}
I'll put my comments into an answer for you...
You've got $POST as your $mode value, fix that up. (or remove it, $mode is already defined from the foreach)
Put your query inside your foreach loop, otherwise you just overwrite those variables each time you iterate, then insert the last one at the end
Put mysql_error into the die callback of mysql_query to show you an error if there is one (if you want to)
$date = date("Y-m-d");
foreach($_POST['mode'] as $key => $mode){
$sellout = $_POST['sellout'][$key];
$shelfshare = $_POST['shelfshare'][$key];
$price = $_POST['price'][$key];
$shortage = $_POST['shortage'][$key];
$inventory = $_POST['inventory'][$key];
mysql_query("INSERT INTO `smartdailyreport`(`SFO_Code`, `Model`, `Sell_Out`, `Shelf_Share`, `Price`, `Shortage`, `Inventory`, `Date`) VALUES ('".mysql_real_escape_string($_SESSION['idd'])."','".mysql_real_escape_string($mode)."','".mysql_real_escape_string($sellout)."','".mysql_real_escape_string($shelfshare)."','".mysql_real_escape_string($price)."','".mysql_real_escape_string($shortage)."','".mysql_real_escape_string($inventory)."','".mysql_real_escape_string($date)."')") or die(mysql_error());
}
Lastly, use mysqli_* instead of mysql as mysql has been deprecated for some time now. And also, use mysqli_real_escape_string or similar to escape your POST variables and save you from SQL Injection

Categories