Click here to Skip to main content
15,611,970 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys I am getting this error I can not figure it out where am I doing wrong

so I have create.php and textboxes choose file and dropdown after that i have js who is calling for createprocess.php

and i am getting error Undefined variable: role
<br /> Notice: Undefined variable: role

on createpocess.php line 34

can you please help

What I have tried:

div class="wrapper">
    <section class="form signup">
      <header>Create User</header>
      <form action="#" method="POST" enctype="multipart/form-data" autocomplete="off">
        <div class="error-text"></div>
        <div class="name-details">
          <div class="field input">
            <label>First Name</label>
            <input type="text" name="fname" placeholder="First name" required>
          <div class="field input">
            <label>Last Name</label>
            <input type="text" name="lname" placeholder="Last name" required>
        <div class="field input">
          <input type="text" name="username" placeholder="Username" required>
        <div class="field input">
          <label>Email Address</label>
          <input type="text" name="email" placeholder="Enter your email" required>
        <div class="field input">
          <input type="password" name="password" placeholder="Enter new password" required>
          class="fas fa-eye">
        <div class="form-group">
  <label class="label_txt">Role: </label>
  <select type="text" class="form-control" name="role"  required>
  <option value="Admin">Admin</option>
  <option value="Owner">Owner</option>
  <option value="Agent">Agent</option>
  <option value="Customer">Customer</option>
        <div class="field image">
          <label>Select Image</label>
          <input type="file" name="image" accept="image/x-png,image/gif,image/jpeg,image/jpg" required>
        <div class="field button">
          <input type="submit" name="submit" value="Create">

    include_once "config.php";
    $fname = mysqli_real_escape_string($conn, $_POST['fname']);
    $lname = mysqli_real_escape_string($conn, $_POST['lname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    if(!empty($fname) && !empty($lname) && !empty($email) && !empty($password)){
        if(filter_var($email, FILTER_VALIDATE_EMAIL)){
            $sql = mysqli_query($conn, "SELECT * FROM users WHERE email = '{$email}'");
            if(mysqli_num_rows($sql) > 0){
                echo "$email - This email already exist!";
                    $img_name = $_FILES['image']['name'];
                    $img_type = $_FILES['image']['type'];
                    $tmp_name = $_FILES['image']['tmp_name'];
                    $img_explode = explode('.',$img_name);
                    $img_ext = end($img_explode);
                    $extensions = ["jpeg", "png", "jpg"];
                    if(in_array($img_ext, $extensions) === true){
                        $types = ["image/jpeg", "image/jpg", "image/png"];
                        if(in_array($img_type, $types) === true){
                            $time = time();
                            $new_img_name = $time.$img_name;
                                $ran_id = rand(time(), 100000000);
                                $status = "Active now";
                                $encrypt_pass = md5($password);
                                $insert_query = mysqli_query($conn, "INSERT INTO `users`(`id`, `fname`, `lname`, `username`, `email`, `password`, `date`, `role`, `unique_id`, `status`, `img`) 
                                VALUES ({$ran_id}, '{$fname}', '{$lname}', '{$username}', '{$email}', '{$encrypt_pass}', '{$date}', '{$role}', '{$ran_id}, '{$status}', '{$new_img_name}')");
                                    $select_sql2 = mysqli_query($conn, "SELECT * FROM users WHERE email = '{$email}'");
                                    if(mysqli_num_rows($select_sql2) > 0){
                                        $result = mysqli_fetch_assoc($select_sql2);
                                        $_SESSION['unique_id'] = $result['unique_id'];
                                        echo "success";
                                        echo "This email address not Exist!";
                                    echo "Something went wrong. Please try again!";
                            echo "Please upload an image file - jpeg, png, jpg";
                        echo "Please upload an image file - jpeg, png, jpg";
            echo "$email is not a valid email!";
        echo "All input fields are required!";
Updated 21-Dec-21 11:11am

1 solution

There is no $role variable in your PHP code except for the INSERT statement; and you are ignoring $_POST['role'].

Share this answer
Member 13084733 21-Dec-21 17:16pm    
on createprocess.php?
'{$role}' and when I remove role from insert command it is working then for the rest
Luc Pattyn 21-Dec-21 17:18pm    
in everything you posted $role appears only once, that cannot be correct.
Member 13084733 21-Dec-21 17:29pm    
yup I didn't define role thank you very much

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