本篇文章主要介绍了"PHP连接数据库(注册页面的增删改查)",主要涉及到方面的内容,对于PHPjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
1.连接数据库—————————–connect.php——————————————–
1.连接数据库
—————————–connect.php——————————————–
//本地测试$host = '127.0.0.1';
$port = 3306;
$user = "root";
$pwd = "";
$link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);
if(!$link) {
die("Connect Server Failed: " . mysql_error());
}
//选择连接的数据库库名
mysql_select_db("my");
//设置字符编码utf8
mysql_set_charset('utf8');
?>
2.注册页面(html页面)
———————————-reg_9.php—————————————
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8" /><title>Documenttitle>head><body><h3>注册页面h3><formaction="add.php"method='post'><tableborder='1'cellpadding='0'cellspacing='0'width='80%'bgcolor='#ABCDEF'><tr><tdalign='right'>用户名td><td><inputtype="text"name="username"id=""/>以小写字母开始,长度要求5~10td>tr><tr><tdalign='right'>密码td><td><inputtype="password"name="password"id=""/>密码不能为空td>tr><tr><tdalign='right'>邮箱td><td><inputtype="text"name="email"id="" />td>tr><tr><tdalign='right'>性别td><td><inputtype="radio"name="sex"id=""value='1' />男
<inputtype="radio"name="sex"id=""value='2' />女
<inputtype="radio"name="sex"id=""value='3' />保密
td>tr><tr><tdalign='right'>个人简介td><td><textareaname="txt"id=""cols="50"rows="10">textarea>td>tr><tr><tdcolspan='2'><inputtype="submit"name='act'value='注册' />td>tr>table>form>body>html>

3.将注册数据显示在数据库
—————————————-add.php—————————————–
//往数据库中添加数据
"Content-type:text/html; charset=utf-8");
//-----------------------连接数据库---------------------------
include_once "connect.php";
//-------------------------将数据连接到数据库------------------
$time=time();
$sql="insert into user (username,password,email,sex,txt,`time`) value('{$_POST['username']}','{$_POST['password']}','{$_POST['email']}','{$_POST['sex']}','{$_POST['txt']}','{$time}')";
$res=mysql_query($sql);
header("location:hello.php");
?>

4.返回后台界面
———————————-hello.php———————————————
header("Content-type:text/html; charset=utf-8");
//-----------------------连接数据库------------------------------include_once"connect.php";
//--------------------查询数据库--------------------------------$query="select * from user";
$result=mysql_query($query);
if(!$result)
{
die("could not to the database
".mysql_error());
}
//-------------------封装函数-----------------------------//该函数将数据库的数据写成数组形式functionresult2Arr($result){while($result_row=mysql_fetch_assoc($result)){
$arr[] = $result_row;
}
return$arr;
}
$arr = result2Arr($result);
foreach($arras$key=>$value){
echo";
echo";
echo" "
;
echo"" | .$value['id']."";
echo"" | .$value['username']."";
echo"" | .$value['password']."";
echo"" | .$value['email']."";
echo"" | .$value['sex']."";
echo"" | .$value['txt']."";
echo"" | .date('Y-m-d H:i:s',$value['time'])."";
echo"修改 删除 | ";
echo"
";
echo"";
}
?>

4.修改数据
—————————————–update1.php———————————–
//当用户要修改信息时,返回页面,页面中包含之前填写的信息
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8" /><title>Documenttitle>head><body><div>connect.php";
$sql="select * from user where id='".$_GET['id']."'";
//echo "sql:".$sql;(显示出修改哪一行)
$result=mysql_query($sql,$link);
$arr = result2Arr($result);
//print_r($arr);
$row = $arr[0];
function result2Arr($result){
while($result_row=mysql_fetch_assoc($result)){
$arr[] = $result_row;
}
return $arr;
}
?><h3>注册页面h3><formaction="update.php"method='post'><inputtype="hidden"name="id"id=""value=""/><tableborder='1'cellpadding='0'cellspacing='0'width='80%'bgcolor='#ABCDEF'><tr><tdalign='right'>用户名td><td><inputtype="text"name="username"id=""value=""/>以小写字母开始,长度要求5~10td>tr><tr><tdalign='right'>密码td><td><inputtype="password"name="password"id=""value="echo $row['password']?>"/>密码不能为空td>tr><tr><tdalign='right'>邮箱td><td><inputtype="text"name="email"id=""value=""/>td>tr><tr><tdalign='right'>性别td><td><inputtype="radio"name="sex"id=""value='1'/>男
<inputtype="radio"name="sex"id=""value='2'/>女
<inputtype="radio"name="sex"id=""value='3'/>保密
td>tr><tr><tdalign='right'>个人简介td><td><textareaname="txt"id=""cols="50"rows="10">textarea>td>tr><tr><tdcolspan='2'><inputtype="submit"name='act'value='修改' />td>tr>table>form>div>body>html>

————————————–update.php————————————–
//将修改的信息存入数据库
"Content-type:text/html; charset=utf-8");
//通过post获取页面提交数据信息
$data = $_POST;
//print_r($data);
include_once "connect.php";
$sql = "update `user` set username='{$data['username']}',password='{$data['password']}', email='{$data['email']}',sex='{$data['sex']}',txt='{$data['txt']}' whereid']}'";
echo$sql;
$res = mysql_query($sql,$link);
if($res){
header("Location:hello.php");
//echo"alert('修改成功')";
}else{
header("Location:update1.php?id=".$data['id']);
//echo"alert('修改失败')";
}
?>


5.删除数据
—————————–delete.php———————————————
//删除数据库里的数据