PHP简单同学录(连接数据库)

这个是学PHP时写的一个简单的php同学录作业,用的DW和wamp使用sqli连接数据库,比较简单没有写登录界面(需要的可自行编写)。
1.欢迎界面

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<style>
		body{
			background-image: url("image/view1.jpg");
			background-size: cover;
			background-repeat: no-repeat;
			margin-top: 250px;
			text-align: center;
			font-size: 40px;
			font-family:STXingkai;
		}
		div{
			margin-top: 10px;
		}
		button{
			background-color: transparent;
			font-size: 40px;
			font-family:STXingkai;
		}
	</style>
	 <script>
		 function insert(){
			 window.location="view.php";
		 }
		 function search(){
			 window.location="search.php";
		 }
		 function deleteview(){
			 window.location="deleteview";
		 }
		 function edit(){
			 window.location="updateview";
		 }
	</script>
</head>

<body>
	<h3>欢迎来到18软工的留言板</h3>
	
	<button type="button" onClick="insert()">添加留言</button>
    <button name="search" onClick="search()">查看留言</button>
	<div class="change">
		<button name="delete" onClick="deleteview()">删除留言</button>
	    <button name="edit" onClick="edit()">修改留言</button>
	</div>
	
</body>
</html>

2.添加留言的用户界面和数据库操作语句

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<style>
		body{
			background-image:url("image/background.jpg");
			background-size: cover;
			background-repeat: no-repeat;
			font-size: 40px;
			font-family:STXingkai;
		}
		center{
			margin-top: 200px;
		}
		input{
			background-color: transparent;
			border-left-width:0px;
			border-right-width: 0px;
			border-top-width: 0px;
			font-size: 30px;
			font-family:STXingkai;
		}		
		textarea{
			background-color: transparent;
			border-left-width:0px;
			border-right-width: 0px;
			border-top-width: 0px;
			border-bottom-width: 0px;
			font-size: 30px;
			font-family:STXingkai;
		}
		button{
			background-color: transparent;
			font-size: 40px;
			font-family:STXingkai;
		}
	</style>
</head>

<body>
	<center>
  <h3>添加留言</h3>
    <form action = "insert.php" method = "post">
     <table width = "auto" border = "0" cellpadding = "4">
          <tr>
                 <td  align = "right">标题:</td>
                 <td><input type = "text" name = "title" ></td>
          </tr>
          <tr>
                 <td  align = "right">留言者:</td>
                 <td><input type ="text" name = "name"></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">留言内容:</td>
                 <td><textarea name = "content" row = "5" cols = "30"></textarea></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">联系电话:</td>
                 <td><input type = "number" name = "phone"></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">联系邮箱:</td>
                 <td><input type ="text" name = "email"></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">QQ</td>
                 <td><input type = "number" name = "qqnumber"></td>
          </tr>
          <tr>
                  
             <td colspan = "2" align = "center">
				 <button type="submit">提交</button>
                 <button type="reset">重置</button></td>
          </tr>
          </table>
  </form>
</center>
</body>
</html>
<?php	
	//连接数据库
	$db=mysqli_connect("localhost","root","")or die("连接失败");
	mysqli_select_db($db,"lyb");
	mysqli_query($db,"set names utf8");
	//获取数据
	$title=$_POST["title"];
	$name=$_POST["name"];
	$content=$_POST["content"];
	if(!is_numeric($_POST["phone"])){
		echo "请输入正确的电话号码";
	}elseif(strlen($_POST["phone"])!=11){
		echo "请输入正确的号码";
	}else{
		$phone=$_POST["phone"];
	}
	$email=$_POST["email"];
	$qqnumber=$_POST["qqnumber"];
	//插入数据
	$sql="insert into software value('$title','$name','$content',$phone,'$email',$qqnumber)";
	$result=mysqli_query($db,$sql);
	if(!$result)
		{
            printf("Error: %s", mysqli_error($db));
		}else{
			echo "成功两秒后跳转";
			header("refresh:2;url=login.php");
			mysqli_close($db);
		}
	?>

3.查询内容的用户界面和对应的数据库操作语句

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<style>
		body{
			background-image:url("image/background.jpg");
			background-size: cover;
			background-repeat: no-repeat;
			margin-top: 250px;
			font-size: 35px;
			font-family:STXingkai;
		}
		input{
			background-color: transparent;
			border-left-width:0px;
			border-right-width: 0px;
			border-top-width: 0px;
			font-size: 40px;
			font-family:STXingkai;
		}
		button{
			float: right;
			background-color: transparent;
			font-size: 35px;
			font-family:STXingkai;
		}			
	</style>
		 <script>
		 function returnview(){
			 window.location="login.php";
		 }

	</script>
</head>

<body>
	<center>
	<h3>我的留言板</h3>
	<?php
	$db=mysqli_connect("localhost","root","");
	mysqli_select_db($db,"lyb");
	mysqli_query($db,"set names utf8");
	$sql="select * from software";
	$result=mysqli_query($db,$sql);
	echo "<table border=2 >";
	echo "<tr><th>标题</th><th>姓名</th><th>内容</th><th>电话</th><th>邮箱</th><th>QQ</th></tr>\n";
    while ($myrow=mysqli_fetch_row($result)){
      printf("<tr> <td>%s</td> <td>%s</td><td>%s</td><td>%s</td> <td>%s</td> <td>%s</td> </tr>", $myrow[0], $myrow[1], $myrow[2], $myrow[3],$myrow[4],$myrow[5]);
    }
    echo "</table>";
	mysqli_close($db);
	?>
    </center>
	<button type="submit" onClick="returnview()">返回</button>
</body>
</html>

4.删除的用户界面和对应的数据库操作语句

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<style>
		body{
			background-image:url("image/background.jpg");
			background-size: cover;
			background-repeat: no-repeat;
			margin-top: 300px;
			font-size: 40px;
			font-family:STXingkai;
		}
		input{
			background-color: transparent;
			border-left-width:0px;
			border-right-width: 0px;
			border-top-width: 0px;
			font-size: 40px;
			font-family:STXingkai;
		}
		button{
			background-color: transparent;
			font-size: 40px;
			font-family:STXingkai;
		}
	</style>
</head>

<body>
	<center>
	<!删除留言板-->
	<h3>删除留言板</h3>
	<form action="delete.php" method="post">
		    <tr>				
                 <td  align="right">输入留言者姓名:</td>
                 <td><input type="text" name ="name"></td>
            </tr>
		<button type="submit">确定</button>
	</form>
	</center>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<style>
		body{
			font-size: 40px;
			font-family:STXingkai;
		}
	</style>
</head>

<body>
	<?php
		$db=mysqli_connect("localhost","root","");
		mysqli_select_db($db,"lyb");
		mysqli_query($db,"set names utf8");
		$name=$_POST['name'];
		$sql="delete from software where name='$name'";
		$result=mysqli_query($db,$sql);
		if(!$result)
		{
			echo mysql_error();
			exit();	
		}
		else
		{	
			echo "删除成功2秒后跳转";
			header("refresh:2;url=login.php");
			mysqli_close($db);
		}
	?>
</body>
</html>

5.修改留言的用户界面和对应的数据库操纵语句

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
	<style>
		body{
			background-image:url("image/background.jpg");
			background-size: cover;
			background-repeat: no-repeat;
			margin-top: 300px;
			font-size: 40px;
			font-family:STXingkai;
		}
		input{
			background-color: transparent;
			border-left-width:0px;
			border-right-width: 0px;
			border-top-width: 0px;
			font-size: 40px;
			font-family:STXingkai;
		}
		button{
			background-color: transparent;
			font-size: 40px;
			font-family:STXingkai;
		}
	</style>
</head>
<body>
	<center>
	<!修改留言板-->
	<h3>修改留言板</h3>
	<form action="edit.php" method="post">
		    <tr>				
                 <td  align="right">输入留言者姓名:</td>
                 <td><input type="text" name ="name"></td>
            </tr>
		<button type="submit">确定</button>
	</form>
	</center>
</body>
</html>
<!doctype html>
<html>
<head>
<meta  charset="utf-8">
<title>无标题文档</title>
	<style>
	body{
			background-image:url("image/background.jpg");
			background-size: cover;
			background-repeat: no-repeat;
			font-size: 40px;
			font-family:STXingkai;
		}
		center{
			margin-top: 200px;
		}
		input{
			background-color: transparent;
			border-left-width:0px;
			border-right-width: 0px;
			border-top-width: 0px;
			font-size: 30px;
			font-family:STXingkai;
		}		
		textarea{
			background-color: transparent;
			border-left-width:0px;
			border-right-width: 0px;
			border-top-width: 0px;
			border-bottom-width: 0px;
			font-size: 30px;
			font-family:STXingkai;
		}
		button{
			background-color: transparent;
			font-size: 40px;
			font-family:STXingkai;
		}
	</style>
</head>

<body>
	<center>
	<h2>修改留言板</h2>
	 <?php
        $db=mysqli_connect("localhost","root","");
		mysqli_select_db($db,"lyb");
		mysqli_query($db,"set names utf8");
		$name=$_POST['name'];
		$sql="select * from software where name='$name'";
		$res=mysqli_query($db,$sql);
		$arr=mysqli_fetch_assoc($res);
    ?>
    <form action = "update.php" method = "post">
     <table width = "auto" border = "0" cellpadding = "4">
          <tr>
                 <td  align = "right">标题:</td>
                 <td><input type = "text" name = "title" value="<?php echo $arr['title'] ?>" /></td>
          </tr>
          <tr>
                 <td  align = "right">留言者:</td>
                 <td><input type = "text" name = "name" value="<?php echo $arr['name'] ?>"/></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">留言内容:</td>
                 <td><textarea name = "content" row = "5" cols = "30" ><?php echo $arr['content'] ?></textarea></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">联系电话:</td>
                 <td><input type = "number" name = "phone" value="<?php echo $arr['phone'] ?>"/></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">联系邮箱:</td>
                 <td><input type = "text" name = "email" value="<?php echo $arr['email'] ?>"/></td>
          </tr>
          <tr>
                 <td align = "right" valign = "top">QQ</td>
                 <td><input type = "number" name = "qqnumber" value="<?php echo $arr['qqnumber'] ?>"/></td>
          </tr>
          <tr>
                  
             <td colspan = "2" align = "center">
				 <button type="submit">修改留言</button>
				 <button type="reset">重置</button>
			  </td>
          </tr>
          </table>
          </form>
		</center>
</body>
</html>
<!doctype html>
<html>
<head>
<meta  charset="utf-8">
<title>无标题文档</title>
	<style>
		body{
			font-size: 40px;
			font-family:STXingkai;
		}
	</style>
</head>

<body>
	<?php
	$db=mysqli_connect("localhost","root","");
	mysqli_select_db($db,"lyb");
	mysqli_query($db,"set names utf8");
	
	//获取数据
	$title=$_POST["title"];
	$name=$_POST["name"];
	$content=$_POST["content"];
	if(!is_numeric($_POST["phone"])){
		echo "请输入正确的电话号码";
	}elseif(strlen($_POST["phone"])!=11){
		echo "请输入正确的号码";
	}else{
		$phone=$_POST["phone"];
	}
	$email=$_POST["email"];
	$qqnumber=$_POST["qqnumber"];
	
	//更新数据库数据
	$sql="update software set title='$title',name='$name',content='$content',phone=$phone,email='$email',qqnumber=$qqnumber where name='$name'";
	$result=mysqli_query($db,$sql);
		if(!$result)
		{
            printf("Error: %s", mysqli_error($db));
		}else{
			echo "成功两秒后跳转";
			header("refresh:2;url=login.php");
			mysqli_close($db);
		}
	?>
</body>
</html>

效果图:
在这里插入图片描述
在这里插入图片描述
个别界面含有隐私内容这里就不贴出来了,下面是编写留言板时使用的数据库大家可自行下载:

链接:https://pan.baidu.com/s/11wNtBj2PhHyZPlkurcTF7g
提取码:usz3

总结:
总的来说php语言是较简单的计算机语言,在连接数据库方面也是很直接的连接方式。需要注意的是在较新版本的php当中连接数据库以及操作数据库用的较多的是sqli语句该语句与旧版的语句是存在不同的。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值