gpt4 book ai didi

php - 存储动态创建的文本字段的输入

转载 作者:行者123 更新时间:2023-11-30 01:30:52 24 4
gpt4 key购买 nike

我制作了一个按钮,每次单击时都会创建一个名称为 1,2,3 ... 的文本。我想将这些文本字段的所有输入存储在数据库中。

<?php 
$con = mysqli_connect("localhost", "root","", "abc");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$maxoptions = 10;

// I don't want only 10 inputs from text fields
// but as many as the user creates and fills
for ($i = 1; $i < $maxoptions; $i++) {
$sql="INSERT INTO qa (q, a$i)
VALUES
('$_POST[q1]', '$_POST[i]')";
// '$_POST[i]' is not working
}

if (!mysqli_query($con, $sql))
{
die('Error: ' . mysqli_error($con));
}

mysqli_close($con);

?>

现在,如何使用这些文本字段在数据库中动态创建列?

这是我用来创建文本字段的 JavaScript 代码:

var intTextBox1 = 0;
//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement1()
{
intTextBox1 = intTextBox1 + 1;
var contentID = document.getElementById('content1');
var newTBDiv = document.createElement('div');
newTBDiv.setAttribute('id','strText'+intTextBox1);
newTBDiv.innerHTML = "Option" + intTextBox1 +
": <input type='text' id='" + intTextBox1 +
"' name='" + intTextBox1 + "'/>";
contentID.appendChild(newTBDiv);
}

//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement1()
{
if (intTextBox1 != 0)
{
var contentID = document.getElementById('content1');
contentID.removeChild(document.getElementById('strText'+intTextBox1));
intTextBox1 = intTextBox1 - 1;
}
}

这是按钮的代码:

<form id="s1form" name="s1form" method="post" action="qno1.php">
<input type="text" name="q1">
<input type="button" value="Add a choice" onClick="javascript:addElement1();" />
<input type="button" value="Remove a choice" onClick="javascript:removeElement1();" />
<div id="content1"></div>

最佳答案

这是我的 2 美分:首先从回显文本字段和按钮开始

<?php
$columns=10; //we'll start off with 10
for($i=0; $i<$columns; $i++){
echo "<input type=\"text\" id=\"$i\" name=\"$field_i\">";
}
//the placeholder for the next element
echo "<div id=\"newfield\"></div>";
//and the buttons
echo "<input type=\"button\" value=\"Add Field\" onclick=\"addfield()\">";
echo "<input type=\"button\" value=\"Remove Field\" onclick=\"removefield()\">";

接下来继续执行 JS 脚本

<script type="text/javascript">
<?php echo "fields=".$columns-1 .";"; /*from before, mind the off-by-one*/ ?>
function addfield(){
elm=document.getElementById("newfield");
//construct the code for new field
nf="<input type=\"text\" name=\"field_"+ fields +"\">";
nf+="<div id=\"newfield\"></div>"; //placeholder for next field
elm.innerHTML=nf;
}

function removefield(){
(elem=document.getElementById(fields)).parentNode.removeChild(elem);
fields--;
}
</script>

我找到了删除元素的代码in this answer .

我对使用 + 进行连接有一些保留,如果遇到任何问题,请使用 .append() 代替。

现在要检查您的结果(因为我没有使用数组进行 GET 请求),我们做了一些修改:

//php
$i=0;
while(isset($_GET["field_".$i])){
$new_cols[$i]=$_GET["field_".$i];
$i++;
}
addColumns($new_cols)

其中 addColumns() 只是 adds new columns to the database有时我发现 isset() 有点喜怒无常,如果它不能解决问题,那就 $_GET["field_".$i]!==false

用于创建新列的 SQL 代码非常简单,而且只是一个 PHP 循环,因此我不会在这里编写该函数。希望有帮助。

编辑:您可以通过两种方式执行添加列功能:

首先MySQL代码如下:

ALTER TABLE Persons
ADD DateOfBirth date

其中 DateOfBirth 是列的名称,date 是其数据类型。因此,使用从前面的代码获取的列名数组,一种方法是顺序执行查询:

addColumns($names){
$sql="ALTER TABLE (your table) ADD ";
for($i=0; $i<count($names); $i++){
if(sanitize($names[$i])===$names[$i])
mysqli_query($sql.sanitize($names[$i])." (datatype)");
else{
//something fishy is going on, report the error
die("error");
}
}
}

其中 sanitize() 是一个正确的 SQL 输入清理函数。请注意,我不仅仅转义输入,还会中止以防转义字符串与原始字符串不匹配

第二种方法是连接单个查询中的所有列。两者都尝试一下,看看效果如何。为了做到这一点,我将修改上面的 for 循环

$sql="ALTER TABLE (your table) ";
for($i=0; $i<count($names); $i++){
if(sanitize($names[$i])===$names[$i])
$sql.="ADD ".$names[$i]." (datatype),"; //notice the comma
else{
//something fishy is going on, report the error
}
}
//remove the comma from the last concatenation. There might be an off-by-one in this,
//depends if strlen also counts the NULL character at the end
$sql[strlen($sql)]='\0';
//execute the query
mysqli_query($sql);

请注意,您可能需要将列名用奇怪的字符(例如 ` 或 ' )括起来。我有一段时间没有使用 MySQL,所以我不记得它的确切语法。

关于php - 存储动态创建的文本字段的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17478681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com