John Davidson

html - Insert multiple values of input fields one column in separate multiple rows using php

0 comments
Message:


I want to Insert multiple values of input fields one column in separate multiple rows. input is taking multiple values in array as tags. I am using PHP & sql server as database


<input class="col-sm-10 tokenfield" style="width:50%;" type="text" placeholder="Enter ID" class="form-control" name="id[]" id="id" required />

sql query is


$id = $_POST['id'];
foreach( $id as $i){
$sql = insert into [db].[table] (id,name,phone,message) values ($i,$name,$phone,$message)
}

it showing as group values in single row in column of table but i want all values in as different rows values as id



My Answer:
$id = $_POST['id'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$message = $_POST['message'];

foreach( $id as $key => $i){
$sql = "INSERT INTO [db].[table] (id, name, phone, message) VALUES ('$i', '$name[$key]', '$phone[$key]', '$message[$key]')";
// execute the SQL query here
}

This code will insert each value from the input fields as a separate row in the database table.

Rate this post

4 of 5 based on 8993 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us