的HTML:我如何做一个PHP插入两个或多个独立的MySQL表
<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" name="First_Name"/></br>
</br>
Last Name: <input type="text" name="Last_Name"/></br>
</br>
E-mail: <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"/> 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>
</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"> Bus </br>
<input type="checkbox" name="trip2date[]" id="Car" value="Car"> Car</br>
<input type="checkbox" name="trip2date[]" id="Weekend fly-in" value="Weekend fly-in"> Weekend fly-in </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"> Bus </br>
<input type="checkbox" name="triplikebest[]" value="Car"> Car</br>
<input type="checkbox" name="triplikebest[]" value="Weekend fly-in"> Weekend fly-in </br>
</div>
<div id="triplikeborder">
</div>
和PHP:
<html>
<?php
include 'head.php';
include 'config/menu.php';
$host="localhost";
$username="somename";
$password="somepass";
$dbname="pax";
$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
die('Error connecting to MySQL server' . mysql_error());
}
mysql_select_db($dbname, $dbc);
$first_name = mysql_real_escape_string($_POST['First_Name']);
$last_name = mysql_real_escape_string($_POST['Last_Name']);
$email = mysql_real_escape_string($_POST['email']);
$age = $_POST['age'];
$my_range = "";
foreach($age as $range) {
$my_range = $my_range . mysql_real_escape_string($range) . ", ";
}
$trip2date = $_POST['trip2date'];
$my_triprange = "";
foreach($trip2date as $triprange) {
$my_triprange = $my_triprange . mysql_real_escape_string($triprange) . ", ";
}
mysql_query("INSERT INTO `pax` (`First_Name`, `Last_Name`, `email`, `age`,`trip2date`)
VALUES('$first_name','$last_name','$email', '$my_range','$my_triprange')")
or die(mysql_error());
mysql_close($dbc);
?>
<div class = "entered">
<p>Success! Your Data Has Been Submitted. Please click on <b>'DATA ENTRY'</b> above to enter another. </P>
</div>
<?php include 'footer.php';?>
</div>
</div>
</body>
</html>
如果我把triprange数据成一个单独的表,我将如何转换INSERT查询来执行插入到新表中? (假设新的/第二个表被称为“旅行”)。 - 或者 - 在此处使用第二个INSERT查询更有意义吗?如果是这样,它应该如何保持连接到第一个表/ ID
谢谢你提前。
你的编辑把你在正确的轨道上......你想要一个用户信息和车次之间有许多relatinoship 。这需要在两者之间共享的索引。 (每个用户的唯一ID,并将该ID添加到旅行表中以供参考) – Dave
好吧,如果我要将此“多个”答案数据放入第二个表中,我将如何调整插入查询以包含输入的数据到第二个表中,还是仅仅编写第二个插入查询更有意义? – shane