Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 插入到不创建新行中_Php_Mysql_Pdo_Registration - Fatal编程技术网

Php 插入到不创建新行中

Php 插入到不创建新行中,php,mysql,pdo,registration,Php,Mysql,Pdo,Registration,我一直想搞乱注册系统。但是,当我尝试将信息插入数据库时。没有生成新行。我没有收到任何错误,我的代码似乎是合法的。是否有我不知道的关于插入的内容 $username = $_POST['regusername']; $email = $_POST['regemail']; $password = $_POST['regpassword']; $cpassword = $_POST['regpasswordcon']; $firstname = $_POST['regfirstname']; $la

我一直想搞乱注册系统。但是,当我尝试将信息插入数据库时。没有生成新行。我没有收到任何错误,我的代码似乎是合法的。是否有我不知道的关于插入的内容

$username = $_POST['regusername'];
$email = $_POST['regemail'];
$password = $_POST['regpassword'];
$cpassword = $_POST['regpasswordcon'];
$firstname = $_POST['regfirstname'];
$lastname = $_POST['reglastname'];
//check username for weird symbols
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $username)){
    // one or more of the 'special characters' found in string
    //header("Location: /register.php");
    echo "Your username should only contain letters and numbers";
    exit;
}

//check if username is taken
$check = $con->prepare("SELECT * FROM accounts WHERE username=:user");
$check->bindParam(':user',$username);
$check->execute();
$result = $check->fetch(PDO::FETCH_ASSOC);

if(!empty($result)){
    //header("Location: /register-page.php"); //direct browser back to sign in
    echo "User is already taken";
    exit;
}else{ //otherwise proceed to register new user

    //Hashing of password
    $hpassword = password_hash($password, PASSWORD_DEFAULT);

    //Prepared statements for SQL injection prevention
    $query = $con->prepare("INSERT INTO accounts (username, password, email, firstname, lastname) VALUES (:name,:hpassword,:email,:fname,:lname) ");

    //bind parameters
    $query->bindParam(':name',$username);
    $query->bindParam(':hpassword',$hpassword);
    $query->bindParam(':email',$email);
    $query->bindParam(':fname',$firstname);
    $query->bindParam(':lname',$lastname);
    $query->execute();
}

$con似乎是PDO的一个实例,对吧?是否已将错误报告模式设置为PDO::ERRMODE_EXCEPTION,如中所述?如果不是,您的脚本缺少错误处理。您好。我想可能是因为您试图插入的一个或一些变量为空。为什么不试着回显它们中的每一个,看看它们是否没有emtpy值?也许这会有帮助。你的脚本很容易受到竞争条件的影响。在不锁定表的情况下,另一个php实例可以在另一个php实例的SELECT和INSERT命令之间修改数据库,两次插入具有完全相同用户名的记录。最好为用户名字段创建一个唯一的约束。它们是不同的;-这并不是一个真正的PDO错误,但MySQL服务器会抱怨数据。1062是ER_DUP_条目,请参阅,因此表的一个或多个字段上已经存在唯一约束。只需删除SELECT查询,只保留INSERT并检查错误代码。如果是1062->重复输入。你真的应该使用PHP来处理密码安全性。