Having a problem linking PHP WEBPAGES, i get error : Fatal error : Call to undef
ID: 3857786 • Letter: H
Question
Having a problem linking PHP WEBPAGES, i get error : Fatal error: Call to undefined function mysql_real_escape_string() in C: mpphtdocsAddUser.php on line 20
Register.php
----------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Fred's Market Biz Login </title>
<link rel="stylesheet" type="text/css" href="Style.css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<table>
<tr>
<form name="form1" method="post" action="AddUser.php">
<td>
<table>
<tr>
<td colspan="2"><strong>Fred’s Market Biz Registration</strong></td>
</tr>
<tr>
<td>Username</td>
<td><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Register"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
-----------------------------------------------------------------------------------------------------
CheckLogin.php
--------------------------------------------------------------------------
<?php
session_start();
$host="localhost"; // Host name
$username="Fred"; // Mysql username
$password="Farmer#Fred"; // Mysql password
$db_name="FredBiz"; // Database name
$tbl_name="users"; // Table name
$_SESSION['LoginFail'] ="False"; //login failure sentinel value
// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password", "$db_name")or die("cannot connect");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$encrypted_mypassword=md5($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysqli_query($con,$sql);
// Mysql_num_row is counting table row
$count=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $encrypted_mypassword;
$_SESSION['LoginBool'] = "False";
header("location:DescSelect.php");
}
else {
$_SESSION['LoginFail'] = "True";
header("location:LoginPage.php");
}
mysqli_close($con);
?>
---------------------------------------------------------------------------------------------------------
AddUser.php
<?php
$host="localhost"; // Host name
$username="Fred"; // Mysql username
$password="Farmer#Fred"; // Mysql password
$db_name="FredBiz"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password", "$db_name") or die("cannot connect");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string('$myusername');
$mypassword = mysql_real_escape_string('$mypassword');
// encrypt password using MD5 hash
$encrypted_mypassword=md5($mypassword);
$sql="INSERT INTO $tbl_name (username, password) VALUES ('$myusername', '$encrypted_mypassword')";
$result=mysqli_query($con,$sql);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$encrypted_mypassword'";
$result=mysqli_query($con,$sql);
// Mysql_num_row is counting table row
$count=mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
$Message= $myusername." you account was created successfully <br> Click your brownsers back button to login";
}
else {
$Message= "User was not registered";
}
mysqli_close($con);
?>
<html>
<head>
<title> Fred’s Market Biz Login </title>
<link rel="stylesheet" type="text/css" href="Style.css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
echo $Message;
?>
</body>
</html>
----------------------------------------------------------------------------------------
Explanation / Answer
If you are connecting your database with "mysqli" , you cannot use mysql functions with it.
Therefore replace your line
$myusername = mysql_real_escape_string('$myusername');
$mypassword = mysql_real_escape_string('$mypassword');
with
$myusername = mysqli_real_escape_string('$myusername');
$mypassword = mysqli_real_escape_string('$mypassword');
use mysqli instead of mysql
Then you will not get the error.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.