默认MySql没有配置密码 已经会基本MySQL语句
一样 开头也要有链接数据库
1 2 3 4 5 6 7 8 9 10 11 12
| <?php $servername = "localhost"; $serverRootName = "root"; $serverPasswd = ""; $db = "mydb"; $mysql = new mysqli($servername, $serverRootName, $serverPasswd, $db); if($mysql->connect_error){ die("链接失败: ". mysqli_connect_error()); } $mysql->set_charset("utf8"); inputSomethingsInDB($mysql); ?>
|
先看题目 我们要实现注册+重复注册这个功能
需要会 SELECT and INSERT
然后需要几个function
这里我的命名可能有点nt 毕竟我是新手w
检测重复注册code1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| function inputSomethingsInDB($mysql) { $sql1 = "SELECT username FROM user WHERE username = ? "; $username = $_POST["username"]; $mysqli_stmt1 = $mysql->prepare($sql1); $mysqli_stmt1->bind_param("s",$username);
if($mysqli_stmt1->execute()){
$dbusername = null; $mysqli_stmt1->bind_result($dbusername); while ($mysqli_stmt1->fetch()){ echo "<script>alert('不允许重复注册!');window.location.href='register.html'</script>"; }if($dbusername == false ){ register($mysql); }else{ echo "AADb:".$dbusername."input:".$username; }
}else{ echo $mysqli_stmt1->error; }
$mysqli_stmt1->close(); }
|
执行注册code1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| function register($mysql) //注册function { $sql = "INSERT INTO user (username , gender , password , ID , brief) VALUES (?,?,?,?,?)"; $mysqli_stmt = $mysql->prepare($sql);
$username = $_POST["username"]; $gender = $_POST["gender"]; $passwd = $_POST["pword"]; $id = $_POST["name"]; $brief = "notings here!"; $mysqli_stmt->bind_param("sisss", $username , $gender , $passwd , $id,$brief);
if($mysqli_stmt->execute()){ echo $mysqli_stmt->insert_id; echo PHP_EOL; echo "<script>alert('恭喜你,注册成功');window.location.href='login.html'</script>";
}else{ echo $mysqli_stmt->error; }
$mysqli_stmt->free_result(); $mysqli_stmt->close();
}
$mysql->close();
|
这样就可以完成注册+检测重复注册判定