这是我的代码:您将自动了解它的作用
<form method="post">
Enter Your Numbers
<input type="number" name="number" min="1" max="10" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
if(isset($_POST['submit']))
{
$number = $_POST['number'];
for ($x=0; $x<=$number; $x++) {
echo "<input type=\"text\" name=\"tbno".$x."\">";
}
echo "<input type=\"submit\" value=\"submit\" name=\"submit1\">";
}
if(isset($_POST['submit1']))
{
$link = mysql_connect("localhost", "root", "");
$db = mysql_select_db("bookabook");
}
?>
以下是我的问题:我想将文本框值存储在数据库中,但我不知道如何执行此操作。
if(isset($_POST['submit1']))
{
$link = mysql_connect("localhost", "root", "");
$db = mysql_select_db("bookabook");
//WHAT SHOULD I WRITE HERE
}
有人可以告诉我吗?
答案 0 :(得分:1)
# HTML
echo "<input type='text' name='tbno[]' value='$x'>";
# PHP
$array = $_POST[tbno];
foreach ($array as $key => $value) {
echo "$key = $value<br/>";
}
答案 1 :(得分:0)
而不是使用计数器$x
- &gt; name=\"tbno".$x."\"
,只需将输入设置为数组 - &gt; name=\"tbno[]\"
。然后,在表单提交上,您可以使用foreach
或for
循环轻松遍历它们。
<?php
if(isset($_POST['tbno']))
{
//your db connection
foreach($_POST['tbno'] as $tbno){
//INSERT INTO tbl VALUE $tbno
}
echo 'INSERTED DATA';
}
?>
<form method="post">
<?php
if(isset($_POST['number']))
{
$number = $_POST['number'];
for ($x=0; $x<$number; $x++) {
echo "<input type=\"text\" name=\"tbno[]\">";
}
}
else
{ ?>
Enter Your Numbers<input type="number" name="number" min="1" max="10" />
<?php
} ?>
<input type="submit" name="submit" value="submit" />
</form>
我将你的2个表单浓缩为1,这是改变输入的唯一因素,因此代码更少。
答案 2 :(得分:-1)
我认为您正在寻找的是插入声明。我不确定我理解代码,但我理解目标。您需要做的就是在for循环中创建一个插入(如下所示)语句,循环遍历书籍等。
$number = trim($_POST['number']);
$numberOfBooks = mysqli_real_escape_string($mysqlDatabseConnectionObject,$numberOfBooks); //variable to be inserted in the database
$insertQuery = sprintf("INSERT INTO <table_name> VALUES('%s'",$numberOfBooks)); //statement to be executed
$insertResults = $mysqlDatabseConnectionObject->query($insertQuery); //execute the query
print_r($insertResults); //printing the query execution results
答案 3 :(得分:-1)
if(isset($_POST['submit1']))
{
$link = mysql_connect("localhost", "root", "");
$db = mysql_select_db("bookabook");
$values = "";
foreach($_POST as $key => $inputValue){
$values.="(".$inputValue."),";
}
$SQL_string = "INSERT INTO mitabla(micampo) VALUES $values";
mysql_query($SQL_string,$link);
}