php新手:增删改查数据,登录验证,简单分页

新手的话要从开始养成一些好的编码习惯,建议看一下代码规范

当然现在我还比较新。。看了也没记住多少,代码勉强能看,尤其适合新手看一看,特别简单直白。


首先建立一个数据库公用调取文件,不仅可以使你省事,少敲些代码,还可以防止一些数据库的漏洞。

conn.php

<?php

$link=@mysql_connect("192.168.31.236","yaozihao","123456");
mysql_select_db("yaozihao",$link);
mysql_query("set name 'utf8'");

login.html

//登陆界面

<!DOCTYPE html>
<html>
    <head>
        <title>登陆</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="logincheck.php" method="POST">
            用户名:<input type="text" name="name"></br>
            密  码:<input type="password" name="pwd"></br> 
            <input type="submit" name="sub" value="登陆">
            <input type="submit" name="zc" value="注册">
            
            
        </form>
    </body>
</html>

logincheck.php
//登录验证界面

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
    session_start();
include_once 'conn.php';
    //print_r($_POST);
if(isset($_POST['sub'])){
    $name=$_POST['name'];
    $pwd=$_POST['pwd'];
    $sql="SELECT * FROM user where name='$name' and password='$pwd'";
    $query=mysql_query($sql);
    $result=mysql_fetch_array($query);
    if($name!=null&&$pwd!=NULL&&$name==$result[1]&&$pwd==$result[2]){
        $_SESSION['username']=$name;
        echo "<script>alert('登陆成功');location='list.php';</script>";
    }else{
        echo '登录失败';
        
    }
}
if(isset($_POST['zc'])){
 echo "<script>location.href='zc.html';</script>";    
}


list.php

//验证成功进入列表页面:此页面中有一个简单的分页循环,删除修改信息的跳转

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */ 
    session_start();
    include_once 'conn.php';
    $url = $_SERVER["REQUEST_URI"];
    $url = parse_url($url);
    $url = $url['path'];
    if(isset($_GET['page'])){
        $page=intval($_GET['page']);
        
    }else{
        $page=1;
    }
$pagesize=2;
$query=  mysql_query("select count(*) from user");
$arr=  mysql_fetch_array($query);
$amount=$arr[0];
if($amount){
    if($amount<$pagesize){
        $pagecount=1;
        
    }
    if($amount%$pagesize){
        $pagecount=(int)($amount/$pagesize);
    }else{
        $pagecount=$amount/$pagesize;
    }
}
echo "<form action='shanchu.php' method='post'><table width=500 border='1'>";
$startcount=($page-1)*$pagesize;
$sql="select * from user limit $startcount,$pagesize";
$query=  mysql_query($sql);
$ii = 0;
while ($arr=  mysql_fetch_array($query)){
    //$ii++;
    echo "<input type='hidden' name='id[]' value='$arr[0]'>";
    echo "<tr><td name='username'>用户名:$arr[1]</td>";
    echo "<td name='pwd'>密  码:$arr[2]</td>";
    //echo "<td><input type='submit' name='delect' value='删除'></tr>";
    echo "<td><a href='shanchu.php'>删除</a></td>";
    echo "</br>\n";
}

echo "</table></form>"; 
$fpage=1;
$npage=$page+1;
$ppage=$page-1; 
$num=5;
$endpage=$page+floor($num/2);
$startpage=$endpage-$num+1;
if($pagecount-$page<$num/2){$startpage=$pagecount-$num+1;}
if($endpage>$pagecount){$endpage=$pagecount;}
if($endpage<$num){$endpage=$num;}
if($startpage<1){$startpage=1;}
if($page != 1){
    echo "<a href='list.php?page=$fpage'>首页</a> ";
    echo "<a href='list.php?page=$ppage'>上一页</a> ";
}
if($pagecount<$num){
    for($i=1;$i<=$pagecount;$i++){
        echo "<a href='list.php?page=".$i."'>".$i."</a> ";
    }
}else{
            for($i=$startpage;$i<=$endpage;$i++){
              echo "<a href='list.php?page=".$i."'>".$i."</a> ";
         }

}
if($page<$pagecount){
    echo "<a href='list.php?page=$npage'>下一页</a> ";
    echo "<a href='list.php?page=$pagecount'>尾页</a>";
}
echo "共有 $pagecount 页( $page / $pagecount )";
	echo  " 到第 <select name='topage' οnchange='window.location=\"$url?page=\"+this.value'>\n";
	for ($i = 1; $i <= $pagecount; $i++) {
			if ($i == $page){
			echo "<option value='$i' selected>$i</option>\n";
                        }else{
			echo "<option value='$i'>$i</option>\n";
        }               }
	echo " </select>页";
       //echo $_SESSION["username"];
       echo "<form method='post'>";
        echo "<input type='submit' name='xiugai' value='修改个人信息'></form>";
        if(isset($_POST['xiugai'])){
            echo "<script>location.href='xiugai.html';</script>";    
            }
zc.html
注册页面

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="zc.php" method="post">
            <table>
            <tr>
            <th>用户名:</th>
            <td>  <input  name="name" type="text"/></td>
            </tr>
            <tr>
            <th>密 码:</th>
            <td>    <input  name="pwd" type="password"  />  </td>
            </tr>
            <tr>           
            <th>姓名:</th>
            <td>        <input  name="username" type="text" /></td>
            </tr>
             <tr>           
            <th>性别:</th>
            <td><input  name="sex" type="radio" checked />男
            <input  name="sex" type="radio"  />女
            <input  name="sex" type="radio"  />保密
            </td>
            </tr> 
            <tr>           
            <th>选修课程:</th>
            <td><input  name="checkbox" type="checkbox" checked />php
            <input  name="checkbox" type="checkbox"/>mysql
            <input  name="checkbox" type="checkbox" />c
            </td>
            </tr>
            <tr>
                        
            <th>学历:</th>
            <td> <select name="edu">
                    <option value="1" selected>小学</option>
                    <option value="2">中学</option>
                    <option value="3">大学</option>
                </select>    
            </td>
            </tr>
            
            <tr><td><input type="submit" name="sub" value="确定" /> </td></tr>
            
            </table>
            
            
        </form>
    </body>
</html>
zc.php
简单的注册验证,写入数据库

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once 'conn.php';
$username=$_POST['username'];
$pwd=$_POST['pwd'];
$name=$_POST['name'];
$sex=$_POST['sex'];
$checkbox=$_POST['checkbox'];
$edu=$_POST['edu'];

if($_SERVER['REQUEST_METHOD']='POST'){
    
    $problem=FALSE;
    if(empty($_POST['username'])){
        
        $problem=TRUE;
        echo '请输入用户名:</br>';
    }
    if(empty($_POST['pwd'])){
        $problem=TRUE;
        echo '请输入密码:</br>';
    }
    if(empty($_POST['name'])){
        $problem=TRUE;
        echo '姓名:</br>';
    }
    if(empty($_POST['sex'])){
        $problem=TRUE;
        echo '请选择性别:</br>';
    }
    if(empty($_POST['checkbox'])){
        $problem=TRUE;
        echo '请选择选修课程:</br>';
    }
    if(empty($_POST['edu'])){
        $problem=TRUE;
        echo '请选择学历:</br>';
    }
    if(!$problem){
        $sql="insert into user (username,password,name,sex,checkbox,edu) values ('$username','$pwd','$name','$sex','$checkbox','$edu')";
        $query=  mysql_query($sql,$link);
        if(mysql_affected_rows($link)==1){
            echo "<a href='list.php?page=1'>注册成功请点击</a>";
        }else{
          
         echo "<a href='zc.html'>注册失败请点击返回</a>";
        }
        
    }else{
        echo '请检查表单是否填写完整.</br>';
         echo "<a href='zc.html'>注册失败请点击返回</a>";
    }
}

xiugai.html

修改页面

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="xiugai.php" method="post">
            <table>
            <tr>
            <th>用户名:</th>
            <td>  <input  name="name" type="text" value="<?php echo $_SESSION['name'];?>"/></td>
            </tr>
            <tr>
            <th>密 码:</th>
            <td>    <input  name="pwd" type="password"  />  </td>
            </tr>
            <tr>           
            <th>姓名:</th>
            <td>        <input  name="username" type="text" /></td>
            </tr>
             <tr>           
            <th>性别:</th>
            <td><input  name="sex" type="radio" checked />男
            <input  name="sex" type="radio"  />女
            <input  name="sex" type="radio"  />保密
            </td>
            </tr> 
            <tr>           
            <th>选修课程:</th>
            <td><input  name="checkbox" type="checkbox" checked />php
            <input  name="checkbox" type="checkbox"/>mysql
            <input  name="checkbox" type="checkbox" />c
            </td>
            </tr>
            <tr>
                        
            <th>学历:</th>
            <td> <select name="edu">
                    <option value="1" selected>小学</option>
                    <option value="2">中学</option>
                    <option value="3">大学</option>
                </select>    
            </td>
            </tr>
            
            <tr><td><input type="submit" name="sub" value="确定" /> </td><td>
                    <input type="reset" name="sub" value="重置" /> </td></tr>
            
            </table>
            
            
        </form>
    </body>
</html>
xiugai.php

//修改页面的php脚本

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
include_once 'conn.php';
$username=$_POST['username'];
$pwd=$_POST['pwd'];
$name=$_POST['name'];
$sex=$_POST['sex'];
$checkbox=$_POST['checkbox'];
$edu=$_POST['edu'];

if($_SERVER['REQUEST_METHOD']='POST'){
    
    $problem=FALSE;
    if(empty($_POST['username'])){
        
        $problem=TRUE;
        echo '请输入用户名:</br>';
    }
    if(empty($_POST['pwd'])){
        $problem=TRUE;
        echo '请输入密码:</br>';
    }
    if(empty($_POST['name'])){
        $problem=TRUE;
        echo '姓名:</br>';
    }
    if(empty($_POST['sex'])){
        $problem=TRUE;
        echo '请选择性别:</br>';
    }
    if(empty($_POST['checkbox'])){
        $problem=TRUE;
        echo '请选择选修课程:</br>';
    }
    if(empty($_POST['edu'])){
        $problem=TRUE;
        echo '请选择学历:</br>';
    }
    if(!$problem){
        $sql="UPDATE user SET 'username = $username', 'password = $pwd', "
                . "'name = $name', 'sex = $sex', 'checkbox = $checkbox', 'edu = $edu'";
        $query=  mysql_query($sql,$link);
        if(mysql_affected_rows($link)==1){
            echo "<a href='list.php?page=1'>修改成功成功请点击</a>";
        }else{
          
         echo "<a href='xiugai.php'>修改失败请点击返回</a>";
        }
        
    }else{
        echo '请检查表单是否填写完整.</br>';
         echo "<a href='xiugai.html'>修改失败请点击返回</a>";
    }
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值