gpt4 book ai didi

php 哪里查询不起作用

转载 作者:行者123 更新时间:2023-11-28 04:19:36 25 4
gpt4 key购买 nike

我是 PHP 的新手,我的要求是检查数据库中的用户电子邮件 ID,如果存在,则导航到下一页。但是通过 SQL 查询在 PHP 中不起作用,而在 SQL 数据库中起作用。

我的数据库结构:

enter image description here

我的 HTML 代码: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
}
?>

我的page2.html

<!DOCTYPE html>
<html>
<body>
<h1> Welcome to Page 2</h1>
</body>
</html>

但是在运行index.html页面时,报错:(最终没有执行mysql查询),但是运行sql查询成功

Error: No such email address
SUCCESS

PHP 查询(失败)

$sql = "SELECT `Name` FROM `table` WHERE email=\'jxxx@gmail.com\'";

SQL 查询(成功)

SELECT `Name` FROM `table` WHERE email='jxxx@gmail.com'

为什么运行查询失败?

最佳答案

按照约翰的要求:

使用引号

WHERE email= '".$emailid."'

$emailid = stripslashes($_POST['email']);
$emailid = mysql_real_escape_string($emailid);
$sql = "SELECT `Name` from `table` WHERE `email`= '".$emailid."'";

结合 $emailid = $_POST['email'];

代替

WHERE email=" . $_POST['email']

这既会导致错误,又会在使用该方法时对 SQL 注入(inject)开放。

您现在的密码是开放给SQL injection .使用 mysqli with prepared statements , 或 PDO with prepared statements它们更安全


旁注:

如果您的表名确实名为 table,请将其用反引号括起来(仅供引用)。

$sql = "SELECT Name from `table`...

将错误报告添加到文件顶部。

error_reporting(E_ALL);
ini_set('display_errors', 1);

and or die(mysql_error())mysql_query()


编辑:重写

<?php
try
{
$connection = mysql_connect("localhost:3306","root","");
mysql_select_db("mobileblog", $connection);
$emailid = stripslashes($_POST['email']);
$emailid = mysql_real_escape_string($emailid);
echo $emailid;
$sql = "SELECT `Name` from `table` WHERE `email`= '".$emailid."'";
$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
}
?>

编辑#2(帮忙)

<?php

$connection = mysql_connect("localhost:3306","root","");
mysql_select_db("mobileblog", $connection);
$emailid = stripslashes($_POST['email']);
$emailid = mysql_real_escape_string($emailid);
$sql = "SELECT `Name` from `table` WHERE `email`= '".$emailid."'";
$result = mysql_query($sql);

// if($result) {
if(mysql_num_rows($result) > 0){
header("Location: page2.php");
exit;
}

else {

echo "<p class='error'>Error: No such email address</p>";

} // brace for else

mysql_close($connection);

?>

关于php 哪里查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com