php 查询导致响应发布为三行,而不是每个用户一行

标签 php mysql

我正在创建我的第一个数据库,并使用一个表来包含 php/mysql 调查中的所有响应。然而,响应将发布到一张表中,但位于三个不同的行中。我怀疑这与针对三部分响应执行 3 次查询有关。我需要连接这个吗?如果需要,如何连接?还有其他解决方案吗?

这是 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:&nbsp;<input type="text" name="First_Name"/></br>
</br>
Last Name:&nbsp;<input type="text" name="Last_Name"/></br>
</br>
E-mail:&nbsp;<input type="text" 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="20-25"/>&nbsp;20-25</br>
<input type="checkbox" name="age[]" id="26-30" value="26-30"/>&nbsp;26-30</br>
<input type="checkbox" name="age[]" id="31-35" value="31-35"/>&nbsp;31-35</br>
</div>
<div id="checkboxes">
</div>

<!--This section is the trips take checkbox area-->
<div id="tripstodatetype">
<p><u><b>WHAT TYPE OF TRIPS TO DATE HAVE YOU TAKEN?</b></u></p>
<input type="checkbox" name="trip2date[]" id="Bus" value="Bus">&nbsp;Bus&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br>
<input type="checkbox" name="trip2date[]" id="Car" value="Car">&nbsp;Car</br>
<input type="checkbox" name="trip2date[]" id="Weekend fly-in" value="Weekend fly-in">&nbsp;Weekend fly-in&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br>
</div>
<div id="tripstodateborder">
</div>

<!--This section is the type of trip client likes best checkbox area-->
    <div id="triplikebest">
<p><u><b>WHAT TYPE OF TRIP DO YOU LIKE BEST?</b></u></p>
<input type="checkbox" name="triplikebest[]" value="Bus">&nbsp;Bus&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br>
<input type="checkbox" name="triplikebest[]" value="Car">&nbsp;Car</br>
<input type="checkbox" name="triplikebest[]" value="Weekend fly-in">&nbsp;Weekend fly-in&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br>
</div>
<div id="triplikeborder">
</div>

以及相应的PHP:

<html>
<?php
include 'head.php';
include 'config/menu.php'; 
$host="localhost";
$username="someusername";
$password="somepass";
$dbname="somedb";

$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
    die('Error connecting to MySQL server' . mysql_error());
    }
mysql_select_db($dbname, $dbc);

//send user data to the 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 data to the database table
$age = $_POST['age'];
$my_range = "";
foreach($age as $range) 
$my_range = $my_range . $range . " ";
mysql_query("INSERT INTO pax(age) VALUES ('$my_range')") or die (mysql_error()); 

//send trip to date data to the database table
$trip2date = $_POST['trip2date'];
$my_triprange = "";
foreach($trip2date as $triprange) 
$my_triprange = $my_triprange . $triprange . ", ";
mysql_query("INSERT INTO pax(trip2date) VALUES ('$my_triprange')") or die (mysql_error()); 


mysql_close($dbc);
?>

非常感谢您的帮助。

最佳答案

...您正在运行 3 个 INSERT 查询。我认为你严重误解了你想要实现的目标。您可能想要连接“trip2date”参数的值,或者最好在另一个表中插入多个记录(查看基本关系数据库用法)。

此外,为什么用户可以选择多个年龄范围?一个人怎么可能同时是20-25岁和26-30岁?您可能需要一个 radio 组,这样他们只能选择一个选项。 (同样,您也可能在那里插入多行;如果用户勾选了所有三个年龄范围(您允许他们这样做,没有明显的原因),那么您将插入包含他们的姓名和电子邮件的一行,然后插入三行仅包含其年龄的行。)

还有,神圣的 SQL 注入(inject), bat 侠。请参阅http://bobby-tables.com/

我假设您仍在学习,但要学会在担心其他事情之前不要先编写不安全的代码。

关于php 查询导致响应发布为三行,而不是每个用户一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12355408/

相关文章:

mysql 'order by' 阿拉伯字母问题

mysql - 在 MySql 中执行查询时与 only_full_group_by 相关的错误

javascript - 使用 PHP/JS/MYSQL 恢复 ID 或值

php - 剥离 "Transfer"和 "Payout"问题 "No such destination"PHP

php - 证书存储可以放在 application.ini 而不是 php.ini 中吗

php - 似乎无法连接到mysql、localhost?

php - PHP 和 SQL 中日期的转换

PHP - 从 mysql 数据库获取用户名字 (session.php)

php - 从 PHP 读取 Git 提交消息

php - 显示总人口变化并按上次人口变化时间排序