1、登录表单及验证
<form action="login.php" method="post"> 用户名:<input type="text" name="uname"/> <br> 密 码:<input type="password" name="pwd"/> <br> <input type="submit" name="sub" value="用户登录"/> <input type="submit" name="sub1" value="管理员登录"/> <input type="submit" name="sub2" value="注册"/> </form>
登录验证
<?php header("content-type:text/html;charset=utf-8"); if(isset($_POST["sub"])){ try { @$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了 } catch (Exception $e) { echo "连接失败".$e->getMessage(); } try { $name=$_POST[‘uname‘]; $pwd=md5($_POST[‘pwd‘]); //$statm=$pdo->prepare("insert into login(uname,pwd)value(‘222‘,‘222‘)"); $statm=$pdo->prepare("select * from login where uname=:name and pwd=:pwd"); $statm->bindParam(‘:name‘, $name); $statm->bindParam(‘:pwd‘, $pwd); $statm->execute(); $c=$statm->rowCount(); if ($c>=1){ header(‘Location:kehu.php‘); }else { echo "<script type=‘text/javascript‘>alert(‘登录失败!‘);</script>"; } } catch (Exception $e) { echo "登录失败".$e->getMessage(); } }
注册用户
1 if(isset($_POST["sub2"])){ 2 try { 3 @$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了 4 } catch (Exception $e) { 5 echo "连接失败".$e->getMessage(); 6 } 7 try { 8 $name=$_POST[‘uname‘]; 9 $pwd=md5($_POST[‘pwd‘]); 10 $statm=$pdo->prepare("insert into login(uname,pwd)value(:name,:pwd)"); 11 $statm->bindParam(‘:name‘, $name); 12 $statm->bindParam(‘:pwd‘, $pwd); 13 $c=$statm->execute(); 14 if ($c){ 15 echo "<script type=‘text/javascript‘>alert(‘注册成功!‘);</script>"; 16 } 17 } catch (Exception $e) { 18 echo "注册失败".$e->getMessage(); 19 } 20 }
删除:
if ($_GET[‘del‘]){ try { @$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了 } catch (Exception $e) { echo "连接失败".$e->getMessage(); } try { $statm4=$pdo->prepare("select * from wz"); $statm4->execute(); $c=$statm4->fetchAll(); foreach ($c as $k=>$v){ echo "<li>"; echo "<a href=‘http://192.168.20.200/news/kehu.php?bs=$v[id]‘>{$v[biaot]}</a>"; echo "<a href=‘http://192.168.20.200/news/home1.php?del=4&deli=$v[id]‘> 删除!</a>"; echo "</li>"; } } catch (Exception $e) { echo "查询失败".$e->getMessage(); } if ($_GET[‘deli‘]){ try { $id=$_GET[‘deli‘]; $statm5=$pdo->prepare("delete from wz where id=:i"); $statm5->bindParam(‘:i‘, $id); $c=$statm5->execute(); } catch (Exception $e) { echo "失败".$e->getMessage(); } } }
发布:
if ($_GET[‘add‘]){ echo "<form action=‘home1.php‘ method=‘post‘><br>"; echo "标题:<input name=‘title‘ type=‘text‘ /><br><br>"; echo "<textarea name=‘content‘ style=‘width:300px;height:300px;border:1px solid black‘></textarea><br><br>"; echo "<input name=‘sub‘ type=‘submit‘ />"; echo "</form>"; } if(isset($_POST[‘sub‘])){ try { @$pdo=new PDO("mysql:hostname=localhost;dbname=news","root","123456");//当时出错了 } catch (Exception $e) { echo "连接失败".$e->getMessage(); } try { $title=$_POST[‘title‘]; $content=$_POST[‘content‘]; $statm1=$pdo->prepare("insert into wz(biaot,wcontent)value(:b,:c)"); $statm1->bindParam(‘:b‘, $title); $statm1->bindParam(‘:c‘, $content); $c=$statm1->execute(); } catch (Exception $e) { echo "失败".$e->getMessage(); } }
时间: 2024-11-26 21:47:53