使用php更新mysql中特定行的特定列?

时间:2017-02-27 18:51:32

标签: php mysql

我一直在尝试为我的网站创建重置密码功能,用户可以在其中插入已注册的电子邮件并生成随机OTP,并将与用户的详细信息存储在同一列中如

id firstname lastname username email resetpassword
1  name      last     user     email OTP

这是我的代码,但它不起作用。

<?php
require 'dbh.php';
session_start();

$random = mt_rand(1000,1000000);

$email = $_POST['email'];

$sql = "SELECT Email FROM registeredusers WHERE Email='$email'";
$result = mysqli_query($connection,$sql);
$emailCheck = mysqli_num_rows($result);

if (empty($email))
{
    echo "please fill out all the fields";
}

else{

    $result = mysqli_query($connection,$sql);
    $sql = "UPDATE registeredusers SET ResetPassword='$random' WHERE Email='$email'";
Header("Location: submitOTP.php");
}

?>

我只是尝试这个,所以表单看起来像这样

<form action="resetPassword.php" method="POST">

<input type="text" value="email" name="email"></input>
<input type="submit" value="submit"></input>

</form>

1 个答案:

答案 0 :(得分:1)

您应该在执行查询后执行sql代码

$sql = "UPDATE registeredusers SET ResetPassword='$random' WHERE Email='$email'";
$result = mysqli_query($connection,$sql);

否则你只需重复上一个(选择)查询..

小心避免sql注入看看准备好的查询和绑定参数