Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
(localhost .PHP error)
Warning: Undefined variable $conn in C:\xampp\htdocs\phpclinicrecordsystem\insert.php on line 31

Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, null given in C:\xampp\htdocs\phpclinicrecordsystem\insert.php:31 Stack trace: #0 C:\xampp\htdocs\phpclinicrecordsystem\insert.php(31): mysqli_query(NULL, 'INSERT INTO tbl...') #1 {main} thrown in C:\xampp\htdocs\phpclinicrecordsystem\insert.php on line 31


LINE 31
        $sql = mysqli_query($conn,"INSERT INTO tblstudentinformation(FirstName,MiddleName,LastName,Course,YearLevel,DateOfBirth,Age,Gender,Address,Parent/Guardian,PhoneNumber,BloodPressure,PulseRate,RespiratoryRate,Temperature,Weight,Height,Date,NurseName,Disease,Remarks,Medicinename,Quantity) VALUES ('$FirstName','$MiddleName','$LastName','$Course','$YearLevel','$DateofBirth','$Age','$Gender','$Address','$ParentGuardian','$PhoneNumber','$BloodPressure','$PulseRate','$RespiratoryRate','$Temperature','$Weight','$Height','$Date','$NurseName','$Disease','$Remarks','$Medicinename','$Quantity')");


What I have tried:

I TRIED EVERYTHING I COULD DO BUT ALWAYS ERROR
Posted
Updated 24-Oct-21 8:29am
Comments
Richard Deeming 25-Oct-21 6:28am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

Given the references to medical data, I can only hope this is a toy application which will never be used in the real world. If not, prepare to face massive fines for not securing your users' data properly.

1 solution

Look at the error message, your $conn variable is not defined, so MySQL cannot figure out where to send the command. See MySQL Tutorial - Learn MySQL Fast, Easy and Fun.[^]
 
Share this answer
 
v2
Comments
Working Student Ph 24-Oct-21 14:39pm    
THANK YOU FOR ANSWERING. WHAT SHOULD I DO ?
Dave Kreskowiak 24-Oct-21 18:10pm    
First, stop copying and pasted code form the internet.

You have to define the $conn variable with the MqSql connection object, which itself must be properly setup to connect to your MySQL database.
Richard MacCutchan 25-Oct-21 3:52am    
The problem you report is something that even a beginner should be able to solve by themselves. You will continue to struggle unless you spend the time to learn both languages in detail.
Working Student Ph 24-Oct-21 14:39pm    
THIS IS MY CODE

alert('New record successfully added!');";
echo "document.location='insert.php';";
}else{
echo "alert('Something Went Wrong!');";
}

}


?>
Richard MacCutchan 25-Oct-21 3:53am    
And it is a)incorrect, and b) serves no useful purpose.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900