Reputation: 87
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
Upvotes: 2
Views: 15965
Reputation: 780818
$_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());
}
}
Upvotes: 3
Reputation: 7762
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);
}
Upvotes: 2
Reputation: 1011
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";
}
?>
Upvotes: 1
Reputation: 231
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";
}
?>
Upvotes: 1