简体   繁体   English

如何在表的同一列的多行中从单一格式插入相同名称的多个数据

[英]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.

相关问题 如何从具有相同列名的多个表中检索数据 - How to retrieve data from multiple table having same column name 使用从表单将相同的变量名称输入从多个数据更新到Laravel数据库的单个列中 - Update multiple data using same variable name input from a form into single column in database in Laravel 如何在同一行中连接同一张表中的多行 - How to join multiple rows from same table in single row 如何从同一表的多个相同名称中回显单个名称 - How to echo single name from multiple same name from the same table 如何将多个输入插入按相同数据分组的同一行 - How to insert multiple inputs into the same row that group by the same data CSV,将多行数据合并为单行和同一列 - CSV, merge multiple rows data into single row and into the same column 从HTML表单向同一数据库列插入多个值 - Insert multiple values to the same database column from HTML form 如何在多个MySQL行中插入具有相同名称的表单值 - How to insert form values, some with same name, to multiple MySQL rows 如何在具有相同名称的mysql的多行中插入(订单数据) - How to insert (order form data) in multiple rows of mysql having same name 如何将数据插入到对表中的多行使用相同输入名称变量的数据库中? - How to insert data into a database which uses the same input name variable for multiple rows in a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM