我的系统有问题。我需要乘以文本框,然后使用 mysql 中的串联插入一列。我在我的系统中使用 Php。具体来说,我的源代码是 ff:
这是索引
<html>
<head>
<title>Sample Text</title>
</head>
<body>
<form action="phpfunctionshandle.php" method="post" name="phpfunctionshandle">
<input type="number" name="text" />
<button type="submit" name="submit"value="Register">Submit</button>
<button type="reset"value="cancel">Cancel</button>
<input type="hidden" name="submitted" value="TRUE" >
</form>
</body>
</html>
这是索引的句柄
<html>
<head>
<title>Sample</title>
</head>
<body>
<form action="handlefinal" method="post" enctype="multipart/form-data" name="phpfunctionshandle">
<?php
include('config.php');
$text =$_POST['text'];
for ($x = 1; $x <= $text; $x++) { // I use for loop to multiple text box
echo '<input type="text" name="sample value=""/>';
}
?>
<button type="submit" name="submit"value="Register" class="btn btn-primary">Submit</button>
<button type="reset"value="cancel" class="btn btn-success">Cancel</button>
<input type="hidden" name="submitted" value="TRUE" >
</form>
<Form name="handlefinal">
<?php
include('config.php');
$sample=$_POST['sample"'.$text.'"'];
$query = "INSERT INTO sample (text) VALUES ('$sample')";
$result = mysql_query($query) or die(mysql_error());
echo'success';
?>
</form>
</body>
</html>
我使用 for 循环根据我在索引上输入的内容对文本框进行倍增。比如我在index.php中的输入类型数字中输入3,文本框就会乘以3。我想要的结果是查询乘以3的文本框。 如果我在第一个文本框中输入 sample1,在第二个文本框中输入 sample2,在第三个文本框中输入 sample3。列文本中数据库中所需的输出应该是 示例 1、示例 2、示例 3。我试了一下,但结果是文本框的最后一个值只插入到数据库中。总之只有 sample3。我不知道如何查询以及如何解决这个问题。有什么建议么?很抱歉发布了我所有的源代码。感谢您的耐心等待。
最佳答案
这里有几个问题:
- 您为所有输入赋予了相同的名称,因此只有最后一个输入的值会传送到服务器。您可以使用表单中的数组解决此问题,该数组将转换为您的
$_POST
的数组。变量:
echo '<input type="text" name="sample[]" />';
- 我不喜欢将多个文本输入放在一个数据库字段中,但如果必须,可以在从表单中获取值时将它们连接起来:
$samples = implode(',', $_POST['sample']); // separated by comma
- 你不应该使用
enctype="multipart/form-data"
如果您不发布任何文件。 - 你有一个sql注入(inject)问题。您应该切换到另一个数据库接口(interface)作为
mysql_*
函数已弃用并使用准备好的语句。
关于php - 如何生成多个文本框然后插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29327349/