The form is submitted but is not adding no data to the database or uploading any files onto the folder on the server, the page just comes up with the following
Form has been submitted successfully.
Please provide another file type [E/2].
the file type bit is working as I am testing it by uploading a php file and I have only allowed pdf, doc and docx files to be uploaded so am thinking the code is in the wrong place, below is my whole code
<?php
if (isset($_POST['submit']) && isset($error) == '') { // if there is no error, then process further
echo "<p class='success'>Form has been submitted successfully.</p>"; // showing success message
## connect mysql server
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
exit();
}
## query database
# prepare data for insertion
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$password = md5($_POST['password']);
/*$password = $_POST['password'];*/
$name = mysqli_real_escape_string($mysqli, $_POST['name']);
$dob = date('Y-m-d', strtotime($_POST['dob']));
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$address1 = mysqli_real_escape_string($mysqli, $_POST['address1']);
$address2 = mysqli_real_escape_string($mysqli, $_POST['address2']);
$town = mysqli_real_escape_string($mysqli, $_POST['town']);
$county = mysqli_real_escape_string($mysqli, $_POST['county']);
$postcode = mysqli_real_escape_string($mysqli, $_POST['postcode']);
$telnumber = mysqli_real_escape_string($mysqli, $_POST['telnumber']);
$mobnumber = mysqli_real_escape_string($mysqli, $_POST['mobnumber']);
$worklocation = mysqli_real_escape_string($mysqli, $_POST['worklocation']);
$desiredsalary = mysqli_real_escape_string($mysqli, $_POST['desiredsalary']);
$currentempstatus = mysqli_real_escape_string($mysqli, $_POST['currentempstatus']);
$educationlevel = mysqli_real_escape_string($mysqli, $_POST['educationlevel']);
$availableforwork = mysqli_real_escape_string($mysqli, $_POST['availableforwork']);
$jobtype = mysqli_real_escape_string($mysqli, $_POST['jobtype']);
$cv = ($_FILES['cvfile']['name']);
$role = mysqli_real_escape_string($mysqli, $_POST['role']);
# check if username and email exist else insert
// u = username, e = emai, ue = both username and email already exists
$exists = "";
$result = $mysqli->query("SELECT username from candidates WHERE username = '{$username}' LIMIT 1");
if ($result->num_rows == 1) {
$exists .= "u";
}
$result = $mysqli->query("SELECT email from candidates WHERE email = '{$email}' LIMIT 1");
if ($result->num_rows == 1) {
$exists .= "e";
}
if ($exists == "u") echo "<p><b>Error:</b> Username already exists!</p>";
else if ($exists == "e") echo "<p><b>Error:</b> Email already exists!</p>";
else if ($exists == "ue") echo "<p><b>Error:</b> Username and Email already exists!</p>";
else {
# insert data into mysql database
$sql = "INSERT INTO `candidates` (`id`, `username`, `password`, `name`, `dob`, `email`, `address1`, `address2`, `town`, `county`, `postcode`, `telnumber`, `mobnumber`, `worklocation`, `desiredsalary`, `currentempstatus`, `educationlevel`, `availableforwork`, `jobtype`, `cvfile`, `role`)
VALUES (NULL, '{$username}', '{$password}', '{$name}', '{$dob}', '{$email}', '{$address1}', '{$address2}', '{$town}', '{$county}', '{$postcode}', '{$telnumber}', '{$mobnumber}', '{$worklocation}', '{$desiredsalary}', '{$currentempstatus}', '{$educationlevel}', '{$availableforwork}', '{$jobtype}', '{$cv}', 'Candidate')";
$allowedExts = array(
"pdf",
"doc",
"docx"
);
$allowedMimeTypes = array(
'application/msword',
'application/pdf'
);
$extension = explode(".", $_FILES["cvfile"]["name"]);
if ( ! ( in_array($extension, $allowedExts ) ) ) {
die('Please provide another file type [E/2].');
}
if ( in_array( $_FILES["cvfile"]["type"], $allowedMimeTypes ) )
{
move_uploaded_file($_FILES["cvfile"]["tmp_name"], "/home/sites/broadwaymediadesigns.co.uk/public_html/sites/recruitment-site/candidatecvs/" . $_FILES["cvfile"]["name"]);
}
else
{
die('Please provide another file type [E/3].');
}
}
if ($mysqli->query($sql)) {
$to = $_POST['email'];
$subject = "Login Credentials";
$message = "Thank you for signing up, your login information is below \r\n Username: {$_POST['username']} \r\n Password: {$_POST['password']}";
$header = "From:noreply@domain.co.uk \r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
redirect_to("candidates-login.php?msg=Registered successfully");
} else {
echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
exit();
}
}
?>
after submitted the code should redirect to the login php page but is not doing that so is why I am thinking I have the code that checks for the file type in the wrong place or something cause if I take that bit of coding out, the script works perfect and it add the data to the database and redirects to the login page