PHP MySQLi 增删改查

1:登陆页面

[php]  view plain  copy
 print ?
  1. //前端显示部分  
  2.   
  3. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. "http://www.w3.org/1999/xhtml">  
  5.   
  6. "Content-Type" content="text/html; charset=utf-8" />  
  7. 无标题文档  
  8.   
  9.   
  10.   
  11. 管理员登陆

      
  12. "dlyz.php" method="post">  
  13. 用户名:"text" name="user" value="请输入您的工号" />
      
  14.   
  15. 密  码:"password" name="psd" />
      
  16.   
  17. "submit" value="登录" />  
  18. "submit" value="注册新用户" formaction="zhuc.php"/>  
  19.   
  20.   
  21.   
//php对提交登陆信息的处理

[php]  view plain  copy
 print ?
  1.   
  2. $user = $_POST["user"];  
  3. $psd = $_POST["psd"];  
  4.   
  5.   
  6. //造对象  
  7. $db = new MySQLi("localhost","root","","newssystem");  
  8.   
  9. //判断是否出错  
  10. !mysqli_connect_error() or die("连接失败!!");  
  11.   
  12. //写sql语句  
  13.   
  14. $sql = "select psd from yonghu where user='{$user}'";  
  15.   
  16.   
  17. //执行SQL语句  
  18.   
  19. $result = $db-> query($sql);  
  20. $v = $result->fetch_row();  
  21. if($psd==$v[0])  
  22. {  
  23. header("location:fabuxinwen.php");  
  24. }  
  25. else  
  26. {  
  27. echo"您输入的用户名或密码不正确,请重新输入!!";  
  28. }  

2:注册页面

[php]  view plain  copy
 print ?
  1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. "http://www.w3.org/1999/xhtml">  
  3.   
  4. "Content-Type" content="text/html; charset=utf-8" />  
  5. 无标题文档  
  6.   
  7.   
  8.   
  9. 欢迎注册

      
  10.   
  11.   
  12. "zhucyz.php"method="post">  
  13. 用户名:"text" name="user" value="请输入您的工号"/>
      
  14.   
  15. 密  码:"password" name="psd" />
      
  16.   
  17. "submit" value="提交" />  
  18.   
  19.   
  20.   
  21.   
  22.   
  23.    
  24.   
  25. $user = $_POST["user"];  
  26. $psd = $_POST["psd"];  
  27.   
  28.   
  29. //造对象  
  30. $db = new MySQLi("localhost","root","","newssystem");  
  31.   
  32. //判断是否出错  
  33. !mysqli_connect_error() or die("连接失败!!");  
  34.   
  35. //写sql语句  
  36.   
  37. $sql = "insert into yonghu values('{$user}','{$psd}')";  
  38.   
  39. //执行SQL语句  
  40.   
  41. $result = $db-> query($sql);  
  42.   
  43. if($result)  
  44. {  
  45. header("location:dl.php");  
  46. }  
  47. else  
  48. {  
  49. echo"很抱歉,注册失败!!";  
  50. }  

3:登陆进去以后,是发布页面,可以发布和查看

[php]  view plain  copy
 print ?
  1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. "http://www.w3.org/1999/xhtml">  
  3.   
  4. "Content-Type" content="text/html; charset=utf-8" />  
  5. 无标题文档  
  6.   
  7.   
  8.   
  9. "width:100%; text-align:center" >  
  10. 发布新闻

      
  11. "post">  
  12. "hidden" name="newsid"/>  
  13. "margin:0 auto; text-align:left" >  
  14.   
  15. 标题:"text" style="width:400px" name="title" />  
  16.   
  17.   
  18. 作者:  
  19. "text" style="width:400px" name="Author" />  
  20.   
  21.   
  22. 来源:"text" style="width:400px" name="source"/>  
  23.   
  24.   
  25. 内容:  
  26. "auto" rows="auto" style="width:400px; height:400px" name="content">  
  27.   
  28.   
  29.   
  30. $time = date('y-m-d h:i:s');  
  31.   
  32. echo "";  
  33. ?>  
  34.   
  35. "submit" value="提交" formaction="tijiao.php"/>  
  36.   
  37. "submit" value="查看" formaction="chakan.php"/>  
  38.   
  
  •   
  •   
  •   
  •    

  • [php]  view plain  copy
     print ?
    1. $title = $_POST["title"];  
    2. $Author = $_POST["Author"];  
    3. $source = $_POST["source"];  
    4. $content = $_POST["content"];  
    5.   
    6. //造对象  
    7. $db = new MySQLi("localhost","root","","newssystem");  
    8.   
    9. //判断是否出错  
    10. !mysqli_connect_error() or die("添加失败!!");  
    11.   
    12. //写sql语句  
    13.   
    14. $sql = "insert into news(title,Author,source,content) values('{$title}','{$Author}','{$source}','{$content}')";  
    15.   
    16.   
    17. //执行SQL语句  
    18.   
    19. $result = $db-> query($sql);  
    20.   
    21. if($result)  
    22. {  
    23. header("location:fabuxinwen.php");  
    24. }  
    25. else  
    26. {  
    27. echo"很抱歉,添加失败!!";  
    28. }  
    4:查看页面

    [php]  view plain  copy
     print ?
    1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    2. "http://www.w3.org/1999/xhtml">  
    3.   
    4. "Content-Type" content="text/html; charset=utf-8" />  
    5. 无标题文档  
    6.   
    7.   
    8.   
    9. "70%" border="1px" cellpadding="0" cellspacing="0" style="text-align:center">  
    10.   
    11. 编号  
    12. 标题  
    13. 作者  
    14. 来源  
    15. 日期  
    16. 删除  
    17. 修改  
    18.   
    19.   
    20.   
    21. $db=new mysqli("localhost","root","","newssystem");  
    22. !mysqli_connect_error() or die("连接错误");  
    23. $sql="select * from news";  
    24. $result=$db->query($sql);  
    25. while($attr=$result->fetch_row())  
    26. {  
    27. echo "  
    28.   
    29. {$attr[0]}  
    30. {$attr[1]}  
    31. {$attr[2]}  
    32. {$attr[3]}  
    33. {$attr[5]}  
    34. " return confirm('确定删除')\" href='scchuli.php?newsid={$attr[0]}'>删除  
    35. 'xiugai.php?newsid={$attr[0]}'>修改  
    36.    
    37. ";  
    38. }  
    39.   
    40. ?>  
    41.   
    42.   
    43.   
    5:在查看页面可以进行修改和删除
    [php]  view plain  copy
     print ?
    1. "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    2. "http://www.w3.org/1999/xhtml">  
    3.   
    4. "Content-Type" content="text/html; charset=utf-8" />  
    5. 无标题文档  
    6.   
    7.   
    8.   
    9. $id = $_GET["newsid"];  
    10.   
    11. $db=new mysqli("localhost","root","","newssystem");  
    12. !mysqli_connect_error() or die("连接错误");  
    13. $sql="select * from news where newsid='{$id}'";  
    14. $result=$db->query($sql);  
    15. $attr=$result->fetch_row();  
    16. ?>  
    17. "width:100%; text-align:center" >  
    18. 修改新闻

        
    19. "xgchuli.php" method="post">  
    20. "hidden" name="newsid" echo "value='{$attr[0]}'";?>/>  
    21. "margin:0 auto; text-align:left" >  
    22.   
    23. 标题:"text" style="width:400px" name="title" echo "value='{$attr[1]}'";?>/>  
    24.   
    25.   
    26. 作者:  
    27. "text" style="width:400px" name="Author" echo "value='{$attr[2]}'";?>/>  
    28.   
    29.   
    30.   
    31. 来源:"text" style="width:400px" name="source" echo "value='{$attr[3]}'";?>/>  
    32.   
    33.   
    34.   
    35. 内容:  
    36. "auto" rows="auto" style="width:400px; height:400px" name="content">echo "{$attr[4]}";?>  
    37.   
    38.   
    39.   
    40.   
    41. $time = date('y-m-d h:i:s');  
    42.   
    43. echo "";  
    44. ?>  
    45.   
    46. "chakan.php">"button" title="查看" value="查看" />"submit" title="修改" value="修改"/>  
      
  •   
  •   
  •   
  •   
  •   
  •   
  •    
  •   
  •   
  • $id=$_POST["newsid"];  
  • $title=$_POST["title"];  
  • $Author=$_POST["Author"];  
  • $source=$_POST["source"];  
  • $content=$_POST["content"];  
  • $time=$_POST["time"];  
  •   
  • $db = new MySQLi("localhost","root","","newssystem");  
  •   
  • !mysqli_connect_error() or die("连接失败");  
  •   
  • $sql="update news set title='{$title}',Author='{$Author}',source='{$source}',content='{$content}',time='{$time}' where newsid='{$id}' ";  
  •   
  • $result=$db->query($sql);  
  • if ($result)  
  • {  
  • header("location:chakan.php");  
  • }  
  • else  
  • {  
  • echo "修改失败";  
  • }  

  • //删除数据

    [php]  view plain  copy
     print ?
    1. $id=$_GET["newsid"];  
    2.   
    3. $db=new mysqli("localhost","root","","newssystem");  
    4.   
    5. !mysqli_connect_error() or die("连接失败");  
    6.   
    7. $sql="delete from news where newsid='{$id}'";  
    8.   
    9. $result=$db->query($sql);  
    10.   
    11. if ($result)  
    12. {  
    13. header("location:chakan.php");  
    14. }  
    15. else  
    16. {  
    17. echo "删除失败";  
    18. }  

    6:修改页面

    [php]  view plain  copy
     print ?
    1. $title = $_POST["title"];  
    2. $Author = $_POST["Author"];  
    3. $source = $_POST["source"];  
    4. $content = $_POST["content"];  
    5.   
    6. //造对象  
    7. $db = new MySQLi("localhost","root","","newssystem");  
    8.   
    9. //判断是否出错  
    10. !mysqli_connect_error() or die("添加失败!!");  
    11.   
    12. //写sql语句  
    13.   
    14. $sql = "insert into news(title,Author,source,content) values('{$title}','{$Author}','{$source}','{$content}')";  
    15.   
    16.   
    17. //执行SQL语句  
    18.   
    19. $result = $db-> query($sql);  
    20.   
    21. if($result)  
    22. {  
    23. header("location:fabuxinwen.php");  
    24. }  
    25. else  
    26. {  
    27. echo"很抱歉,添加失败!!";  
    28. }  

    你可能感兴趣的:(PHP MySQLi 增删改查)