简体   繁体   English

PHP MySQL / PDO UPDATE查询将不会执行

[英]PHP MySQL/PDO UPDATE Query Won't Execute

I'm very new to all of these things and I'm just really stumped on this. 我对所有这些事情都很陌生,我真的很难过。 I've been trying for a day and a half to get this part of the code to work, and I've tried numerous different things. 我已经尝试了一天半让这部分代码工作,我尝试了很多不同的东西。 It's just not wanting to work for me. 它只是不想为我工作。

Here's the whole script 这是整个脚本

<?php
$dbusername = "****";  // info works to connect to login
$dbpassword = "****";  // and everything works fine retrieving
$dbhost = "localhost"; // the email to send the code to (which all works)
$dbname = "****"; 
try {
$conn = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbusername, $dbpassword);
}
catch(PDOException $ex)
{
    $msg = "Failed to connect to the database";
}

function getToken($length=32){
//redacted - working and unrelated, suffice it to say the token returns properly
return $token;
}

if (isset($_POST["ForgotPassword"])) {

    if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
        $email = $_POST["email"];

    }else{
        echo "Email is invalid.";
        exit;
    }

    // Check to see if a user exists with this e-mail
    $query = $conn->prepare('SELECT email FROM users WHERE email = :email');
    $query->bindParam(':email', $email);
    $query->execute();
    $userExists = $query->fetch(PDO::FETCH_ASSOC);
    $conn = null

    if ($userExists["email"])
    {
        $resetpass = getToken();        

        try {
            $conn = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbusername, $dbpassword);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $stmt = $conn->prepare('UPDATE users SET passwordreset=:resetpass WHERE email=:email');
            $stmt->bindParam(':resetpass', $resetpass);
            $stmt->bindParam(':email', $email);
            $stmt->execute();

            echo $stmt->rowCount() . " records UPDATED successfully";
            }
        catch(PDOException $e)
            {
            echo $sql . "<br>" . $e->getMessage(); //$sql not set anymore
            }

        $conn = null;



    // Create a url which we will direct them to reset their password
    $pwrurl = "*******/reset_password.php?q=".$resetpass;

    // Mail them their key
    $mailbody = "redacted \n\n" . $pwrurl;
    mail($userExists["email"], "redacted", $mailbody);
    echo "Your password recovery key has been sent to your e-mail address.";

    }

    else
        echo "No user with that e-mail address exists.";
    }
?>

Without this query, everything else works famously. 没有这个查询,其他一切都很有名。 It breaks and won't continue here. 它打破了,不会在这里继续。 It never echos the success or failure. 它永远不会成功或失败。

Edit Here's the HTML form too 编辑这里也是HTML表单

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Forgot Password</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<form action="change.php" method="POST">
<table align="center" width="30%" border="0">
<div> 
<tr>
<td><input type="text" name="email" placeholder="you@example.com" required /></td>
</tr>
<tr>
<td><button type="submit" name="ForgotPassword" value=" Request Reset ">Reset</button></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

Now that you've posted your full code... 现在你已经发布了完整的代码......

Your code is failing because of this wee little bug in your code that is causing some BIG problems. 您的代码失败,因为您的代码中的这个错误导致了一些问题。

$conn = null
            ^ right there.

I know this is considered as an off-topic question, but we've been at this for so long, I felt that I had to submit it as answer. 我知道这被认为是一个偏离主题的问题,但我们已经这么久了,我觉得我必须提交它作为答案。 (Consult Special note below). (请参阅下面的特别说明 )。 It's not completely off-topic. 这不是完全偏离主题的。

There is a missing semi-colon in there; 那里有一个缺少的分号; add it. 添加它。

$conn = null;

Had error reporting been set to catch and display errors in your code, would have thrown you a parse error. 如果错误报告已设置为捕获并显示代码中的错误,则会抛出一个解析错误。

Add error reporting to the top of your file(s) which will help find errors. 错误报告添加到文件的顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production. 旁注:显示错误只能在分段中完成,而不能在生产中完成。


However, you should use proper bracing for all your conditional statements, such as this one: 但是,您应该对所有条件语句使用适当的支撑,例如:

else
    echo "No user with that e-mail address exists.";

as that could have adverse effects. 因为这可能会产生不利影响。

Special note: 特别说明:

There is another thing though and it's this variable $token that you've return 'ed in your getToken() function. 还有另外一件事,你可以在你的getToken()函数中return '这个变量$token You're not using it anywhere, so it's unsure as to what you want to do with it exactly. 你没有在任何地方使用它,所以不确定你想要用它做什么。

As noted in a comment under your answer, $sql isn't doing anything; 正如你的回答中的注释所述, $sql没有做任何事情; it's undefined. 这是未定义的。 However, that won't cause your code to fail, but just throw an undefined variable sql notice, when error reporting is set to catch and display. 但是,当错误报告设置为catch和display时,这不会导致代码失败,而只是抛出一个未定义的变量sql通知。

As im not able to comment, I am also new to php and pdo. 由于我无法评论,我也是php和pdo的新手。 I checked the code but it seems fine. 我检查了代码,但似乎很好。

only error found 1)$sql variable 2) $conn=null;(semi colon missing) after first query. 只发现错误1)$ sql变量2)第一次查询后$ conn = null;(半冒号缺失)。

Can you provide db side details,table details. 您能否提供数据库端详细信息,表详细信息。 So that i can try with that and will try to find a solution. 这样我就可以尝试并尝试找到解决方案。

http://php.net/manual/en/pdo.exec.php#refsect1-pdo.exec-examples http://php.net/manual/en/pdo.exec.php#refsect1-pdo.exec-examples

If you look at the example I provided you will see that you can do this without all the bindParam functions. 如果你看一下我提供的例子,你会看到你可以在没有所有bindParam函数的情况下完成这个。 Settings your SQL up before you execute can be helpful. 在执行之前设置SQL可能会有所帮助。

Although this Isn't tested - my best advice would be to create an $sql variable that you use to store your SQL in as you create it. 虽然这没有经过测试 - 我最好的建议是创建一个$ sql变量,用于在创建SQL时存储SQL。

Something to this extent will let you see exactly what your $sql is and you can better find your problems after you see exactly what you are trying to execute. 在某种程度上,你可以看到你的$ sql究竟是什么,你可以在看到你正在尝试执行的内容后更好地找到问题。

$sql = "UPDATE users SET passwordreset = '" . $resetpass . "' WHERE email ='" . $email . "' ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM