关于编辑PHP留言板遇到问题分享!

作者在 2011-11-03 22:34:52 发布以下内容
<?php
include("conn.php");
session_start();

//对登录的用户进行判断
if(isset($_SESSION['unc'])){
     $sql=mysql_query("select id from tb_user where `Usernc`='".$_SESSION['unc']."'") or die (mysql_error());
     $info=mysql_fetch_array($sql);
     $useid=$info['id'];
     if($useid){
         $sql="select * from  tb_leaveword";
         $query1=mysql_query($sql);
         $row=mysql_fetch_array($query1);
         //echo $row[id];
         $_SESSION[uid]= $row[id];//这里是我做了个权限判断,如果你是登录的用户id,你可以进行留言
     }
}

  if(isset($_POST["submit"])){//如果单击提交按钮话,就可以实现修改你的留言内容
  if(mysql_query("update tb_leaveword set title='".$_POST["title"]."',content='".$_POST["content"]."' where id='".$_POST["id"]."'",$conn)){
  $url=urlencode("查看留言");
      echo "<script>alert('留言更改成功!');</script>";

  }else{
      echo "<script>alert('留言更改失败!');</script>";
  }
//exit;
  }
$id=$_SESSION[uid];
$sql="select * from tb_leaveword where id='".$id."'";
$query=mysql_query($sql);
$info=mysql_fetch_array($query);
print_r($info);//如果用户单击编辑,则查询该用户留言内容,并最将未编辑留言内容显示在编辑中
?>

<table>
  <tr>
    <td></td>
  </tr>
</table>
<table width="550" height="200" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#FFFFFF"bgcolor="#FCD424">

<script language="javascript">
  function chkinput(form){
    if(form.title.value==""){

      alert("留言主题不能为空!");
      form.title.focus();
      return(false);

    }

     if(form.content.value==""){

      alert("留言内容不能为空!");
      form.content.focus();
      return(false);

    }
   return(true);

  }

</script>

<form name="form1" method="post" action="editleaveword.php" onSubmit="return chkinput(this)">

  <tr>
    <td height="25" colspan="2" background="images/dh_back_1.gif" bgcolor="#FFFFFF">&nbsp;&nbsp;&nbsp;<img src="images/biao.gif">&nbsp;编辑留言</td>
  </tr>
  <tr>
    <td width="76" height="25" bgcolor="#FFFFFF"><div align="center">留言主题:
</td>
    <td width="371" bgcolor="#FFFFFF">&nbsp;<input name="title" type="text" class="inputcss" size="45" value="<?php echo $info['title'];?>"></td>
  </tr>
  <tr>
    <td height="200" bgcolor="#FFFFFF"><div align="center">留言内容:
</td>
    <td height="200" bgcolor="#FFFFFF">&nbsp;<textarea name="content" cols="52" rows="12" class="inputcss"><?php echo $info['content'];?></textarea></td>
  </tr>
  <tr>
    <td height="25" colspan="2" bgcolor="#FFFFFF"><div align="center"><input type="hidden" name="id" value="<?php echo $row[id];?>"><input type="submit" value="编辑" class="buttoncss" name="submit">&nbsp;&nbsp;<input type="reset" value="取消" class="buttoncss">
</td>
  </tr>
  </form>

</table>
 其实,我在写的时候总是忽略ID,
if(isset($_SESSION['unc'])){
     $sql=mysql_query("select id from tb_user where `Usernc`='".$_SESSION['unc']."'") or die (mysql_error());
     $info=mysql_fetch_array($sql);
     $useid=$info['id'];
     if($useid){
         $sql="select * from  tb_leaveword";
         $query1=mysql_query($sql);
         $row=mysql_fetch_array($query1);
         //echo $row[id];
         $_SESSION[uid]= $row[id];//这里是我做了个权限判断,如果你是登录的用户id,你可以进行留言
     }
}
 这个是我自己写的权限判断,登录用户session 和留言者进行比较。我用SESSION保存我的$_SESSION[uid]= $row[id];这样的话,我就实现编辑成功!获取数据库ID很简单,用到以上查询数据库的语句+session 就OK!




默认分类 | 阅读 1020 次
收藏
文章评论,共0条
游客请输入验证码
浏览90921次
文章分类
文章归档
最新评论