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

asked on

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\test\personal details functions.php on line 71 hi i keep getting this error and i cant solve it

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

// variable declaration
$username = "";
$Email    = "";
$Phonenumber = "";
$country = "";
$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;

      // 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']);
      // 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");
    }
      // register user if there are no errors in the form
      

            if (isset($_POST['personal details'])) {
                  $user_type = e($_POST['personal details']);
                  $query = "INSERT INTO users (username, Email, Phonenumber, country )
                                VALUES('$username', '$Email', '$Phonenumber', '$country' )";
                  mysqli_query($db, $query);
                  $_SESSION['success']  = "New user successfully created!!";
                  header('C:\wamp64\www\test\HOME PAGE 1.HTML');
            }else{
                  $query = "INSERT INTO personal details (username, Email, Phonenumber, country )
                                VALUES('$username', '$Email', '$Phonenumber', '$country' )";
                  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 $username;
}

// 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>';
      }
}

function isLoggedIn()
{
      if (isset($_SESSION['user'])) {
            return true;
      }else{
            return false;
      }
}
?>
SOLUTION
Avatar of Elangovan Sundar
Elangovan Sundar

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
SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India 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
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
ASKER CERTIFIED 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
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
write query like this

select * from [personnel details]
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
Avatar of mark louie
mark louie

ASKER

thank you instructors for helping me on my project i'v been stuck to this code for an hour and i'm still learning some of these codes from a book and a few videos on the internet, because i'm still a college student maybe next time i'll try to help you guys in a different field in IT course.
@mark louie it works or not,

we here for to help u and others, any time welcome...
it works thank you