我想实现的功能,其中它会检查是否如果用户在我的数据库已经存在,是它会插入所有注册数据,但它似乎没有工作=(可能有人请帮我找出错误所在。真的很感谢所有的答案提前。PHP检查用户是否存在PDO
<?php
require '../ppuyakul/php/db_conn.php';
$message = '';
//Prepare date
$DOB = date("Y-m-d", strtotime($_POST['year'].'-'. $_POST['month'].'-'. $_POST['day']));
$accessType = "0";
//Check enpty field
if(!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['fullname']) && !empty($_POST['username']) && !empty($_POST['password_confirmation']) && !empty($_POST['gender']) && !empty($_POST['country']) && !empty($_POST['state']) && !empty($_POST['city']) && !empty($_POST['day']) && !empty($_POST['month']) && !empty($_POST['year'])):
// Enter the new user in the database
$sql = "INSERT INTO assignment2 (fullname, username, email, password, gender, country, state, city, DOB, type) VALUES (:fullname, :username, :email, :password, :gender, :country, :state, :city, :DOB, :type)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':fullname', $_POST['fullname']);
$stmt->bindParam(':username', $_POST['username']);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
$stmt->bindParam(':gender', $_POST['gender']);
$stmt->bindParam(':country', $_POST['country']);
$stmt->bindParam(':state', $_POST['state']);
$stmt->bindParam(':city', $_POST['city']);
$stmt->bindParam(':DOB', $DOB);
$stmt->bindParam(':type', $accessType);
$chk = $conn->prepare("SELECT username FROM assignment2 WHERE username = :name");
$chk->bindParam(':name', $username);
$chk->execute();
if($chk->rowCount() > 0):
$message = 'Error ! ! User already exists';
else:
if($stmt->execute()):
$message = 'Successfully created new user';
else:
$message = 'Sorry there must have been an issue creating your account';
endif;
endif;
endif;
?>
$ username是如何设置的? –
嗨Paul感谢so muc h为您的回复,我尝试在这里声明 '$ all_rows = $ chk-> fetchAll(PDO :: FETCH_ASSOC); 的foreach($ ALL_ROWS为$行): $用户名= $行[ “用户名”]; $ _GLOBAL ['username'] = $ username; endforeach;' 之前'如果($ chk-> rowCount时()> 0):' 但仍没有运气 –
我看不出有什么最初发布的代码?当我搜索$ username时(在我的第一条评论之前),这个页面上只有一个'点击'。上面的代码中没有foreach。为你的rowCount检查添加一个else,并打印'找不到行'(或其他),看看你得到了什么。你也可以使用$ username来查看输出结果。 –