[英]How to insert multiple data of same name from single form in multiple row of same column of table
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, 和entry.php代码,
<?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 此代码适用于单个输入数据,但是,我想在同一列的单独字段中输入四个数据,当我提交该代码时,它将数据插入单独的行中,字段名称相同且
$_POST['searchid']
is an array, not a single string. $_POST['searchid']
是一个数组,而不是一个字符串。 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. 此外,摆脱HTML中的重复id="searchid"
属性。 IDs have to be unique. ID必须是唯一的。 You probably don't need these elements to have IDs at all. 您可能根本不需要这些元素来拥有ID。
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());
}
}
As you are getting value in array. 当你在数组中获得价值时。 So you should place your insert statement inside loop. 所以你应该把你的insert语句放在循环中。 may be something like this:- 可能是这样的: -
As suggested you can't mysql_real_escape_string
over array. 如建议的那样,您不能通过数组使用mysql_real_escape_string
。 So you should remove from top and mysql_real_escape_string
inside the loop for each value. 因此,您应该从循环顶部和每个值的mysql_real_escape_string
删除。
$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);
}
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";
}
?> ?>
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";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.