我刚开始学习PHP和我的SQL数据库,我已经学会了如何创建数据库,并创建注册表单来存储数据库上的信息,但我不知道如何阻止人们使用已经被占用的用户名进行注册,我也不知道如何允许用户在我的网站上拥有自己的个人资料页面。你知道怎么做吗?我正在使用XAMPP在本地服务器上测试我的数据库和PHP代码,如果这有任何帮助的话。下面是我的PHP代码:
<?php
$con=mysql_connect("localhost", "root", "" );
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
mysql_select_db("test", $con);
mysql_query("INSERT INTO users (id, username, password, email)
VALUES (NULL,'$username', MD5('$password'), '$email')");
if (my_query)
echo "Account Successfully Created";
else
echo "Sorry Could Not Create Account !";
mysql_close($con);
?>发布于 2010-08-21 09:48:50
在继续之前,请确保您阅读了SQL injections上的内容。尽早养成良好的MySQL习惯是很好的!
您需要更改以下SQL查询以适合您当前的数据库结构,但您应该看到所发生的事情的模式-
$getSQL = "SELECT * FROM users WHERE username = '$username';";
$getResult = mysql_query($getSQL);
if(mysql_num_rows($getResult) > 0) { // This username is already taken } else { // This is a new username }就个人资料页面而言,创建一个接受用户ID的viewprofile.php文件,下面的代码应该可以帮助您找到正确的方向。
$getSQL = "SELECT * FROM users WHERE id = '$id';";
$getResult = mysql_query($getSQL);
if(mysql_num_rows($getResult) > 0) {
// The profile being viewed exists
while($gR = mysql_fetch_array($getResult)) {
$userid = $gR['id'];
$username = $gR['username'];
}
} else {
// The profile being viewed doesn't exist
}我真的希望这对你有帮助!为您提供其他一些不错的资源:MySQL Tutorial、Basic User Authentication Tutorial、
发布于 2010-08-21 09:44:23
https://stackoverflow.com/questions/3535906
复制相似问题