I am new to PHP and my requirement is to check the user email id in database and if exists , navigate to next page. But by SQL query is not working in PHP , and working in SQL database.
My database structure :
My HTML Code : index.html
<!DOCTYPE html>
<html>
<body>
<form action="checkform.php" method="post">
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
</body>
</html>
PHP: checkform.php
<?php
try
{
$connection = mysql_connect("localhost:3306","root","");
mysql_select_db("mobileblog", $connection);
$emailid = $_POST['email'];
echo $emailid;
$sql = "SELECT Name from table WHERE email=" . $_POST['email'];
$result = mysql_query($sql);
echo $result;
if(!$result) { echo "<p class='error'>Error: No such email address</p>"; }
// note that the if is asking if there is no result
else {
while ($row = mysql_fetch_assoc($result)) {
echo "<p class='success'>Welcome " . $row['Name'] . "!!</p>";
}
} // end
//mysql_query(" // suggest here to validate against emails in db");
mysql_close($connection);
echo "SUCCESS";
}
catch(Exception $e)
{
echo $e->getMessage();
// Note: Log the error or something
}
?>
My page2.html
<!DOCTYPE html>
<html>
<body>
<h1> Welcome to Page 2</h1>
</body>
</html>
But when running the index.html page , facing the error: (which eventually does nt execute the mysql query),But running the sql query is successful
Error: No such email address
SUCCESS
PHP Query (failed)
$sql = "SELECT `Name` FROM `table` WHERE email=\'jxxx@gmail.com\'";
SQL Query (Success)
SELECT `Name` FROM `table` WHERE email='jxxx@gmail.com'
Why does it fail to run the query ?