Link to home
Start Free TrialLog in
Avatar of mark louie
mark louie

asked on

hey guys i need help my database is not receiving any data that has been entered to my form and there's no error so i paste the function that i used to the form and i attached the file booking.php

booking.php<?php
    session_start();
      // connect to database
$db = mysqli_connect('localhost', 'root', '12345', 'hotel booking');

// variable declaration
$username = "";
$Email    = "";
$Phonenumber = "";
$country = "";
$bookdate ="";
$meals ="";
$numberofguest ="";
$Room ="";
$Foodmenu ="";
$errors = array();

// call the register() function if register_btn is clicked
if (isset($_POST['register_btn'])) {
      register();
}

// REGISTER USER
function register(){
      // call these variables with the global keyword to make them available in function
      global $db, $errors, $username, $Email , $Phonenumber , $country, $bookdate, $meals, $numberofguest, $Room, $Foodmenu;

      // receive all input values from the form. Call the e() function
    // defined below to escape form values

      $username    = e($_POST['username']);
      $Email       = e($_POST['Email']);
      $Phonenumber = e($_POST['Phonenumber']);
      $country     = e($_POST['country']);
    $bookdate    = e($_POST['bookdate  ']);
      $meals       = e($_POST['meals']);
      $numberofguest = e($_POST['numberofguest']);
      $Room = e($_POST['Room']);
      $Foodmenu = e($_POST['Food menu']);
      // form validation: ensure that the form is correctly filled
      if (empty($username)) {
            array_push($errors, "username is required");
      }
      if (empty($Email)) {
            array_push($errors, "Email is required");
      }
      if (empty($Phonenumber)) {
            array_push($errors, "Phonenumber is required");
    }
      if (empty($country)) {
            array_push($errors, "country is required");
    }
      if (empty($bookdate)) {
            array_push($errors, "bookdate is required");
    }
      if (empty($meals)) {
            array_push($errors, "meals is required");
    }
      if (empty($numberofguest)) {
            array_push($errors, "numberofguest is required");
    }
      if (empty($Room)) {
            array_push($errors, "Room is required");
    }
      if (empty($Foodmenu)) {
            array_push($errors, "Foodmenu is required");
    }
      // register user if there are no errors in the form
      

            if (isset($_POST['personal details'])) {
                  $user_type = e($_POST['personal details']);
                  $query = "INSERT INTO `personal details` (`IDNUMBER`, `username`, `Email`, `Phonenumber`, `country`, `book date`, `meals`, `number of guest`, `Room`, `Food menu`)
                  VALUES (NULL, '$username', '$Email', '$Phonenumber', '$country', '$bookdate', '$meals', '$numberofguest', '$Room', '$Foodmenu') ";
                  mysqli_query($db, $query);
                  $_SESSION['success']  = "New user successfully created!!";
                  header('HOME PAGE 1.HTML');
            }else{
                  $query = "INSERT INTO `personal details` (`IDNUMBER`, `username`, `Email`, `Phonenumber`, `country`, `book date`, `meals`, `number of guest`, `Room`, `Food menu`)
                  VALUES (NULL, '$username', '$Email', '$Phonenumber', '$country', '$bookdate', '$meals', '$numberofguest', '$Room', '$Foodmenu') ";
                  mysqli_query($db, $query);

                  // get id of the created user
                  $logged_in_personaldetails_id= mysqli_insert_id($db);
                  $_SESSION['personaldetails'] = getUserById($username); // put logged in user in session
                  $_SESSION['success']  = "New user successfully created!!";
                  header('HOME PAGE 1.HTML');
            }
      }
// return user array from their id
function getUserById($username){
      global $db;
      $query = "SELECT * FROM `personal details` WHERE `username`  ='$username'";
      $result = mysqli_query($db, $query);
      $personaldetails = mysqli_fetch_assoc($result);
      return $personaldetails;
}

// escape string
function e($val){
      global $db;
      return mysqli_real_escape_string($db,trim($val));
}

function display_error() {
      global $errors;

      if (count($errors) > 0){
            echo '<div class="error">';
                  foreach ($errors as $error){
                        echo $error .'<br>';
                  }
            echo '</div>';
      }
}
?>
ASKER CERTIFIED SOLUTION
Avatar of Peos John
Peos John
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mark louie
mark louie

ASKER

i got no error on the code  and my database is still not receiving any data is there any possible reason? because i search the whole web about the problem but none are related to the problem i'm having.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
done no error on the page
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
done and still no error and database is still not receiving data.
User generated imageUser generated imagei manage to redo all the codes i got 4 errors about undefined index does this mean i have to redo the database table?
updated codes
?php
    session_start();
      ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
      // connect to database
$db = mysqli_connect('localhost', 'root', '12345', 'hotel booking');

// variable declaration
$username = "";
$Email    = "";
$Phonenumber = "";
$country = "";
$bookdate ="";
$meals ="";
$numberofguest ="";
$Room ="";
$Foodmenu ="";
$errors = array();

// call the register() function if register_btn is clicked
if (isset($_POST['register_btn'])) {
      register();
}

// REGISTER USER
function register(){
      // call these variables with the global keyword to make them available in function
      global $db, $errors, $username, $Email, $Phonenumber, $country, $bookdate, $meals, $numberofguest, $Room, $Foodmenu;

      // receive all input values from the form. Call the e() function
    // defined below to escape form values

      $username    = e($_POST['username']);
      $Email       = e($_POST['Email']);
      $Phonenumber = e($_POST['Phonenumber']);
      $country     = e($_POST['country']);
    $bookdate    = e($_POST['bookdate']);
      $meals       = e($_POST['meals']);
      $numberofguest = e($_POST['numberofguest']);
      $Room = e($_POST['Room']);
      $Foodmenu = e($_POST['Foodmenu']);
      // form validation: ensure that the form is correctly filled
      if (empty($username)) {
            array_push($errors, "username is required");
      }
      if (empty($Email)) {
            array_push($errors, "Email is required");
      }
      if (empty($Phonenumber)) {
            array_push($errors, "Phonenumber is required");
    }
      if (empty($country)) {
            array_push($errors, "country is required");
    }
      if (empty($bookdate)) {
            array_push($errors, "bookdate is required");
    }
      if (empty($meals)) {
            array_push($errors, "meals is required");
    }
      if (empty($numberofguest)) {
            array_push($errors, "numberofguest is required");
    }
      if (empty($Room)) {
            array_push($errors, "Room is required");
    }
      if (empty($Foodmenu)) {
            array_push($errors, "Foodmenu is required");
    }
      // register user if there are no errors in the form
      

             if (isset($_POST['personal details'])) {
                  $user_type = e($_POST['personal details']);
                  $query = "INSERT INTO `personal details` (`IDNUMBER`, `username`, `Email`, `Phonenumber`, `country`, `bookdate`, `meals`, `numberofguest`, `Room`, `Foodmenu`)
                          VALUES (NULL, '$username', '$Email', '$Phonenumber', '$country', '$bookdate', '$meals', '$numberofguest', '$Room', '$Foodmenu') ";
                  mysqli_query($db, $query) or die(mysqli_error($db));
                  $_SESSION['success']  = "New user successfully created!!";
                  header("location:../booking.php");
            }else{
                  $query = "INSERT INTO `personal details` (`IDNUMBER`, `username`, `Email`, `Phonenumber`, `country`, `bookdate`, `meals`, `numberofguest`, `Room`, `Foodmenu`)
                          VALUES (NULL, '$username', '$Email', '$Phonenumber', '$country', '$bookdate', '$meals', '$numberofguest', '$Room', '$Foodmenu') ";
                  mysqli_query($db, $query) or die(mysqli_error($db));

                                    
            }

            }
      
// return user array from their id
function getUserById($username){
      global $db;
      $query = "SELECT * FROM `personal details` WHERE `username`=". $username;
      $result = mysqli_query($db, $query) or die(mysqli_error($db));
      $personaldetails = mysqli_fetch_assoc($result);
      return $personaldetails;
}

// escape string
function e($val){
      global $db;
      return mysqli_real_escape_string($db,trim($val));
}

function display_error() {
      global $errors;

      if (count($errors) > 0){
            echo '<div class="error">';
                  foreach ($errors as $error){
                        echo $error .'<br>';
                  }
            echo '</div>';
      }
}
?>
thank you guys for your help and ideas i solved the problem and its working