+ 1

I have this error shown in the browser: Notice: Undefined variable: link in C:\xampp\htdocs\xxxx\login.php on line 40

Hi! The browser shows the errors below in php after clicking login. so it fails to go to home page It shows the following errors Connected successfully Notice: Undefined variable: link in C:\xampp\htdocs\social\login.php on line 40 Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\social\login.php on line 40 The code below: <?php // Initialize the session session_start(); // Check if the user is already logged in, if yes then redirect him to Home page if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){ header("location: home.php"); exit; } // Include config file require_once "connect.php"; // Define variables and initialize with empty values $username = $password = ""; $username_err = $password_err = ""; // Processing form data when form is submitted if($_SERVER["REQUEST_METHOD"] == "POST"){ // Check if username is empty if(empty(trim($_POST["UserName"]))){ $username_err = "Please enter username."; } else{ $username = trim($_POST["UserName"]); } // Check if password is empty if(empty(trim($_POST["Password"]))){ $password_err = "Please enter your Password."; } $password = trim($_POST["Password"]); } // Validate credentials if(empty($username_err) && empty($password_err)){ // Prepare a select statement $sql = "SELECT id, UserName, Password FROM members WHERE username = ?"; if($stmt = mysqli_prepare($link, $sql)){ //line 40 here echo mysqli_error($conn); // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "s", $param_username); // Set parameters $param_username = $username; // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ // Store result

17th Jan 2020, 5:38 AM
Stephen Phiri
Stephen Phiri - avatar
4 Antworten
+ 2
Stephen Phiri Change this line to this at line 40 if ($stmt = $mysqli->prepare($sql)) to if ($stmt = mysqli_prepare($link, $sql)) where $link is the variable of connection file where connection code is stored in $link
17th Jan 2020, 6:48 AM
DishaAhuja
DishaAhuja - avatar
+ 1
Notice: Undefined variable Happens when you try to use a variable that wasn't previously defined.
17th Jan 2020, 6:37 AM
DishaAhuja
DishaAhuja - avatar
+ 1
People usually use $link or $conn as name for variables that contains a database connection object. In your code I see both are used. In `mysqli_prepare` you use $link, and in `mysqli_error` you use $conn. You need to decide, which of these two ($link and $conn) is the actual and valid database connection object to use. Considering the code is only partial copy, pasted as raw text, and truncated - It would rather be difficult to assist you. I would suggest you to save the code in SoloLearn and share the code link instead of raw text like that (notice that it is also truncated due to character limits). If your code is more than 10-15 lines it is best to save and share the link. Follow the below guide to sharing links, in case you didn't know how https://www.sololearn.com/post/74857/?ref=app
17th Jan 2020, 6:57 AM
Ipang
0
I removed $link because I didn't included it but I don't know about this. Warning: mysqli_prepare() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\xxxx\login.php on line 40
17th Jan 2020, 6:32 AM
Stephen Phiri
Stephen Phiri - avatar