【发布时间】:2023-03-19 16:21:02
【问题描述】:
我有一个包含多个不同文本和复选框字段的 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 以此类推。
【问题讨论】:
-
你有一个 SQL 注入漏洞。
-
您的输入毫无意义 - ID 不会在 httpd 请求中发送,因此您将得到的只是一系列
1值,而不是 20-25 类型的值。例如如果选中任何复选框,则您发送的值意味着所有复选框都已选中。