环境php 5.2.13 mysql 5.5.3
创建register.html
<html>
<body>
<form name="register" action="register.php" method="POST">
注册用户名<input type="text" name="name"/>
<p>密码<input type="password" name="password"/> </p>
<input name="log" type="submit" value="注册">
<input name="log" type="submit" value="登入">
</form>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
创建register.php
<?php header("content-type:text/html; charset=utf-8"); ?>
<?php
$mysqli=mysqli_connect("localhost","root","******","test");
$name=$_POST["name"];
$password=$_POST["password"];
$hashpw=md5($password);
if ($name && $password){
$sql = "SELECT * FROM login WHERE user='$name'";
$res = mysqli_query($mysqli,$sql);
$rows=mysqli_num_rows($res);
if($rows){
echo "已有人注册此名,请重新选择名字!";
echo "<a href=register.html>返回</a>";
exit;
}
else{
$ins = "insert into login values('$name','$hashpw')";
$result = mysqli_query($mysqli,$ins);
if($result){
echo "祝贺你,注册成功!";
echo "<a href=login.html>登入</a>";
exit;}
}
mysqli_free_result($res);
mysqli_close($mysqli);
}
?>
创建 login.html
<html>
<body>
<form name="login" action="login1.php" method="POST">
用户名<input type="text" name="name"/>
<p>密码<input type="password" name="password"/> </p>
<input name="log" type="submit" value="登录">
</form>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
创建login1.php
<?php
$mysqli=mysqli_connect("localhost","root","******","test");
$name=$_POST["name"];
$password=$_POST["password"];
$hashpw=md5($password);
if ($name && $password){
$sql = "SELECT * FROM login WHERE user='$name' and password='$hashpw'";
$res = mysqli_query($mysqli,$sql);
$rows=mysqli_num_rows($res);
if($rows){
header("location:index.php");
exit;
}
else{
echo "登入失败,请验证!";
}
mysqli_free_result($res);
mysqli_close($mysqli);
}
?>
通过以上代码可以知道实现简单的注册和登入测试,可以了解PHP和mysql简单的调用和交互过程。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ydt619/archive/2010/09/16/5888856.aspx