为什么我的用户被添加到一个表而不是另一个

时间:2017-06-20 01:09:17

标签: php mysql

我正在为我的用户创建个人资料图片上传系统。注册后,php代码应该在表中创建一个用户" user"并在" profileImg"中创建用户表。我的日志中没有错误,但是用户被添加到" user"但不是" profileImg"。谁能请你帮忙。提前谢谢。

SIGNUP.INC.PHP:

<?php  
session_start();
include '../dbh.php';

$respond = array(
    'status'   => true,
    'message'  => 'There was an error',
    'redirect' => '../profile.php',
    'errors',
);

if (isset($_POST['submit'])) {

    $first = mysqli_real_escape_string($conn, $_POST['first']);
    $last = mysqli_real_escape_string($conn, $_POST['last']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

    $errorEmpty = false;
    $errorEmail = false;


if (empty($first) || empty($last) || empty($email) || empty($pwd)) {

    $respond['errors'][] = "Please fill out all fields!";
    $respond['errorEmpty'] = true;

} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    $respond['errors'][] = "Please enter a valid email address!";
    $respond['errorEmail'] = true;

} else {

    $sql = "SELECT email FROM user WHERE email='$email'";
    $result = mysqli_query($conn, $sql);
    $emailcheck = mysqli_num_rows($result);


    if ($emailcheck > 0) {
        $respond['errors'][] = "That email address already exists!";
        $respond['errorEmail'] = true;
    } 



else {
    $encryptpwd = password_hash($pwd, PASSWORD_DEFAULT);
    $sql = "INSERT INTO user (first, last, email, pwd)
    VALUES ('$first', '$last', '$email', '$encryptpwd')";
    $result = mysqli_query($conn, $sql);

    $sql = "SELECT * FROM user WHERE email='$email' AND first='$first'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $email = $row['id'];
            $sql = "INSERT INTO profileImg (email, status)
                    VALUES ('$email', 1)";

        }
    }


    }

}
}
echo json_encode($respond);
?>

PROFILE.PHP:

1 个答案:

答案 0 :(得分:0)

这必须违反数据库级别。

看到你的这个块:

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $email = $row['id'];
        $sqlProfile = "INSERT INTO profileImg (email, status)
                VALUES ('$email', 1)";
    }
}

我非常确定在您的数据库中,profileImg表的电子邮件列是varchar,尽管您将其作为int $email = $row['id'];插入

将此行替换为此$email = $row['email'];

更改后的代码:

if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $email = $row['email'];
        $sqlProfile = "INSERT INTO profileImg (email, status)
                VALUES ('$email', 1)";
        mysqli_query($conn, $sqlProfile);
    }
}

更新:添加mysqli_query($conn, $sqlProfile);以执行查询