gpt4 book ai didi

php - 如何使用 XAMPP 从我的表单中获取信息以插入到 mySQL 数据库中?

转载 作者:行者123 更新时间:2023-11-30 21:31:37 27 4
gpt4 key购买 nike

到目前为止,我已经让我的 PHP 在输入表单数据后将其输出到另一个页面。提交后,我需要将数据插入名为“megatravel”的 mySQL 数据库中。我收到错误,即“表‘megatravel.reservations’不存在”和另一个“警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,bool 在 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();

?>

最佳答案

您只需要创建一次数据库。

错误消息告诉您需要创建预订表。

CREATE TABLE reservations (
name VARCHAR(250),
email varchar(100)
)

它有助于使用 SQL 实用程序来管理数据库。并注意 SQL 抛出的任何错误消息。

关于php - 如何使用 XAMPP 从我的表单中获取信息以插入到 mySQL 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55885503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com