• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 77
  • Last Modified:

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>';
      }
}
?>
0
mark louie
Asked:
mark louie
  • 5
  • 2
3 Solutions
 
Peos JohnPHPCommented:
It looks like concatenation issue. Also please let us know what is the error you are getting.

can you try changing these lines like below
 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);

Open in new window

0
 
mark louieAuthor Commented:
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.
0
 
Peos JohnPHPCommented:
Can you try enabling error reporting on the page ?

Please add these lines on top of your pages and see if you are getting any error.

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
mark louieAuthor Commented:
done no error on the page
0
 
Sam WallisIt/Web ManagerCommented:
Change your mysqli query line to
mysqli_query($db, $query) or die(mysqli_error($db));

Open in new window


This will let you know if there are any errors in your SQL syntax/query
0
 
mark louieAuthor Commented:
done and still no error and database is still not receiving data.
0
 
mark louieAuthor Commented:
errors that happen when i redo all the codesthis is my table in the serveri 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>';
      }
}
?>
0
 
mark louieAuthor Commented:
thank you guys for your help and ideas i solved the problem and its working
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now