繁体   English   中英

如何存储表格多个字段的数据?

[英]how to store the data of multiple numbers of fields of form?

我有一个表单,用户可以在其中根据需要增加或添加字段数。

字段添加成功.....

为此,我使用javascript,代码在这里。

<script type="application/javascript">
var counter3 = 0;
function addAuthor() {
    // Get the main Div in which all the other divs will be added
    var mainContainer = document.getElementById('Author');

    // Create a new div for holding text and button input elements
    var newDiv = document.createElement('div');

    // Create a new text input
    var newText = document.createElement('input');
    newText.type = "text";
    //var i = 1;
    newText.name = "Author[]";
    //newText.value = counter3 + 2 + ". ";

    //Counter starts from 2 since we already have one item
    //newText.class = "input.text";
    // Create a new button input
    var newDelButton = document.createElement('input');
    newDelButton.type = "button";
    newDelButton.value = "-";

    // Append new text input to the newDiv
    newDiv.appendChild(newText);
    // Append new button input to the newDiv
    newDiv.appendChild(newDelButton);
    // Append newDiv input to the mainContainer div
    mainContainer.appendChild(newDiv);
    counter3++;
    //i++;

    // Add a handler to button for deleting the newDiv from the mainContainer
    newDelButton.onclick = function() {
        mainContainer.removeChild(newDiv);
        counter3--;
    }
}
</script>

我在这里有一个表格。

<form method="post" action="">
    <input type = "text" name="Author[]" />
    <input type="button" value="+" id="Authorbutton" onclick="addAuthor()" />
    <input type="submit" value="submit" name="submit">
    </form>

我使用php和mysql查询将数据保存到数据库中。代码为..

<?php

if(isset($_POST['submit']))
{
$authors = $_POST['Author'];
foreach($authors as $author) 
{
    $sql=mysql_query("INSERT INTO test (number, type) VALUES ('{$_POST['type']}','{$author}')");

    if (!$sql){
       die('Error: ' . mysql_error());
    }
//endforeach;

}
}

?>

但是问题是数据没有正确存储在数据库中...

它仅将第一个字段保存在数据库中,而不保存第二,第三,第四等等。

帮我 ........

您的查询应为

$sql=mysql_query("INSERT INTO test (number, type) VALUES ('$_POST['type']','$author')");

并且在您的视图文件中应该是

 <input type = "text" name="Author" />

Just会建议执行一个print_r($_REQUEST)并检查您在请求中获得的所有数据。 此外,Plus还希望您签入Firebug / Chrome检查工具,以检查是否将元素正确添加到浏览器中。 因为看代码,它似乎无法正常运行。 这里缺少的唯一一件事是ID-Auther,而您通过id本身却失败了。 另外,还尝试使用该修订/补丁运行相同的文件,但无法运行以检查输出。 因此,您希望签入作为您在请求中获得的确切值是多少。 如果表单提交仅将单个数据传递回服务器,那么获取响应/期望的结果总是很麻烦。

尝试这个,

if(isset($_POST['submit']))
{
$authors = $_POST['Author'];
$valuesToInsert =   1;
for($i=0;$i<count($authors);$i++) 
{   $number = $i +1;
    $valuesToInsert .=   "($number,$authors[$i]),";

}
$valuesToInsert =   trim($valuesToInsert,',');
$sql=mysql_query("INSERT INTO test (number, type) VALUES ".$valuesToInsert);    
    if (!$sql){
       die('Error: ' . mysql_error());
    }
//endforeach;

}


?>

您正在尝试提交两个具有相同名称的独立数组。 每次添加作者时,您将获得

$_POST['Author'][1] = 'foo';

代替

$_POST['Author'][1] = 'foo';
$_POST['Author'][2] = 'bar';

这就是您所期望的。 您应该尝试使输入已经具有索引,即

<input type = "text" name="Author[1]" />
<input type = "text" name="Author[2]" />

等等。您可以使用counter3 JS变量逐渐增加输入索引,并执行类似的操作

newText.name = "Author[" + counter3 + "]";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM