到目前为止,一旦输入数据,我就已经将PHP的表单数据输出到另一个页面。提交后,我需要将数据插入名为“ megatravel”的mySQL数据库中。我收到错误消息,即“表'megatravel.reservations'不存在”,而另一个错误消息“警告:mysqli_fetch_assoc()期望参数1为mysqli_result,在C:\ xampp1 \ htdocs \ JordanMahsman-MegaTravel2 \ contactform.php中给出布尔值在第111行”
我正在使用XAMPP,并且去过localhost / phpmyadmin创建数据库“ megatravel”和创建表“ reservations”,但是我遇到了错误,需要帮助。
<?php
$required = array('name', 'email', 'phone', 'adultsNo', 'children', 'city', 'activity', 'date');
$error = false;
foreach($required as $field) {
if (!isset($_POST[$field])) {
$error = true;
}
}
if(!$error){
if(!(is_numeric($_POST['phone']) && is_numeric($_POST['adultsNo']) && is_numeric($_POST[
'children']))){
$error=true;
echo "Numeric entries are required for Phone Number, No.of Adults, and No.of Children<br>";
}
}
if(!$error){
if(!validateEmail($_POST['email']))
{
$error=true;
echo "Not a valid Email Address. Please try again.<br>";
}
}
if(!$error){
if(!validateDate($_POST['traveldate'])){
$error=true;
echo "Not a valid date. Please try again.<br>";
}
}
if (!$error) {
echo "All fields are required.</br>";
} else {
echo "Hello, a client has submitted their information for you to review! <br>";
echo "<br>Name: ".$_POST['name']."
<br></br>
Email: ".$_POST['email']."
<br></br>
Phone Number: ".$_POST['phone']."
<br></br>
No. of Adults: ".$_POST['adultsNo']."
<br></br>
No. of Children: ".$_POST['children']."
<br></br>
City: ".$_POST['city']."
<br></br>
Activity: ".$_POST['activity']."
<br></br>
Date: ".$_POST['traveldate'];
}
function validateEmail($email) {
// Validate email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
return 1;
} else {
return 0;
}
}
function validateDate($traveldate, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $traveldate);
return $d && $d->format($format) === $traveldate;
}
// DATABASE CREATION AND INSERTION
$servername = "localhost";
$username = 'root';
$password = '';
$dbname = 'megatravel';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Failed to Connect: " . $conn->connect_error);
}
$sql = "CREATE DATABASE megatravel";
if ($conn->query($sql) === TRUE) {
echo "Database created!";
} else {
echo "A problem occurred creating your database. Try again!: " . $conn->error;
}
$sql = "INSERT INTO reservations (name, email, phone, adultsno, children, city, activity, date)
VALUES ('{$_POST['name']}', '{$_POST['email']}','{$_POST['phone']}','{$_POST['adultsNo']}', '{$_POST['children']}',
'{$_POST['city']}', '{$_POST['activity']}', '{$_POST['traveldate']}')";
if ($conn->query($sql) === TRUE) {
echo "<br><br>Success!<br>";
} else {
echo "<br>Error: " . $sql . "<br>" . $conn->error;
}
$sql = "SELECT * FROM reservations";
$result = mysqli_query($conn, $sql);
echo "<br>";
echo "<table border='1'>";
echo "<tr>";
echo "<td style='text-align: center; font-weight: bold'>Name</td>","<td style='text-align: center; font-weight: bold'>Email</td>","<td style='text-align: center; font-weight: bold'>Phone</td>","<td style='text-align: center; font-weight: bold'>Adults</td>","<td style='text-align: center; font-weight: bold'>Children</td>","<td style='text-align: center; font-weight: bold'>City</td>","<td style='text-align: center; font-weight: bold'>Activity</td>","<td style='text-align: center; font-weight: bold'>Date</td>";
echo "</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
$conn->close();
?>
答案 0 :(得分:0)
您只需创建一次数据库。
错误消息告诉您需要创建预订表。
CREATE TABLE reservations (
name VARCHAR(250),
email varchar(100)
)
使用SQL实用程序来管理数据库很有帮助。并注意SQL抛出的任何错误消息。