Php 返回的记录数。在这种情况下,OP绝对不需要它。@Dharman好的,谢谢。我给它添加了另一个选项。我同意有不止一种方法可以用来检查PDO中是否存在记录。我不明白为什么不调用它。最后两条$msg语句在我用代码创建新帐户时出现$msg在用echo语句重新加载时
Php 返回的记录数。在这种情况下,OP绝对不需要它。@Dharman好的,谢谢。我给它添加了另一个选项。我同意有不止一种方法可以用来检查PDO中是否存在记录。我不明白为什么不调用它。最后两条$msg语句在我用代码创建新帐户时出现$msg在用echo语句重新加载时,php,mysql,Php,Mysql,返回的记录数。在这种情况下,OP绝对不需要它。@Dharman好的,谢谢。我给它添加了另一个选项。我同意有不止一种方法可以用来检查PDO中是否存在记录。我不明白为什么不调用它。最后两条$msg语句在我用代码创建新帐户时出现$msg在用echo语句重新加载时,在HTML表单中被调用。@JeremyMyrtle自从我最初发表文章以来,我做了很多编辑。请重新加载它。PHP代码中只有一个标题,我已经在其中添加了exit()。我还是不懂$msg。它在PHP中作为一个空白变量启动。然后,如果发生错误,将重写
返回的记录数。在这种情况下,OP绝对不需要它。@Dharman好的,谢谢。我给它添加了另一个选项。我同意有不止一种方法可以用来检查PDO中是否存在记录。我不明白为什么不调用它。最后两条
$msg
语句在我用代码创建新帐户时出现$msg
在用echo
语句重新加载时,在HTML表单中被调用。@JeremyMyrtle自从我最初发表文章以来,我做了很多编辑。请重新加载它。PHP代码中只有一个标题,我已经在其中添加了exit()
。我还是不懂$msg
。它在PHP中作为一个空白变量启动。然后,如果发生错误,将重写$msg
以显示消息。之后,需要刷新页面并显示错误消息。这发生在PHP代码中的最后两个$msg
上。我必须指出,rowCount
很少是一个好的选择。您通常不需要知道返回记录的数量。在这种情况下,OP绝对不需要它。@Dharman好的,谢谢。我给它添加了另一个选项。我同意有不止一种方法可以用来检查PDO中是否存在记录。我不明白为什么不调用它。最后两条$msg
语句在我用代码创建新帐户时出现$msg
在用echo
语句重新加载时,在HTML表单中被调用。@JeremyMyrtle自从我最初发表文章以来,我做了很多编辑。请重新加载它。PHP代码中只有一个标题,我已经在其中添加了exit()
。我还是不懂$msg
。它在PHP中作为一个空白变量启动。然后,如果发生错误,将重写$msg
以显示消息。之后,需要刷新页面并显示错误消息。这发生在PHP代码中的最后两个$msg
上。
<?php
session_start();
include('api/dbconnect.php');
$msg = "";
if(isset($_POST['register-submit'])) {
// Now we check if the data was submitted, isset() function will check if the data exists.
if (!isset($_POST['first_name'], $_POST['last_name'], $_POST['email'], $_POST['username'], $_POST['password'])) {
// Could not get the data that should have been sent.
$msg = 'Please complete the registration form';
}
// Make sure the submitted registration values are not empty.
if (empty($_POST['first_name']) ||
empty($_POST['last_name']) ||
/* empty($_POST['email']) || */
empty($_POST['username']) ||
empty($_POST['password'])) {
// One or more values are empty.
$msg = 'All fields are required for the form to be submitted';
}
/*if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
die ('The email address provided is invalid');
}*/
if (preg_match('/[A-Za-z0-9]+/', $_POST['username']) == 0) {
$msg = 'The username provided is invalid';
}
if (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 5) {
$msg = 'Password must be between 5 and 20 characters';
}
if ($_POST['password'] != $_POST['confirm_pwd']) {
$msg = 'The two password provided do not match.';
}
// We need to check if the account with that username exists.
if ($stmt = $pdo->prepare('SELECT id, password FROM users WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bindParam(1, $_POST['username']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
if ($stmt->num_rows > 0) {
// Username already exists
$msg = 'Another account with this username already exists';
} else {
// Username doesn’t exist, insert new account
if ($stmt = $pdo->prepare('INSERT INTO users (first_name, last_name, email, username, password) VALUES (?, ?, ?, ?, ?)')) {
// We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt->bindParam(1, $_POST['first_name']);
$stmt->bindParam(2, $_POST['last_name']);
$stmt->bindParam(3, $_POST['email']);
$stmt->bindParam(4, $_POST['username']);
$stmt->bindParam(5, $password);
$stmt->execute();
header('Location: users.php');
} else {
// Something is wrong with the SQL statement. Check to make sure the users table exists with all three fields.
$msg = 'There was a problem creating this account. Contact your Network Administrator.';
}
}
$stmt->close();
}
else {
// Something is wrong with the SQL statement. Check to make sure the users table exists with all 3 fields.
$msg = 'There was a problem creating this account. Contact your network administrator.';
}
//$con->close();
}
?>
<form class="user" method="post" action="user_new.php">
<div class="custom-control small">
<strong class="text-danger"><?php echo $msg; ?></strong>
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="text" class="form-control form-control-user" id="first_name" name="first_name" placeholder="First Name" autocomplete="off" required>
</div>
<div class="col-sm-6">
<input type="text" class="form-control form-control-user" id="last_name" name="last_name" placeholder="Last Name" autocomplete="off" required>
</div>
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="email" class="form-control form-control-user" id="email" name="email" placeholder="Email Address (optional)" autocomplete="off">
</div>
<div class="col-sm-6">
<input type="text" class="form-control form-control-user" id="username" name="username" placeholder="Username" autocomplete="off" required>
</div>
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="password" class="form-control form-control-user" id="password" name="password" placeholder="Password" autocomplete="off" required>
</div>
<div class="col-sm-6">
<input type="password" class="form-control form-control-user" id="confirm_pwd" name="confirm_pwd" placeholder="Confirm Password" autocomplete="off" required>
</div>
</div>
<button type="submit" name="register-submit" class="btn btn-primary btn-user btn-block" style="background-color: #a40000; border-color: #a40000;">Create User Account</button>
<hr>
<a href="users.php" class="btn btn-warning btn-user btn-block" style="background-color: #2658a8; border-color: #2658a8;">Return to Users</a>
</form>
$if_row = $stmt->fetchColumn();
if ($if_row > 0) {
$msg = "A record exists.";
}
<?php if(!empty($msg)) { echo $msg; } ?>