我做了一个按钮,每次单击都会创建一个名称为= 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了,所以我不记得那个上的确切语法。