输入值未更新

时间:2017-10-26 14:55:06

标签: php validation

基本上,我要做的是验证用户在注册到系统时输入的值。当他点击提交按钮时,数据将进入处理信息的脚本,当他发现用户名和电子邮件已被占用时,他将被带回同一页面但保留$ _POST值。

问题是,当我修改用户名和电子邮件字段中的值时,它仍然认为我在检查数据库时已经使用了用户名和电子邮件的错误,但事实并非如此。

这是我的代码:

<form method="POST" autocomplete="off" enctype="multipart/form-data">
<?php include '../../includes/backend/register.php'; ?>
    <label for="">First Name:</label>
    <input type="text" class="form-control" name="fname" required placeholder="ex. John" onkeyup="this.value=this.value.replace(/[^a-zA-Z ]/g,'');" value="<?php echo isset($_POST[" fname "]) ? $_POST["fname "] : ''; ?>">
    <label for="">Last Name:</label>
    <input type="text" class="form-control" name="lname" required placeholder="ex. Doe" onkeyup="this.value=this.value.replace(/[^a-zA-Z -]/g,'');" value="<?php echo isset($_POST[" lname "]) ? $_POST["lname "] : ''; ?>" ">
                <label for=" ">Phone:</label>
                <input type="number " class="form-control " name="phone " required placeholder="ex. 09351231234 " onkeyup="this.value=this.value.replace([0-9], ''); " value="<?php echo isset($_POST[ "phone"]) ? $_POST[ "phone"] : ''; ?>"">
    <label for="">Address:</label>
    <input type="text" name="address" list="citynames" class="form-control" placeholder="ex. Dumaguete" autocomplete="on" value="<?php echo isset($_POST[" address "]) ? $_POST["address "] : ''; ?>">
    <datalist id="citynames">
        <option value="Amlan">Amlan</option>
        <option value="Ayungon">Ayungon</option>
        <option value="Bacong">Bacong</option>
        <option value="Bais City">Bais City</option>
        <option value="Basay">Basay</option>
        <option value="Bayawan">Bayawan</option>
        <option value="Bindoy">Bindoy</option>
        <option value="Canlaon City">Canlaon City</option>
        <option value="Dauin">Dauin</option>
        <option value="Dumaguete City">Dumaguete City</option>
        <option value="Guihulngan">Guihulngan</option>
        <option value="Jimalalud">Jimalalud</option>
        <option value="La Libertad">La Libertad</option>
        <option value="Mabinay">Mabinay</option>
        <option value="Manjuyod">Manjuyod</option>
        <option value="Pamplona">Pamplona</option>
        <option value="San Jose">San Jose</option>
        <option value="Siaton">Siaton</option>
        <option value="Sibulan">Sibulan</option>
        <option value="Sta. Catalina">Sta. Catalina</option>
        <option value="Tanjay">Tanjay</option>
        <option value="Tayasan">Tayasan</option>
        <option value="Valencia">Valencia</option>
        <option value="Vallehermoso">Vallehermoso</option>
        <option value="Zamboanguita">Zamboanguita</option>
    </datalist>
    <label for="">Username:</label>
    <input type="text" class="form-control" name="username" required placeholder="ex. johndoe29" value="<?php echo isset($_POST[" username "]) ? $_POST["username "] : ''; ?>">
    <label for="">Email:</label>
    <input type="email" class="form-control" name="email" required placeholder="ex. jdoe@domain.com" autocomplete="off" value="<?php echo isset($_POST[" email "]) ? $_POST["email "] : ''; ?>">
    <label for="">Password:</label>
    <input type="password" class="form-control" name="match" min="6" required placeholder="*******" id="p1">

    <label for="">Confirm Password:</label>
    <input type="password" class="form-control" name="password" min="6" required placeholder="*******" id="p2" onchange="confirm()">
    <p class="note" style="visibility: hidden; font-size: 11px; color: red; margin-top: 2px;">Password does not match</p>

    <label for="">Profile Image:</label>
    <input type="file" class="form-control" name="image" required>

    <input type="Submit" name="submit" value="Register" class="btn btn-primary" id="submit">

我的后端:

    if (isset($_POST['submit'])) {
    $fname = mysqli_real_escape_string($con, $_POST['fname']);
    $lname = mysqli_real_escape_string($con, $_POST['lname']);
    $phone = mysqli_real_escape_string($con, $_POST['phone']);
    $address = mysqli_real_escape_string($con, $_POST['address']);
    $username = mysqli_real_escape_string($con, $_POST['username']);
    $email = $_POST['email'];
    $password = $_POST['password'];
    $image = $_FILES['image']['name'];
    $temp = $_FILES['image']['tmp_name'];
    $rand = rand(1, 1000000);
    $loc = "assets/images/users/$rand.$image";
    move_uploaded_file($temp, "../../assets/images/users/$rand.$image");

    $r_user = mysqli_query($con, "SELECT username FROM user WHERE username = '$username'");
    $r_email = mysqli_query($con, "SELECT email FROM user WHERE email = '$email'");

    if ($r_user) {
        echo "That username is already taken<br>";
    }

    if ($r_email) {
        echo "That email is already taken<br>";
    }

    if (!$r_user && !$r_email) {
        mysqli_query($con, "INSERT INTO `user` (`id`, `fname`, `lname`, `phone`, `address`, `username`, `email`, `image`, `password`) VALUES (NULL, '$fname', '$lname', '$phone', '$address', '$username', '$email', '$loc', '$password')");
        header("Location: welcome.php");
    }
}

1 个答案:

答案 0 :(得分:2)

您没有正确使用mysqli功能。 mysqli_query返回true的事实并不意味着您的查询返回任何行。 您需要使用mysqli_num_rows方法。 这样的事情应该这样做:

if (mysqli_num_rows($r_user) > 0) {
  echo "That username is already taken<br>";
}

我还建议您使用面向对象的样式而不是程序。