How to insert multiple data of same name from single form in multiple row of same column of table
Solution 1
$_POST['searchid']
is an array, not a single string. You need to loop over them:
if (isset($_POST["submit"]))
foreach ($_POST['searchid'] as $searchid) {
$data1 = mysql_real_escape_string($searchid);
mysql_query("INSERT INTO php_test (name) VALUES ('$data1')") or die(mysql_error());
}
}
header("location: index.php");
Also, get rid of the duplicate id="searchid"
attributes in your HTML. IDs have to be unique. You probably don't need these elements to have IDs at all.
If you have multiple columns, you can do it like this:
if (isset($_POST["submit"]))
foreach ($_POST['searchid'] as $index => $searchid) {
$data1 = mysql_real_escape_string($searchid);
$data2 = mysql_real_escape_string($_POST['searchid2'][$index]);
mysql_query("INSERT INTO php_test (name, name2) VALUES ('$data1', '$data2')") or die(mysql_error());
}
}
Solution 2
As you are getting value in array. So you should place your insert statement inside loop. may be something like this:-
As suggested you can't mysql_real_escape_string
over array. So you should remove from top and mysql_real_escape_string
inside the loop for each value.
$data1 = $_POST['searchid'];
foreach($data1 as $postValues) {
$name = mysql_real_escape_string($postValues);
$query1 = "INSERT INTO php_test (name) VALUES ('$name')";
$query = mysql_query($query1,$connection);
}
Solution 3
Please try below code :
<?php
include "db.php";
if (isset($_POST["submit"]))
foreach($_POST['searchid'] as $id){
$searchid= mysql_real_escape_string($id);
$query1 = "INSERT INTO php_test (name) VALUES ('$searchid')";
$query = mysql_query($query1,$connection);
}
if($query){
header ("location: index.php");
}
else{
echo "Something Wrong";
}
?>
Solution 4
try this:
<?php
include "db.php";
if (isset($_POST["submit"]))
foreach($_POST["submit"] as $searchval) {
$query1 = "INSERT INTO php_test (name) VALUES ('$searchval')";
$query = mysql_query($query1,$connection);
}
if($query){
header ("location: index.php");
}
else{
echo "Something Wrong";
}
?>
Anuveester
Updated on June 14, 2022Comments
-
Anuveester almost 2 years
I have
<form action="entry.php" method="post" > <table> <tr> <td> <input type="text" name="searchid[]" id="searchid" placeholder="Data 1" ><br /> <input type="text" name="searchid[]" id="searchid" placeholder="Data 2" ><br /> <input type="text" name="searchid[]" id="searchid" placeholder="Data 3" ><br /> <input type="text" name="searchid[]" id="searchid" placeholder="Data 4" ><br /> </td> </tr> <tr> <td><input type="submit" name="submit" id="submit" value="submit" /></td> </tr> </table> </form>
And entry.php code,
<?php include "db.php"; if (isset($_POST["submit"])) $data1 = mysql_real_escape_string($_POST['searchid']); $query1 = "INSERT INTO php_test (name) VALUES ('$data1')"; $query = mysql_query($query1,$connection); if($query){ header ("location: index.php"); } else{ echo "Something Wrong"; } ?>
This code working with single input data but, I want to enter four data in seperate field of same column and when I submit then it insert data in seperate row, fields name are same and
-
Barmar over 10 yearsyou can't call
mysql_real_escape_string
on an array. -
Anuveester over 10 yearsWarning: mysql_real_escape_string() expects parameter 1 to be string, array given in F:\GoogleDrive\www\search engine\entry.php on line 6 Warning: Invalid argument supplied for foreach() in F:\GoogleDrive\www\search engine\entry.php on line 8 Notice: Undefined variable: query in F:\GoogleDrive\www\search engine\entry.php on line 12 Something Wrong
-
Barmar over 10 yearsTwo issues: 1. You're subject to SQL injection. 2. You're only checking whether the last insert was successful.
-
Anuveester over 10 yearshello sir if I will add one more input field and one more colunm which name is "searchid 2" then how to do same thing for both column "is it possible?"