我有一个包含多个文本和复选框字段的HTML表单。我将文本字段发布到正确的表格,但复选框响应根本没有发布(打算发布到单独的表格)。
这是我的HTML表单:
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\paxdb\head.php';
include 'config/menu.php';?>
<div id="dataentry">
<!--This section is the demographic text field area-->
<form method="post" action="dataentered.php">
First Name: <input type="text" id="First_Name" name="First_Name"/></br>
</br>
Last Name: <input type="text" id="Last_Name" name="Last_Name"/></br>
</br>
E-mail: <input type="text" id="email" name="email"/></br>
</br>
<!--This section is the age range checkbox selection area-->
<p><u><b>Age Range</b></u></p>
<input type="checkbox" name="age[]" id="20-25" value="1"/> 20-25</br>
<input type="checkbox" name="age[]" id="26-30" value="1"/> 26-30</br>
<input type="checkbox" name="age[]" id="31-35" value="1"/> 31-35</br>
<input type="checkbox" name="age[]" id="36-40" value="1"/> 36-40</br>
<input type="checkbox" name="age[]" id="41-45" value="1"/> 41-45</br>
</div>
<p><u><b>What City or region would you like to visit in the US or Canada?:</b></u></p>
<textarea name="comment2" rows="4" cols="50"></textarea>
<?php include 'footer.php';?>
</div>
</body>
</html>
,这是我正在尝试的PHP代码,但只有文本字段有效:
<html>
<?php
$host="localhost";
$username="someusername";
$password="somepassword";
$dbname="somedbname";
$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
die('Error connecting to MySQL server' . mysql_error());
}
mysql_select_db($dbname, $dbc);
//send pax data to pax database table
$first_name=$_POST['First_Name'];
$last_name=$_POST['Last_Name'];
$email=$_POST['email'];
mysql_query("INSERT INTO pax (First_Name, Last_Name, email)
VALUES('$first_name','$last_name','$email')");
//send age checkbox data to age database table
$age = $_POST['age'];
foreach($age as $range) mysql_query("INSERT INTO age ($age) VALUES ('$range')") or die (mysql_error());
mysql_close($dbc);
感谢任何帮助。
编辑澄清: mysql表'age'包含以下字段: age_id(关键), pax_id(表单开头的文本字段数据的索引), 20-25 26-30 通过年龄段等等。
答案 0 :(得分:1)
在尝试实际运行mysql查询之前,您应该进行测试以确保您构建的查询中包含正确的信息。
你可以做这样的事情来快速检查:
foreach($age as $range) {
print "INSERT INTO age ($age) VALUES ('$range')";
}
此外,您不应该将$_POST
数据直接插入查询中,有人可以轻松地将SQL代码写入字段并删除数据库。查找“mysql输入卫生”主题
答案 1 :(得分:0)
我已经为你编辑了代码
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\paxdb\head.php';
include 'config/menu.php';?>
<div id="dataentry">
<!--This section is the demographic text field area-->
<form method="post" action="dataentered.php">
First Name: <input type="text" id="First_Name" name="First_Name"/></br>
</br>
Last Name: <input type="text" id="Last_Name" name="Last_Name"/></br>
</br>
E-mail: <input type="text" id="email" name="email"/></br>
</br>
<!--This section is the age range checkbox selection area-->
<p><u><b>Age Range</b></u></p>
<!-- Change your checkbox value -->
<input type="checkbox" name="age[]" id="20-25" value="20-25"/> 20-25</br>
<input type="checkbox" name="age[]" id="26-30" value="26-30"/> 26-30</br>
<input type="checkbox" name="age[]" id="31-35" value="31-35"/> 31-35</br>
<input type="checkbox" name="age[]" id="36-40" value="36-40"/> 36-40</br>
<input type="checkbox" name="age[]" id="41-45" value="41-45"/> 41-45</br>
</div>
<p><u><b>What City or region would you like to visit in the US or Canada?:</b></u></p>
<textarea name="comment2" rows="4" cols="50"></textarea><br/>
<input type="submit" name="submit" value="Submit"/>
<?php include 'footer.php';?>
</div>
</body>
</html>
你不能像在INSERT语句中那样直接将数组($ age)插入到数据库中...我做的一个解决方案是将数组转换为字符串,然后将其插入到数据库中,如图所示......
<?php
$host="localhost";
$username="someusername";
$password="somepassword";
$dbname="somedbname";
$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
die('Error connecting to MySQL server' . mysql_error());
}
mysql_select_db($dbname, $dbc);
//send pax data to pax database table
$first_name=$_POST['First_Name'];
$last_name=$_POST['Last_Name'];
$email=$_POST['email'];
mysql_query("INSERT INTO pax (First_Name, Last_Name, email)
VALUES('$first_name','$last_name','$email')");
//send age checkbox data to age database table
$age = $_POST['age'];
$my_range = "";
foreach($age as $range)
$my_range = $my_range . $range . " ";
//You have written this query wrong
mysql_query("INSERT INTO age(age) VALUES ('$my_range')") or die (mysql_error());
mysql_close($dbc);
从数据库中检索它时,您可以使用expode()来获取每个年龄段...以下是示例代码
$range_string = "20-25 26-30 31-35";
$range_array = explode(" ", $range_string);
echo $range_array [0]; // 20-25
echo $range_array [1]; // 26-30
echo $range_array [2]; // 31-35
您可能还想查看http://www.html-form-guide.com/php-form/php-form-checkbox.html有关复选框的一些信息......
希望这有帮助