php简易增删改查

1、新建数据库 news,blogs

+        htis int(11)

2、php连接数据库(conn.php)

<?php
	header('Content-type:text/html;charset=utf-8');
	@mysql_connect("localhost:3306","root","admin123")or die("mysql连接失败");
	@mysql_select_db("news")or die("db连接失败");
	//mysql_query("set names 'gbk'");
	mysql_set_charset("utf8");
?>


3、显示数据页面<后台首页>(index.php)


<a href="add.php" target="_blank">添加内容</a>
<hr />
<form action="" method="get">
	<input type="text" name="keys">
	<input type="submit" name="subs" value="搜索">
</form>
<?php
	include("conn.php");

	if(!empty($_GET['keys'])){
		$w = " `title` like '%".$_GET['keys']."%' ";
	}else{
		$w=1;
	}

	$sql = "select * from `blog` where $w order by id desc limit 10";
	$query = mysql_query($sql);

	while ($rs = mysql_fetch_array($query)) {
?>

<h2>
标题: <a href="view.php?id=<?php echo $rs['id'] ?>" target="_blank"><?php  echo $rs['title'] ?></a>
|<a href="edit.php?id=<?php echo $rs['id'] ?>" target="_blank">编辑</a> | 
<a href="del.php?del=<?php echo $rs['id'] ?>" target="_blank">删除</a> |  
</h2>
<li><?php  echo $rs['dates'] ?></li>
<p><?php  echo iconv_substr($rs['contents'], 0,2,"utf-8") ?>...</p>
<hr />
<?php
}
?> 


4、添加数据页面(add.php)

<?php
	include("conn.php");

	if(!empty($_POST['sub'])){
		$title = $_POST['title'];
		$con = $_POST['con'];
		$sql = "insert into `blog`(`id` , `title` , `dates` , `contents`) values (null , '$title' , now() , '$con')";
		mysql_query($sql);
		echo "插入成功 ";
	}
?>

<form action="add.php" method="post">
	标题<input type="text" name="title"><br />
	内容<textarea rows="5" cols="50" name="con"></textarea><br />
	<input type="submit" name="sub" value="发表">
</form>



5、编辑页面(edit.php)
<?php
	include("conn.php");

	if(!empty($_GET['id'])){
		$sql = "select * from blog where `id` = '".$_GET['id']."'";
		$query = mysql_query($sql);
		$rs = mysql_fetch_array($query);
	}

	if(!empty($_POST['sub'])){
		$title = $_POST['title'];
		$con = $_POST['con'];
		$hid = $_POST['hid'];
		$sql = "update `blog` set `title`='$title' , contents='$con' where id='$hid'";
		mysql_query($sql);
		echo "<script>alert('更新成功');location.href='index.php'</script>";
	}
?>

<form action="edit.php" method="post">
	<input type="hidden" name="hid"  value="<?php echo $rs['id'] ?>">
	标题<input type="text" name="title" value="<?php echo $rs['title'] ?>"><br />
	内容<textarea rows="5" cols="50" name="con"><?php echo $rs['contents'] ?></textarea><br />
	<input type="submit" name="sub" value="发表">
</form>



6、删除数据(del.php)

<?php
	include("conn.php");

	if(!empty($_GET['del'])){
		$d = $_GET['del'];
		$sql = "delete from `blog` where `id`='$d'";
		mysql_query($sql);
		echo "删除成功";
	}
?>


7、前台显示(view.php)

<?php
	include("conn.php");

	if(!empty($_GET['id'])){
		$sql = "select * from blog where `id` = '".$_GET['id']."'";
		$query = mysql_query($sql);
		$rs = mysql_fetch_array($query);

		$sqlup="update blog set hits = hits+1 where `id` = '".$_GET['id']."'";
		mysql_query($sqlup);
	}
?>
<h1><?php echo $rs['title'] ?></h1>
<h1><?php echo $rs['dates'] ?></h1>
<h3>点击量:<?php echo $rs['hits'] ?></h3>
<hr />
<p><?php echo $rs['contents'] ?></p>

8、登录

<?PHP
    include("conn.php");
?>

<!DOCTYPE html>
<html>
<head>
<title>login</title>
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
<div class="login_in">
    <form name="form" method="post" action="" >
    <ul>
        <li><span>用户名:</span><input type="text" class="input" name="username" id="username" placeholder="请输入用户名" /></li>
        <li> <span>密   码:</span><input type="password" class="input" name="password" id="password" placeholder="请输入密码" /></li>
        <li class="denglu_btn"><input type="submit" name="sub" id="sub" value="登录" /></li>
    </ul>
</form>

<?php
    if(!empty($_POST['sub'])){
        $username=$_POST['username'];
        $password=$_POST['password'];
        $sql="select * from `userflag` where username='$username' and password='$password'";
        $query = mysql_query($sql);
        $rs = mysql_fetch_array($query);

        if($rs['password']==$password && $password!=null){
            echo "<script>alert('登录成功');location.href='log/php/index.php'</script>";
        }else{
            echo "<script>alert('密码错误,请重新输入');</script>";
        }

    }
?>
</div>
</body>
</html>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值