-1

Sorry if what I'm going to ask is a dumb question, but I have read through and even apply some of the solutions to my problem but it's still not working. I've got the solutions from here: 1. Not Getting response after registration is successful 2. php register form not updating database 3. Inserted data was not saved during registration

I have a system where the user can register himself/herself as a candidate for job interviews. But right now, the system doesn't save their registration. I have go through the queries but find nothing. Perhaps anybody can point out where I have been doing wrong that make my system doesn't want to keep the data.

my register-candidates.php

<section class="content-header">
      <div class="container">
        <div class="row latest-job margin-top-50 margin-bottom-20 bg-white">
          <h1 class="text-center margin-bottom-20">CREATE YOUR PROFILE</h1>
          <form method="post" id="registerCandidates" action="adduser.php" enctype="multipart/form-data">
            <div class="col-md-6 latest-job ">
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="fname" name="fname" placeholder="First Name *" required>
              </div>
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="lname" name="lname" placeholder="Last Name *" required>
              </div>
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="email" name="email" placeholder="Email *" required>
              </div>
              <div class="form-group">
                <textarea class="form-control input-lg" rows="4" id="aboutme" name="aboutme" placeholder="Brief intro about yourself *" required></textarea>
              </div>
              <div class="form-group">
                <label>Date Of Birth</label>
                <input class="form-control input-lg" type="date" id="dob" min="1960-01-01" max="1999-01-31" name="dob" placeholder="Date Of Birth">
              </div>
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="age" name="age" placeholder="Age" readonly>
              </div>
              <div class="form-group">
                <label>Passing Year</label>
                <input class="form-control input-lg" type="date" id="passingyear" name="passingyear" placeholder="Passing Year">
              </div>       
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="qualification" name="qualification" placeholder="Highest Qualification">
              </div>
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="stream" name="stream" placeholder="Stream">
              </div>                    
              <div class="form-group checkbox">
                <label><input type="checkbox"> I accept terms & conditions</label>
              </div>
              <div class="form-group">
                <button class="btn btn-flat btn-success">Register</button>
              </div>
              <?php 
              //If User already registered with this email then show error message.
              if(isset($_SESSION['registerError'])) {
                ?>
                <div class="form-group">
                  <label style="color: red;">Email Already Exists! Choose A Different Email!</label>
                </div>
              <?php
               unset($_SESSION['registerError']); }
              ?> 

              <?php if(isset($_SESSION['uploadError'])) { ?>
              <div class="form-group">
                  <label style="color: red;"><?php echo $_SESSION['uploadError']; ?></label>
              </div>
              <?php unset($_SESSION['uploadError']); } ?>     

            </div>            
            <div class="col-md-6 latest-job ">
              <div class="form-group">
                <input class="form-control input-lg" type="password" id="password" name="password" placeholder="Password *" required>
              </div>
              <div class="form-group">
                <input class="form-control input-lg" type="password" id="cpassword" name="cpassword" placeholder="Confirm Password *" required>
              </div>
              <div id="passwordError" class="btn btn-flat btn-danger hide-me" >
                    Password Mismatch!! 
                  </div>
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="contactno" name="contactno" minlength="10" maxlength="10" onkeypress="return validatePhone(event);" placeholder="Phone Number">
              </div>
              <div class="form-group">
                <textarea class="form-control input-lg" rows="4" id="address" name="address" placeholder="Address"></textarea>
              </div>
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="city" name="city" placeholder="City">
              </div>
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="state" name="state" placeholder="State">
              </div>
              <div class="form-group">
                <textarea class="form-control input-lg" rows="4" id="skills" name="skills" placeholder="Enter Skills"></textarea>
              </div>              
              <div class="form-group">
                <input class="form-control input-lg" type="text" id="designation" name="designation" placeholder="Designation">
              </div>

              <div class="form-group">
                <label style="color: red;">File Format PDF Only!</label>
                <input type="file" name="resume" class="btn btn-flat btn-danger" required>
              </div>
            </div>
          </form>
          
        </div>
      </div>
    </section>

adduser.php

<?php

//To Handle Session Variables on This Page
session_start();

//Including Database Connection From db.php file to avoid rewriting in all files
require_once("db.php");

//If user clicked register button
if(isset($_POST)) {

 //Escape Special Characters In String First
 $firstname = mysqli_real_escape_string($conn, $_POST['fname']);
 $lastname = mysqli_real_escape_string($conn, $_POST['lname']);
 $address = mysqli_real_escape_string($conn, $_POST['address']);
 $city = mysqli_real_escape_string($conn, $_POST ['city']);
 $state = mysqli_real_escape_string($conn, $_POST ['state']);
 $contactno = mysqli_real_escape_string($conn, $_POST ['contactno']);
 $qualification = mysqli_real_escape_string($conn, $_POST ['qualification']);
 $stream = mysqli_real_escape_string ($conn, $_POST['stream']);
 $passingyear = mysqli_real_escape_string($conn, $_POST['passingyear']);
 $dob = mysqli_real_escape_string($conn, $_POST['dob']);
 $age = mysqli_real_escape_string($conn, $_POST['age']);
 $designation = mysqli_real_escape_string($conn, $_POST['designation']);
 $aboutme = mysqli_real_escape_string($conn, $_POST['aboutme']);
 $skills = mysqli_real_escape_string($conn, $_POST['skills']);
 $email = mysqli_real_escape_string($conn, $_POST['email']);
 $password = mysqli_real_escape_string($conn, $_POST['password']);

 //Encrypt Password
 $password = base64_encode(strrev(md5($password)));

 //sql query to check if email already exists or not
 $sql = "SELECT email FROM users WHERE email='$email'";
 $result = $conn->query($sql);

 //if email not found then we can insert new data
 if($result->num_rows == 0) {

   //This variable is used to catch errors doing upload process. False means there is some error and we need to notify that user.
  $uploadOk = true;

  //Folder where you want to save your image. THIS FOLDER MUST BE CREATED BEFORE TRYING
  $folder_dir = "uploads/resume/";

  //Getting Basename of file. So if your file location is Documents/New Folder/myResume.pdf then base name will return myResume.pdf
  $base = basename($_FILES['resume']['name']); 

  //This will get us extension of your file. So myimage.pdf will return pdf. If it was image.doc then this will return doc.
  $imageFileType = pathinfo($base, PATHINFO_EXTENSION); 

  //Setting a random non repeatable file name. Uniqid will create a unique name based on current timestamp. We are using this because no two files can be of same name as it will overwrite.
  $file = uniqid() . "." . $resumeFileType; 
   
  //This is where your files will be saved so in this case it will be uploads/image/newfilename
  $filename = $folder_dir .$file;  

  //We check if file is saved to our temp location or not.
  if(file_exists($_FILES['resume']['tmp_name'])) { 

   //Next we need to check if file type is of our allowed extention or not. I have only allowed pdf. You can allow doc, jpg etc. 
   if($resumeFileType == "pdf")  {

    //Next we need to check file size with our limit size. I have set the limit size to 5MB. Note if you set higher than 2MB then you must change your php.ini configuration and change upload_max_filesize and restart your server
    if($_FILES['resume']['size'] < 500000) { // File size is less than 5MB

     //If all above condition are met then copy file from server temp location to uploads folder.
     move_uploaded_file($_FILES["resume"]["tmp_name"], $filename);

    } else {
     //Size Error
     $_SESSION['uploadError'] = "Wrong Size. Max Size Allowed : 5MB";
     $uploadOk = false;
    }
   } else {
    //Format Error
    $_SESSION['uploadError'] = "Wrong Size. Max Size Allowed : 5MB ";
    $uploadOk = false;
   }
  } else {
    //File not copied to temp location error.
    $_SESSION['uploadError'] = "Something Went Wrong. File Not Uploaded. Try Again.";
    $uploadOk = false;
   }

  //If there is any error then redirect back.
  if($uploadOk == false) {
   header("Location: register-candidates.php");
   exit();
  }

  //sql new registration insert query
    $sql = "INSERT INTO users(firstname, lastname, email, password, address, city, state, contactno, qualification, stream, passingyear, dob, age, designation, resume, hash, aboutme, skills) VALUES ('$firstname', '$lastname', '$email', '$password', '$address', '$city', '$state', '$contactno', '$qualification', '$stream', '$passingyear', '$dob', '$age', '$designation', '$file', '$hash', '$aboutme', '$skills')";

  if($conn->query($sql)===TRUE) {

   //If data inserted successfully then Set some session variables for easy reference and redirect to company login
   $_SESSION['registerCompleted'] = true;
   header("Location: login-candidates.php");
   exit();

  } else {
   //If data failed to insert then show that error. Note: This condition should not come unless we as a developer make mistake or someone tries to hack their way in and mess up :D
   echo "Error " . $sql . "<br>" . $conn->error;
  }
 } else {
  //if email found in database then show email already exists error.
  $_SESSION['registerError'] = true;
  header("Location: register-candidates.php");
  exit();
 }

 //Close database connection. Not compulsory but good practice.
 $conn->close();

} else {
 //redirect them back to register page if they didn't click register button
 header("Location: register-candidates.php");
 exit();
}

thank you for the help and your time.

marcramser
  • 579
  • 1
  • 10
  • 23
WanHazyan
  • 257
  • 1
  • 12
  • Do you get any error message? Did you try to make a select query to the DB so you can see if the connection works? Could you add the $sql variable (`echo($sql)`). – marcramser Jul 23 '18 at 08:11
  • I don't get any error message and as for the connection, it works fine because when I tested the add company part, it's working fine. – WanHazyan Jul 23 '18 at 08:14
  • Use if(isset($_POST['resume'])){ // Your code – PHP Web Jul 23 '18 at 08:17
  • Try to remove/comment all header("Location: register-candidates.php"); (just for debugging). And make a echo($_SESSION) at the end of the file. And add the output here. – marcramser Jul 23 '18 at 08:20
  • I did the ($_POST['resume']) still not working – WanHazyan Jul 23 '18 at 08:32
  • I don't understand where should I put the echo ($_SESSION). I'm a very novice developer. Still learning a lot. – WanHazyan Jul 23 '18 at 08:33

2 Answers2

0

I think you should to debug your app by following those steps :

  • Try to direct insert data from SQL command in your ManagementInterface
  • Try to echo $var | var_dump($var) of each input you get from your form when you reach your register.php.
  • Try to see if you reach your condition like if(isset($_POST["var"]&&!empty($_POST["var"])) but if you can echo them it's not here you have to search

It might be your queries statement that block you. You should try to improve your code with prepared statement for your SQL queries, it will be more readable and maintenable. Also, i think that mysqli & co are deprecated.

I hope it will be usefull for you and it ill help you to find your error. Respond in comment if this doesn't help you, 'ill try to find out why

Regards

Snics
  • 1
  • 3
0

You have not defined $resumeFileType and in order for your script to execute, it has to check that the $resumeFileType is a PDF.

ninjadave
  • 56
  • 4