php - 故障排除 'undefined variable' 错误

标签 php mysql

我不断收到以下错误 undefined variable :第 33 行上的密码如何纠正此问题?所以这个错误将停止显示。

这是 PHP 代码。

$first_name = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['first_name']))));
$password1 = mysqli_real_escape_string($mysqli, $purifier->purify(strip_tags($_POST['password1'])));
$password2 = mysqli_real_escape_string($mysqli, $purifier->purify(strip_tags($_POST['password2'])));




// Check for a password and match against the confirmed password:
if ($password1 == $password2) {
    $sha512 = hash('sha512', $password1);
    $password = mysqli_real_escape_string($mysqli, $sha512);
} else {
    echo '<p class="error">Your password did not match the confirmed password!</p>';
}



//If the table is not found add it to the database
if (mysqli_num_rows($dbc) == 0) {
        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        $dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, first_name, password) 
                                     VALUES ('$user_id', '$first_name', '$password')");
}



//If the table is in the database update each field when needed
if ($dbc == TRUE) {
        $dbc = mysqli_query($mysqli,"UPDATE users 
                                     SET first_name = '$first_name', password = '$password' 
                                     WHERE user_id = '$user_id'");

        echo '<p class="changes-saved">Your changes have been saved!</p>';

}

最佳答案

只有一处为 $password 赋值

if ($password1 == $password2) {
    $sha512 = hash('sha512', $password1);
    $password = mysqli_real_escape_string($mysqli, $sha512);
}

因此,如果不满足条件,则不会有 $password。在这种情况下,无论如何执行 UPDATE 查询都是没有意义的。

关于php - 故障排除 'undefined variable' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2580811/

相关文章:

java - 查询以检查时间戳

php - 为什么我的 SQL 查询没有将数据放入数据库?

php - 如何安全地使用 file_get_contents?

php - Concat(2 个字段)LIKE 使用 Propel/Mysql/PHP 提交的值

mysql - 优化代码以在一个巨大的表中填充一个新列

php - 如何循环遍历 PDO 语句并在 if 语句上写入数据

php - JQuery 自动完成,用 pHp json 中的数据填充

php - 使用curl加载使用JS提交外部表单的HTML文件

MySQL 带有 CASE WHEN、INNER JOIN 和 GROUP BY1 的多重 SELECT 查询

mysql选择在哪里并使用多个相同的变量