尝试通过检查用户名是否已存在而插入新寄存器时没有结果

时间:2014-09-30 13:30:45

标签: php mysqli

尝试通过检查用户名是否已存在而尝试插入新寄存器时没有结果。 我已尝试使用msql插入查询而不是msqli - 也没有工作。 谁能形容我做错了什么?

这是我的代码:

<?php

if (isset($_POST["submit"])) {
    if (!empty($_POST['user']) && !empty($_POST['pass'])) {
        $user = $_POST['user'];
        $pass = $_POST['pass'];
        $con = mysqli_connect("xxxxxxx", "xxxx", "xxxx", "xxxxx") or die(mysqli_error());
        mysqli_select_db($con, 'xxxxxx') or die("cannot select DB");
        mysqli_set_charset($con, 'utf8');

        if ($result = mysqli_query($con, "SELECT * FROM login WHERE username='" . $user . "'")) {
            $row_cnt = mysqli_num_rows($result);
            if ($row_cnt == 0) {
                $sql = "INSERT INTO login(username,password)VALUES('$user','$pass')";
                $query = mysqli_query($con, $sql);
                if ($query) {
                    echo "Account Successfully Created";
                } else {
                    echo "Failure!";
                }
            } else {
                echo "That username already exists! Please try again with another.";
            }

            /* close result set */
            //mysqli_free_result($result);          
        }
    } else {
        echo "All fields are required!";
    }
}
?>

0 个答案:

没有答案