'My data is not being inserted into the phpmyadmin database [duplicate]

I am trying to create a from which takes input from the user and stores its a database called patientappointment_db. I created the following form in html:

<form id="form1" name="form1" method="post" action="doctor.php">
<label for="EmployeeID">EmployeeID</label><input type="text" name="EmployeeID" id="EmployeeID" />
<br class="clear" /> 
<label for="Name">Name</label><input type="text" name="Name" id="Name" />
<br class="clear" /> 
<label for="Specialty">Specialty</label><input type="text" name="Specialty" id="Specialty" />
<br class="clear" /> 
<label for="Department">Department</label><input type="text" name="Department" id="Department" />
<br class="clear" /> 
<label for="HospitalID">HospitalID</label><input type="text" name="HospitalID" id="HospitalID" />
<br class="clear" /> 
<label for="LicenseNo">LicenseNo</label><input type="text" name="LicenseNo" id="LicenseNo" />
<br class="clear" /> 
<input type="submit" name="suibmit" id="suibmit" value="suibmit" />
<br class="clear" /> 
</form>

My connect file:

<?php
$servername = "localhost";
$database = "patientappointment_db";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($conn);
?>

Finally my PHP query

<?php include 'connect.php';?>


<?php //Post Params 
$EmployeeID = $_POST['EmployeeID'];  
$Name = $_POST['Name'];  
$Specialty = $_POST['Specialty'];  
$Department = $_POST['Department'];  
$HospitalID = $_POST['HospitalID'];  
$LicenseNo = $_POST['LicenseNo'];  

?>

<?php //Query 

 //INSERT 
 $query = " INSERT INTO doctor ( EmployeeID, Name, Specialty, Department, HospitalID, LicenseNo )  VALUES ( '$EmployeeID', '$Name', '$Specialty', '$Department', '$HospitalID', '$LicenseNo' ) "; 
 $result = mysql_query($query); 

 if( $result )
 {
    echo 'Success';
 }
 else
 {
    echo 'Query Failed';
 }

?>

When I am hitting the submit button my output is the the below html code and not the success message and no value is being entered in the database.

<?php include 'connect.php';?>


<?php //Post Params 
$EmployeeID = $_POST['EmployeeID'];  
$Name = $_POST['Name'];  
$Specialty = $_POST['Specialty'];  
$Department = $_POST['Department'];  
$HospitalID = $_POST['HospitalID'];  
$LicenseNo = $_POST['LicenseNo'];  

?>

<?php //Query 

 //INSERT 
 $query = " INSERT INTO doctor ( EmployeeID, Name, Specialty, Department, HospitalID, LicenseNo )  VALUES ( '$EmployeeID', '$Name', '$Specialty', '$Department', '$HospitalID', '$LicenseNo' ) "; 
 $result = mysql_query($query); 

 if( $result )
 {
    echo 'Success';
 }
 else
 {
    echo 'Query Failed';
 }

?>

I am not sure where my problem is and what to do to solve this error.



Solution 1:[1]

you mix mysql and mysqli function in your code.

In connect.php you use mysqli functions and in your insert script doctor.php you call :

$result = mysql_query($query); 

Replace by:

$result = mysqli_query($query); 

You should really use mysqli because mysql is deprecated.

You also need to escape datas you insert in database because without, you are exposed to SQL injections

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1