0

i want to insert a form record in mysql table using php which are in array.

i am doing this but its not working .

if(isset($_POST['subimt'])){
$query = "INSERT INTO item (`id`, `1`, `2`, `3`, `4`, `5`) VALUES  ";
foreach($_POST['name'] as $i => $name) 
{
$item = $_POST['item'];
$name = $_POST['name'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$total = $_POST['total'];

        // Add to database
  $query = $query." (NULL, '$item', '$name', '$price', '$quantity', '$total'),";
}
$query = substr($query,0,-1); //remove last char
$result = mysql_query($query);
}
        ?>

this code is not inserting any record in mysql table . thanks in advance

  • 2
    Those aren't your real column names, right? Also, stop using the mysql_* functions, they were deprecated in PHP 5, and removed in 7, so migrate to mysqli, or PDO, and use prepared statements. – Jonnix Feb 21 '18 at 11:39
  • can you share what's your post data ? – Naincy Feb 21 '18 at 11:40
  • Thos code doesn't really make any sense. Also you want to look at http://bobby-tables.com and learn about SQL injection and how to prevent them. use prepared statements - never put user inputs directly into your queries. The way your code is right now, a random person could delete your whole database in a few seconds. Also you want to learn about `auto_increment` for the `id` attribute – Twinfriends Feb 21 '18 at 11:40
  • Please stop using PHP's archaic, insecure, and long-since deprecated mysql_ API – Strawberry Feb 21 '18 at 11:40
  • still not working @BilalAhmed – narendra yadav Feb 21 '18 at 11:42

0 Answers0