简体   繁体   中英

Can anyone tell me why isn't the query executing in the following php code?

<html>
  <head>
    <title>
      Welcome
    </title>
  </head>
  <body>
    <?php
$fn=$_POST['f_name'];
$ln=$_POST['l_name'];
$gen=$_POST['sex'];
$dob=$_POST['dob'];
$em=$_POST['email'];
$un=$_POST['u_name'];
$pd=$_POST['pswd'];
$mb=$_POST['mob'];
$con=mysqli_connect('localhost','username','password','db')
   or die("Error connecting to database");
$query="INSERT INTO user_details(First_Name, Last_Name, Gender, DOB, Email_Address,
Username, Password, Mobile_No) VALUES                   
('$fn','$ln','$gen','$dob','$em','$un',SHA('$pd'),'$mb')";
echo $gen;
$result=mysqli_query($con,$query) 
  or die("Error querying the database");

mysqli_close($con);
$name=$fn.' '.$ln;
echo "<b>Welcome $name</b>";
    ?>
  </body>
</html>

What I am trying to do in this code is to take data from an HTML form and store it into a MYSQL database. I even thoroughly checked my database for any type conflicts or any datatype length issues but still I am not getting it. I don't know why it is showing >> Error quering the database ? Thanks in advance.

Change

$result=mysqli_query($con,$query) 
  or die("Error querying the database");

to

$result=mysqli_query($con,$query) 
  or die($mysqli->error);

This will provide you with the exact error message from the server .. docs are here

First, you should be using prepared statements with mysqli. It is very simple to do.

Second, you should use ...or die($mysqli->error) to see the error message.

a proper code from someone who have a clue to get the error message in case of failed query

$result = mysqli_query($con,$query) 
  or trigger_error(mysqli_error($con)." ".$query);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM