Judul : insert multiple fields using foreach loop
link : insert multiple fields using foreach loop
insert multiple fields using foreach loop
create a table students:
SQL>
create table students
(id int primary key auto_increment,
name varchar(200),
age varchar(200)
);
<?php
if (isset($_POST['submit'])) {
$i = 0;
foreach ($_POST as $val) {
$name = $_POST['name'][$i];
$age = $_POST['age'][$i];
$con=mysqli_connect("localhost","root","","abhi");
$sql="INSERT INTO students (name,age) VALUES ('$name','$age')";
$result=mysqli_query($con,$sql);
$i++;
if($result)
{
echo "record inserted";
}
}
}
?>
<h1>Add user</h1>
<form method="post" action="test.php">
<table>
<thead>
<th>Name</th>
<th>Age</th>
</thead>
<tr>
<td><input name="name[]" type="text" /></td>
<td><input name="age[]" type="text" /></td>
</tr>
<tr>
<td><input name="name[]" type="text" /></td>
<td><input name="age[]" type="text" /></td>
</tr>
<tr>
<td><input name="name[]" type="text" /></td>
<td><input name="age[]" type="text" /></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
SQL>
create table students
(id int primary key auto_increment,
name varchar(200),
age varchar(200)
);
<?php
if (isset($_POST['submit'])) {
$i = 0;
foreach ($_POST as $val) {
$name = $_POST['name'][$i];
$age = $_POST['age'][$i];
$con=mysqli_connect("localhost","root","","abhi");
$sql="INSERT INTO students (name,age) VALUES ('$name','$age')";
$result=mysqli_query($con,$sql);
$i++;
if($result)
{
echo "record inserted";
}
}
}
?>
<h1>Add user</h1>
<form method="post" action="test.php">
<table>
<thead>
<th>Name</th>
<th>Age</th>
</thead>
<tr>
<td><input name="name[]" type="text" /></td>
<td><input name="age[]" type="text" /></td>
</tr>
<tr>
<td><input name="name[]" type="text" /></td>
<td><input name="age[]" type="text" /></td>
</tr>
<tr>
<td><input name="name[]" type="text" /></td>
<td><input name="age[]" type="text" /></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
Demikianlah Artikel insert multiple fields using foreach loop
Sekianlah artikel insert multiple fields using foreach loop kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel insert multiple fields using foreach loop dengan alamat link https://othereffect.blogspot.com/2018/05/insert-multiple-fields-using-foreach.html
0 Response to "insert multiple fields using foreach loop"
Post a Comment