Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在表单上添加另一项/行,插入数据库_Php_Mysql - Fatal编程技术网

Php 在表单上添加另一项/行,插入数据库

Php 在表单上添加另一项/行,插入数据库,php,mysql,Php,Mysql,这两个字段被插入到数据库中。但是,我想让用户能够“添加另一项”。理想情况下,他们应该能够添加任意数量的项目。当他们提交表单时,数据将被插入到mysql表中 我该怎么做呢?在我的数据库中创建10个额外的列以容纳添加的额外项目听起来既不现实也不理想 谢谢你的帮助 下面是我的代码片段,我将数据插入数据库: if ($stmt = $mysqli->prepare("INSERT items (number, description) VALUES (?, ?)"))

这两个字段被插入到数据库中。但是,我想让用户能够“添加另一项”。理想情况下,他们应该能够添加任意数量的项目。当他们提交表单时,数据将被插入到mysql表中

我该怎么做呢?在我的数据库中创建10个额外的列以容纳添加的额外项目听起来既不现实也不理想

谢谢你的帮助

下面是我的代码片段,我将数据插入数据库:

if ($stmt = $mysqli->prepare("INSERT items (number, description) VALUES (?, ?)"))
                            {
                                    $stmt->bind_param("ss", $number, $description);
                                    $stmt->execute();
                                    $stmt->close();
                            }
只需使用:

name="item[]"
对于项目#'

name=“description[]”
对于item description字段,并在服务器端迭代这两个字段。 在前端,只需创建一个脚本,多次复制该节点

编辑

根据您显示的代码,它应该类似于以下代码:

if(isset($_POST['item']){
for($i = 0; $i < count($_POST['item']); $i++){
$number = $_POST['item'][$i];
$description= $_POST['description'][$i];
if ($stmt = $mysqli->prepare("INSERT items (number, description) VALUES (?, ?)"))
                            {
                                    $stmt->bind_param("ss", $number, $description);
                                    $stmt->execute();
                                    $stmt->close();
                            }
}
if(设置($\u POST['item'])){
对于($i=0;$iprepare(“插入项目(编号、说明)值(?)”)
{
$stmt->bind_参数(“ss”,$number,$description);
$stmt->execute();
$stmt->close();
}
}
在javascript中,请执行以下操作:

function addRow(){
  document.getELementById('container').el.innerHTML += '<input type="text" name="item[]" /><input type="text" name="description[]" />' ;
}
函数addRow(){
document.getELementById('container').el.innerHTML+='';
}
HTML代码:

<form name='myform' >

 <div id="container">
 <input type="text" name="item[]" /><input type="text" name="description[]" />     
 </div>
    <p onclick="addRow() ;" >Add another item</p>
</form>

添加另一项

我希望这有帮助

更新

试试这个:

if(isset($_POST['item']){

if ($stmt = $mysqli->prepare("INSERT items (number, description) VALUES (?, ?)"))
{
  for($i = 0; $i < count($_POST['item']); $i++){
  $number = $_POST['item'][$i];
  $description= $_POST['description'][$i];

                                    $stmt->bind_param("ss", $number, $description);
                                    $stmt->execute();

  }
  $stmt->close();

 }
if(设置($\u POST['item'])){
if($stmt=$mysqli->prepare(“插入项目(编号、说明)值(?)”)
{
对于($i=0;$ibind_参数(“ss”,$number,$description);
$stmt->execute();
}
$stmt->close();
}

这是一个相当复杂的问题,但有一些非常简单的解决方案。首先,您需要更改用于处理项目数量可变的后端PHP脚本

例如,现在,您可能有以下情况:

$item_number = $_POST['item_number'];
$item_description = $_POST['item_description'];
add_item_to_db($item_number, $item_description);
jQuery('<input type="text" name="item_number[]" /><input type="text" name="item_description[]" />').appendTo('#someDiv');
您需要更改代码以处理项目数组:

$item_numbers = $_POST['item_number'];
$item_descriptions = $_POST['item_description'];
// validate that count($item_numbers) == count($item_descriptions)
for ($i = 0; $i < count($item_numbers); $i++) {
    add_item_to_db($item_numbers[$i], $item_descriptions[$i]);
}
$item\u number=$\u POST['item\u number'];
$item\u description=$\u POST['item\u description'];
//验证计数($item\u number)==count($item\u说明)
对于($i=0;$i
上面没有显示您需要执行的大量错误处理。如果用户输入的item_编号与item_描述的编号不同,则您必须确定如何处理。此外,任何字段都可能为空。某些情况可能是错误,其他情况可能不是

您必须更改您的HTML:

<input type="text" name="item_number[]" />
<input type="text" name="item_description[]" />

请注意名称中的[]。它为每个名称指定一个值数组

最后,当用户按下“添加另一项”链接时,您需要在屏幕上动态添加一组新的输入项。我建议使用jQuery进行此操作。要完成此操作,您可以执行以下操作:

$item_number = $_POST['item_number'];
$item_description = $_POST['item_description'];
add_item_to_db($item_number, $item_description);
jQuery('<input type="text" name="item_number[]" /><input type="text" name="item_description[]" />').appendTo('#someDiv');
jQuery(“”).appendTo('#someDiv');

确保新的输入元素被附加到表单元素中。显然,还有很多代码需要编写。这只是概念的一个基本示例。

您的代码?您可以将其发布到JSFIDLE上吗?在数据库中创建额外的列?您肯定做错了,请搜索一些“数据库规范化”tutorials@MehdiKaramosly编辑了我的post@WouterJ我对mysql有点陌生。我将看一些教程。谢谢如果我没有其他项目,它可以正常工作。但是如果我添加更多项目,我会得到
警告:mysqli::prepare()[mysqli.prepare]:无法为添加的每个附加项获取mysqli
。已将一个项添加到数据库中,但未将任何附加项添加到数据库中,并收到所述错误。@Alex请尝试我刚才添加的更新。我如何添加“id”变量?@Alex将
id
添加到您的表中?将
id
与插入到表中的每一行一起添加,是的。同时添加的每个项目的id都应该相同。因此,如果我插入了两个项目,则两个项目的
id