Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Warning: Undefined variable $con in E:\New folder\htdocs\Online Property Sales System\Src\register.php on line 16

Fatal error: Uncaught Error: Call to a member function query() on null in E:\New folder\htdocs\Online Property Sales System\Src\register.php:16 Stack trace: #0 {main} thrown in E:\New folder\htdocs\Online Property Sales System\Src\register.php on line 1


What I have tried:

<?php

require 'config.php';

$fname=$_POST["fname"];
$lname=$_POST["lname"];
$uname=$_POST["uname"];
$pass=$_POST["pass"];
$mno=$_POST["mno"];
$dob=$_POST["dob"];
$email=$_POST["email"];

$sql = "INSERT INTO customer(Customer_Id,F_Name,L_Name,User_Name,Password,Mobile_Number,DOB,Email)
         VALUES ('' , '$fname' , '$lname' , '$uname' , '$pass' , '$mno', '$dob' , '$email' )";

if($con -> query($sql))
{
	echo "<script> alert('Create Account Successfully')</script>";
}

else {
	echo "<script> alert('Error')</script>";
}

$con -> close() ;



?>
Posted
Updated 10-May-22 22:39pm
Comments
Chris Copeland 11-May-22 4:13am    
Before someone swoops in with the fact you're using string concatenation/interpolation for the SQL query when you shouldn't be, the error you're getting as exactly what it says. The $con -> query line cannot execute because this $con variable doesn't exist. You need to check your config.php file to make sure that the variable is definitely called $con

1 solution

You don't establish any connection in that code, so $con is not defined, and the query cannot work. You have to establish a connection before you can query a database!

Which is lucky for you, because the way you are doing this is very dangerous. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 

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