Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My attempts to address this have failed since I am unable to locate the fault. I apologise for any inconvenience. Help me out if you can! So far, here is what I've written in my code: Was there anything I could have done differently? My search for it has gone over every detail, but I have come up empty. As soon as I press the button, there is no problem and the programme will display the result that I specify it to echo. The data i wish to insert on my won't show on my phpmyadmin database.

The code below is what i'm using

What I have tried:

<pre>
<pre><?php 
define('USER', 'root');
define('PASSWORD', 'root');
define('HOST', 'localhost:8889');
define('DATABASE', 'test');
try {
    $connection = new PDO("mysql:host=".HOST.";dbname=".DATABASE, USER, PASSWORD);
    echo "Database connection established";
} catch (PDOException $e) {
    exit("Error: " . $e->getMessage());
}

$status ="";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $date = $_POST['date'];
  $time = $_POST['time'];
  $salon_name = $_POST['salon_name'];

  if (empty($name) || empty($email) || empty($date) || empty($time) || empty($salon_name)) {
    $status = "All fields are required";
    }else {
      if (strlen($name) > 255 || !preg_match("/^[a-zA-Z-'\s]+$/",$name)) {
        $status = "Please enter a valid name";
      } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $status = "Please enter a valid email";
      } else {

        $sql = "INSERT INTO bookings (name, email, date, time, salon_name)
        VALUES ( :name , :email , :date , :time, :salon_name)";
        
        $query = $connection->prepare($sql);
        
        $result = $query->execute(['name' => $name, 'email' => $email, 'date' =>$date, 'time' =>$time, 'salon_name' => $salon_name]);

        $status = "Your booking was successfully created";
        $name = "";
        $email = "";
        $date = "";
        $time = "";
        $salon_name = "";
      } 
    } 
  }

?>


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  <link rel="stylesheet" href="style.css">
  <title>Booking form</title>
</head>

<body>

  <div class="container">
    <form action="" method="POST" class="main-form">
    <h1>Booking form</h1>
      <div class="form-group">
        <label for="name">Full Name</label>
        <input type="text" name="name" id="name" 
          value="<?php if($_SERVER['REQUEST_METHOD'] == 'POST') echo $name ?>">
      </div>

      <div class="form-group">
        <label for="email">Email</label>
        <input type="text" name="email" id="email" 
          value="<?php if($_SERVER['REQUEST_METHOD'] == 'POST') echo $email ?>">
      </div>

      <div class="form-group">
        <label for="date ">date</label>
        <input type="date" name="date" id="date" 
          value="<?php if($_SERVER['REQUEST_METHOD'] == 'POST') echo $date ?>">
      </div>

      <div class="form-group">
        <label for="time ">time</label>
        <select id="time" name="time">
            <option value="time">9:00</option>
            <option value="time">10:30</option>
            <option value="time">11:40</option>
            <option value="time">12:30</option>
            <option value="time">1:20</option>
            <option value="time">2:00</option>
            <option value="time">3:30</option>
          </select>
      </div>

      <div class="form-group">
      <label for="salon_name">select salons</label>
          <select id="salon_name" name="salon_name">
            <option value="salon">The Barbers</option>
            <option value="salon">Jo's Hair Salon</option>
            <option value="salon">Class Cuts Unisex Hair & Beauty salon</option>
            <option value="salon">The Continental</option>
            <option value="salon">HAIR IMAGEZ</option>
            <option value="salon">Faith and Grace Hair and Beauty</option>
          </select>
      </div>

      <input type="submit"  value="Submit">

      <div class="form-status">
        <?php echo $status ?>
      </div>
    </form>
  </div>


</body>

</html>
Posted
Comments
Richard MacCutchan 10-Apr-22 3:45am    
What is the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900