PHP连接MySQL注册

前台register.php

<form method="post" action="connect.php">
<table width="400" border="0" align="center" bgcolor="#F0F0F0">
<tr>
<td align="center">用户名</td>
<td><input name="userid" type="text" size="20" onblur="userCheck()"><span id="a"></span></td>
</tr>
<tr>
<td align="center">真实姓名</td>
<td><input name="username" type="text" size="20"/></td>
</tr>
<tr>
<td align="center">性别</td>
<td>
<input type="radio" name="sex" value="0" checked="checked">&nbsp;
<input type="radio" name="sex" value="1">&nbsp;
</td>
</tr>
<tr>
<td align="center">密码</td>
<td><input name="pwd" type="password" size="20"/></td>
</tr>
<tr>
<td align="center">密码确认</td>
<td><input name="pwd2" type="password" size="20" onblur="pwdCheck()"/><span id="b"></span></td>
</tr>
<tr>
<td align="center">电子邮箱</td>
<td><input name="email" type="text" size="20"/></td>
</tr>
<tr>
<td align="center">学校</td>
<td><input name="school" type="text" size="20"/></td>
</tr>
<tr>
<td align="center">电话号码</td>
<td><input name="phonenum" type="text" size="20"/></td>
</tr>
<tr>
<td align="center"><img border=0 src="image.jsp"></td>
<td><input type="text" name="in" maxlength="4" size="8"></td>
</tr>

<tr>
<td align="center"><input type="submit" name="cmd" value="register" /></td>
</tr>
</table>
</form>


后台

<?php
$cmd=$_POST["cmd"];
$userid=$_POST["userid"];
$pwd=$_POST["pwd"];
$link=mysql_connect("localhost","root","123456");
if(!$link) echo "没有连接成功!";
else echo "连接成功!";
mysql_select_db("studyabroad", $link); //选择数据库

if($cmd=="login")
{
$q = "SELECT * FROM users where user_id = '$userid'"; //SQL查询语句
//echo $q;
//mysql_query("SET NAMES UTF-8");

$rs = mysql_query($q); //获取数据集
if(!$rs){die("Valid result!");}
echo "<table>";
echo "<tr><td>真实姓名</td><td>密码</td><td>性别</td></tr>";
while($row = mysql_fetch_row($rs)) echo "<tr><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>"; //显示数据
echo "</table>";
mysql_free_result($rs); //关闭数据集
}
else if($cmd="register")
{
$username=$_POST["username"];
$sex=$_POST["sex"];
$email=$_POST["email"];
$phonenum=$_POST["phonenum"];
$school=$_POST["school"];
$q = "INSERT INTO users (user_id,user_name,password,sex,email,phone_num,school) values('$userid','$username' ,'$pwd' ,$sex,'$email','$phonenum','$school');"; //SQL查询语句
if(!mysql_query($q, $link))
{
die('Error: ' . mysql_error());
}
}
?>
1,mysql_error()函数居然可以指出具体错在哪里

2,在SQL语句中插入的数据如果是整数不能加单引号,否则会出错



posted @ 2012-03-02 20:29    阅读(1272)  评论(0编辑  收藏  举报