So far, I've gotten my PHP to output my form data to another page once it has been input. After it has been submitted, I need the data to be inserted into a mySQL database titled 'megatravel'. I am getting errors i.e "Table 'megatravel.reservations' doesn't exist" and another one "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in C:\xampp1\htdocs\JordanMahsman-MegaTravel2\contactform.php on line 111"

I am using XAMPP and have been to localhost/phpmyadmin to create the database 'megatravel' and to create the table 'reservations' but I am getting errors, and I need help.

<?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();

?>

1 Answers

0
atoms On

You only need to create the database once.

The error message is telling you that the reservations table needs creating.

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

It helps to use a SQL utility to manage the DB. and to play attention to any error messages SQL throws.