1.php表单验证用户名已存在
php表单验证用户名已存在
<?整表载cgic源码分析php
require 'form.php'; // 你的表单, 注意提交的URL
if (isset($_POST['submit'])) {
$username = $_POST['username'];
$sql = "SELECT count(id) AS number FROM users WHERE username = '$username'";
$r = mysql_query($sql);
$number = mysql_fetch_assoc($r)['number'];
if ($number < 1) {
// 保存数据
$username = $_POST['username'];
$password = $_POST['password'];
// ...
$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
mysql_query($sql);
if (mysql_affected_rows()) {
echo '注册成功!';
} else {
echo '注册失败';
}
} else {
echo "该用户已经存在, 请更换用户名";
}
}