In the below code dbconnect.php is the file where i have written the code for connecting to database and there are no errors in it. I have done this php code to add users into a database. My Database connectivity is working perfectly. But When i try to select the tables from my database which is initially empty i'm getting the sqlstate as 42S22. Where am i going wrong. Is there any problem in mysqli statements. Kindly help me out.
<?php
include('dbconnect.php');
ob_start();
session_start();
if( isset($_SESSION['user'])!="" ){
header("Location: home.php");
}
// include 'dbconnect.php';
// require_once 'dbconnect.php';
// $con = $_SESSION['db'];
$error = false;
$nameError = $emailError = $passError = $ageError = $genderError =
$phoneError = $placeError = "";
if ( isset($_POST['btn-signup']) ) {
// clean user inputs to prevent sql injections
$name = trim($_POST['name']);
$name = strip_tags($name);
$name = htmlspecialchars($name);
$email = trim($_POST['email']);
$email = strip_tags($email);
$email = htmlspecialchars($email);
$pass = trim($_POST['pass']);
$pass = strip_tags($pass);
$pass = htmlspecialchars($pass);
$age = trim($_POST['age']);
$age = strip_tags($age);
$age = htmlspecialchars($age);
$gender = trim($_POST['gender']);
$gender = strip_tags($gender);
$gender = htmlspecialchars($gender);
$phone = trim($_POST['phone']);
$phone = strip_tags($phone);
$phone = htmlspecialchars($phone);
$place = trim($_POST['place']);
$place = strip_tags($place);
$place = htmlspecialchars($place);
// basic name validation
if (empty($name)) {
$error = true;
$nameError = "Please enter your full name.";
} else if (strlen($name) < 3) {
$error = true;
$nameError = "Name must have atleat 3 characters.";
} else if (!preg_match("/^[a-zA-Z ]+$/",$name)) {
$error = true;
$nameError = "Name must contain alphabets and space.";
}
//basic email validation
if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$error = true;
$emailError = "Please enter valid email address.";
} else {
// check email exist or not
$em = $conn->real_escape_string($email);
$q = "SELECT Email FROM users WHERE Email='$em'";
$result = $conn->query($q);
**Here Comes the error **
if (!$result) {
printf("%s",$conn->sqlstate);
exit();
}
**It prints 42S22**
$count = $result->num_rows;
if($count!=0){
$error = true;
$emailError = "Provided Email is already in use.";
}
}
// password validation
if (empty($pass)){
$error = true;
$passError = "Please enter password.";
} else if(strlen($pass) < 6) {
$error = true;
$passError = "Password must have atleast 6 characters.";
}
if (empty($age)) {
$error = true;
$ageError = "Enter your Age";
}
if (empty($gender)) {
$error = true;
$genderError = "Enter your Department";
}
if (empty($place)) {
$error = true;
$placeError = "Enter your Place";
}
if (empty($phone)) {
$error = true;
$phoneError = "Enter your Phone";
}
// password encrypt using SHA256();
$password = hash('sha256', $pass);
// if there's no error, continue to signup
if( !$error ) {
$query = "INSERT INTO users(Name,Email,Password,Age,Gender,Phone,Place) VALUES('$name','$email','$password','$age','$gender','$phone','$place')";
$res = $conn->query($query);
if ($res) {
$errTyp = "success";
$errMSG = "Successfully registered, you may login now";
unset($name);
unset($email);
unset($pass);
unset($age);
unset($gender);
unset($phone);
unset($place);
} else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}
}
}
?>
some html code below to get the input
please help how to solve it