Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so this is my code which named("loginlinkstudent.php")

<?php
session_start();
?>

<?php
$x = $_POST[ "sid" ];
$y = $_POST[ "pass" ];

include( "database.php" );
//searching login id and password entered in $x & $y
$sql = "select * from studenttable where Eid='" . $x . "' and Pass='" . $y . "'";

$result = mysqli_query( $connect, $sql );

if ( $result->num_rows > 0 )

//session create
{
	if ( $row = $result->fetch_assoc() ) {
		$_SESSION[ "sidx" ] = $row[ "Eid" ];
		$_SESSION[ "fname" ] = $row[ "FName" ];
		$_SESSION[ "lname" ] = $row[ "LName" ];
		$_SESSION[ "seno" ] = $row[ "Eno" ];

	} //redirecting to welcome student page
	header( 'Location:welcomestudent.php' );

} else {
	//error message if SQL query fails
	echo "<h3><span style='color:red; '>Invalid Student ID & Password. Page Will redirect to Login Page after 2 seconds </span></h3>";
	header( "refresh:3;url=studentlogin.php" );
}
//close the connection
$connect->close();

?>




and this is my error:

Warning: Undefined variable $connect in C:\xampp\htdocs\stemchat\loginlinkstudent.php on line 13

Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, null given in C:\xampp\htdocs\stemchat\loginlinkstudent.php:13 Stack trace: #0 C:\xampp\htdocs\stemchat\loginlinkstudent.php(13): mysqli_query(NULL, 'select * from s...') #1 {main} thrown in C:\xampp\htdocs\stemchat\loginlinkstudent.php on line 13

What I have tried:

i had tried to delete line 13 but end up fail
Posted
Updated 26-Jun-21 6:41am

1 solution

Read the error message:
Undefined variable $connect in C:\xampp\htdocs\stemchat\loginlinkstudent.php on line 13

What do you think that means?

If you don't declare a variable before you try to use it, what value do you think it will have?
Go back to the example code you used to create that, and find out what they did to create $connect. Hint: it'll involve a connection string to tell the system what database you are trying to use...


And 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