gpt4 book ai didi

PHP 使用 mySQL 检查现有用户名

转载 作者:行者123 更新时间:2023-11-29 08:31:47 25 4
gpt4 key购买 nike

我一直在遵循本教程来构建一个简单的注册/登录脚本。

http://forum.codecall.net/topic/69771-creating-a-simple-yet-secured-loginregistration-with-php5/

我是 PHP 新手,但我有很多使用 C++ 的经验,所以我认为转换不会太难,我只需要弄清楚语法。我还在大学里对 mySQL 进行了非常非常快速的介绍,所以我认为当用户注册时使用 mySQL 来检查现有用户名会容易得多,尽管我的知识不太好。我认为这样的事情会起作用;

SELECT username
FROM codecalltut
WHERE username = username;

这真的有用吗?它从数据库 codecalltut 中选择用户名,然后检查输入的用户名是否已经是用户名?即使这是正确的,我也不知道如何将它包含在我的 PHP 中。

我尝试过使用

$qry = "SELECT username
FROM codecalltut
WHERE username = username;"

但是当它移动到下一个语句时,我只是收到语法错误。

<?php 
$qry = "SELECT username
FROM codecalltut
WHERE username = username;"

//if register button was clicked.
} else {
$usr = new Users; //create new instance of the class Users
$usr->storeFormValues( $_POST ); //store form values

//if the entered password is match with the confirm password then register him
if( $_POST['password'] == $_POST['conpassword'] ) {
echo $usr->register($_POST);
} else {
//if not then say that he must enter the same password to the confirm box.
echo "Password and Confirm password not match";
}
}

?>

这是用于构建数据库的查询:

CREATE DATABASE `codecalltut` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `codecalltut`;


CREATE TABLE IF NOT EXISTS `users` (
`userID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varbinary(250) NOT NULL,
PRIMARY KEY (`userID`,`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

这是用户单击“注册”时的 HTML 代码

<li class="buttons">
<input type="submit" name="register" value="Register" />
<input type="button" name="cancel" value="Cancel" onclick="location.href='index.php'" />
</li>

最佳答案

您的 HTML 注册表单

<form action='' method='POST'>
<input type='text' name='username' />
<input type='password' name='password' />
<input type='password' name='re-password' />
<input type='submit' name='submit' />
</form>
<小时/>

您的 PHP 代码

if($_POST){

if(empty($_POST['username']) && empty($_POST['password']) && empty($_POST['re-password'])) {
echo 'Please enter all fields';
}else {

$username = $_POST['username'];
$password = $_POST['password'];
$re_password = $_POST['re-password'];

if($password !== $re_password){
echo 'Both passwords do not match';
}else {

$db_name =
$db_user =
$db_pass =

$conn = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx',
array( PDO::ATTR_PERSISTENT => true )
);

$stmt = $conn->prepare("SELECT username,password FROM users WHERE username = ? AND password = ?");
$stmt->execute(array($username, $password));

if($stmt->rowCount() === 0 ) {


$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?,?)");
$stmt->execute(array($username, $password));
if($stmt->rowCount() ===1){
echo 'Registration complete';
}else {
echo 'Sorry, unknown error: please try again later';

}

}else {
echo 'Sorry, the username '.$username.' already exists';
}



}

}
}

关于PHP 使用 mySQL 检查现有用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16375680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com