数据插入到第一个表中,但是获取 ID 号的代码似乎不起作用,并且数据没有插入到接下来的两个表中。代码运行并出现感谢消息,感谢此人提交其详细信息。
一共有三页代码。连接代码在一个文件中。处理代码文件和表单文件。
我不会在此处包含连接代码,因为它有效。
这是表单代码:
enter code here
<form method="post" action="formprocess3.php">
<table>
<tr>
<td>Customer Details</td>
<td>Appointment Preference</td>
<td>Cupcake Details</td>
</tr>
<tr>
<td>First Name
<input name="FirstName" type="text" id="FirstName" maxlength="20" value="<?php if (isset($_POST['FirstName'])) echo $_POST ['FirstName']; ?>"/>
</td>
<td>Appointment Date
<input name="AppointmentDate" type="date" id="AppointmentDate" maxlength="10" value="<?php if (isset($_POST['AppointmentDate'])) echo $_POST['AppointmentDate']; ?>"/>
</td>
<td>Size
<select name="CupcakeSize" id="CupcakeSize" type="radio" maxlength="5" value="<?php if (isset($_POST['CupcakeSize'])) echo $_POST['CupcakeSize']; ?>"/>
<option></option>
<option>Small</option>
<option>Large</option>
</select></td>
</tr>
<tr>
<td>Surname
<input name="Surname" type="text" id="Surname" maxlength="20" value="<?php if (isset($_POST['Surame'])) echo $_POST['Surname']; ?>"/></td>
<td>Appointment Time
<select name="AppointmentTime" type="radio" maxlength="20" value="<?php if (isset($_POST['AppointmentTime'])) echo $_POST ['AppointmentTime']; ?>"/>
<option></option>
<option>9.30am -10.30am</option>
<option>11am - 12pm</option>
<option>1.30pm - 2.30pm</option>
<option>3pm - 4pm</option>
<option>4.30pm - 5.30pm</option>
<option>7pm - 8pm</option>
</select>
</td>
<td>Quantity
<input type="text" name="Quantity" id="Quantity"/></td>
</tr>
<tr>
<td>Email address
<input name="EmailAddress" type="email" id="Email" maxlength="20" value="<?php if (isset($_POST['EmailAddress'])) echo $_POST['EmailAddress']; ?>"/></td>
<td>Taster
<input name="Taster" type="checkbox" id="Taster"/>
</td>
<td maxlength="1" type="radio" value="<?php if (isset($_POST['Taster'])) echo $_POST['Taster']; ?>"/>
<td>Frosting
<select name="CupcakeFrosting" id="CupcakeFrosting" type="radio" maxlength="10" value="<?php if (isset($_POST['CupcakeFrosting'])) echo $_POST['CupcakeFrosting']; ?>"/>
<option></option>
<option>Strawberry</option>
<option>Chocolate</option>
<option>Vanilla</option>
<option>Coffee</option>
<option>Orange</option>
<option>Blue</option>
<option>Pink</option>
<option>Green</option>
<option>Red</option>
<option>Purple</option>
</select></td>
</tr>
<tr>
<td>Postcode
<input name="Postcode" type="text" id="Postcode" style="width: 130px; height: 20px" class="auto-style24" maxlength="10" value="<?php if (isset($_POST['Postcode'])) echo $_POST['Postcode']; ?>"/></td>
<td>Cake wanted by
<input name="CakeWantedBy" type="date" id="CakeWantedBy" maxlength="10" value="<?php if (isset($_POST['CakeWantedBy'])) echo $_POST['CakeWantedBy']; ?>"/>
</td>
<td>
<select name="CupcakeFlavour" id="Flavour" type="radio" maxlength="10" value="<?php if (isset($_POST['CupcakeFlavour'])) echo $_POST['CupcakeFlavour']; ?>"/>
<option></option>
<option>Banana</option>
<option>Caramel</option>
<option>Carrot</option>
<option>Chocolate</option>
<option>Vanilla</option>
<option>Red Velvet</option>
<option>Oreo</option>
<option>Coffee</option>
<option>Decide with taster £20</option>
</select></td>
</tr>
<tr>
<td>
<input name="MobileNumber" type="text" id="MobileNumber" maxlength="20" value="<?php if (isset($_POST['MobileNumber'])) echo $_POST['MobileNumber']; ?>"/>
</td>
<td>
<span class="auto-style24">Occasion
<select name="Occasion" type="radio" id="Occasion" maxlength="20" value="<?php if (isset($_POST['Occassion'])) echo $_POST['Occassion']; ?>"/>
<option></option>
<option>New baby</option>
<option>Birthday</option>
<option>Wedding</option>
<option>New Job</option>
<option>Christmas</option>
<option>Easter</option>
<option>Valentines</option>
<option>Congratulations</option>
<option>Anniversary</option>
<option>Other</option>
</select></td>
</tr>
</table>
</form>
将表单数据插入三个数据库表的代码:
<html>
<head>
<title>Form Process Message</title>
</head><body>
<?php #
// This script performs an INSERT query to add a record to the users table.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// open the database...
require ('mysqli_connect.php');
// Make the query:
// Customer details
$t = $_POST[Title];
$fn = $_POST[FirstName];
$sn = $_POST[Surname];
$e = $_POST[EmailAddress];
$ht = $_POST[HomeTelephone];
$mn = $_POST[MobileNumber];
$hn = $_POST[HouseNumberName];
$s = $_POST[Street];
$tw = $_POST[Town];
$c = $_POST[County];
$pc = $_POST[Postcode];
// Cake details
$ct = $POST[CupcakeType];
$cn = $_POST[CupcakeNumber];
$cf = $_POST[CupcakeFrosting];
$o = $_POST[Occassion];
// Preferred Appointment
$ad = $_POST[AppointmentDate];
$at = $_POST[AppointmentTime];
$ta = $_POST[Taster];
$cwb = $_POST[CakeWantedBy];
$q = "INSERT INTO customerdetails(Title, FirstName, Surname, EmailAddress, HomeTelephone, MobileNumber, HouseNumberName, Street, Town, County, Postcode) VALUES ('$t','$fn', '$sn', '$e', '$ht', '$mn', '$hn', '$s', '$tw', '$c', '$pc')";
//execute query
$r = @mysqli_query ($dbc, $q);
//get customer id for preferred appointment
$ci = my_sqli_insert_id($dbc);
$q1 = "INSERT INTO cakedetail(CupcakeType, CupcakeNumber, CupcakeFrosting, Occassion) VALUES ('$ct','$cn', '$cf', '$o')";
//execute query
$r1 = @mysqli_query ($dbc, $q1);
//get cakedetail id for preferred appointment
$cdi = my_sqli_insert_id($dbc);
$q2 = "INSERT INTO preferredappointment(AppointmentDate, AppoitmentTime, Taster, CakeWantedBy, EmailAddress) VALUES ($ci, $cdi, '$ad','$at', '$ta', '$cwb', '$e')";
//execute query
$r2 = @mysqli_query ($dbc, $q2);
// Run the query.
if ($r) {
// If it ran OK.
// Print a message:
echo '<h1>Thank you!
<br />
Your request is now registered.
<br />
<a href="gallery.html">Back to the Gallery page</a></h1>';
}
else {
// If it did not run OK.
// Public message:
echo '<h1>System Error</h1>
<p class="error">You could not be registered due to a system error. We apologise for any inconvenience.</p>
<a href="gallery.html">Back to the Gallery page</a>';
// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />
Query: ' . $q . '</p>';
}
//close the dbc
mysqli_close($dbc);
}
?>
</body>
</html>
共有三个数据库表,分别称为 cakeorder、customerdetails 和首选约会。我不认为多表插入适用于早期版本的 PHP,这是我开始使用的,但我现在使用的是 xampp 5.5.24 和 PHP 5.5.24。我去掉了大部分 html 的格式,所以我可能在这里的某个地方留下了一个悬挂标签,但实际网页上没有。我对PHP不是很精通,所以很多都是通过浏览这个网站来整理的。任何帮助将不胜感激。谢谢