<?php
//session_start();
require('methods.php');?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
p.plocation{
position:relative;
width:400px;
top:30px;
border:thin;
left:10px;
}
.location{
position:absolute;
left:480px;
top:18px;
border:thin;
}
</style>
</head><body><div  class="location"><form action="guestbook.php" method="post">
<table>
<tr>
<td height="81">输入框:</td>
<td><textarea name="text" cols="50" rows="10" ></textarea></td>
</tr>
<tr>
<td height="31"></td>
<td align="center"><input type="submit" value="提交" /></td>
</tr>
</table>
</form>
</div><?php
//提交
$mysql=db_connect();
$text=$_POST['text'];
//通过对比来减弱刷新造成的重复提交 
$query="select count(*) from guestbook where message='".$text."'";
$result=mysqli_query($mysql,$query);
$row=mysqli_fetch_row($result);
$count=$row[0];
//echo $count;
if($count>0){
echo'<p >禁止重复提交</p>';
}else{if($text!=null){
// echo $text;
$query="insert into guestbook values('','admin','".$text."')";
//因为user是主键,所以不能允许那两次插入时名字一样 撤掉主键设置。
$result=mysqli_query($mysql,$query);

}else{
echo'<p>内容不能为空</p>';
}//显示$query='select message from guestbook';
$result=mysqli_query($mysql,$query);
$rows=mysqli_num_rows($result);for($i=0;$i<$rows;$i++ ){
$message=mysqli_fetch_row($result);
echo '<p class="plocation">num'.$i.' : '.$message[0].' </p>';
echo'<form>';
echo'<p class="plocation"><input type="button" value="删除本条" onclick="'.del($message[0]).'"/></p>';

echo'</form>';}
// echo'<script language="javascript">';
// echo "this.location.replace('guestbook.php')";
// echo'<script>';
}?>
</body>
</html>

解决方案 »

  1.   

    //从数据库中删除一行记录
    function del($text){
    $mysql=db_connect();
    $query="delete from guestbook where message='".$text."'";
    $result=mysqli_query($mysql,$query);
    }再附上调用的del()函数代码
      

  2.   

    function del 是一个php函数?
      

  3.   

    onclick 只能触发客户端   php是服务端代码  不能这样子调用
    你可以通过一个a链接到其它页面来删除  或者用js来跳转页面
      

  4.   

    就是新建多一个页面 比如说 del.php页面
    然后把 echo'<p class="plocation"><input type="button" value="删除本条" onclick="'.del($message[0]).'"/></p>'; 换成下面的echo '<p class="plocation"><a href="del.php?xx="'.$message[0].'"/>删除本条</a>';在del页 
    $text = $_GET["xx"];
    来执行 
    $mysql=db_connect();
    $query="delete from guestbook where message='".$text."'";
    $result=mysqli_query($mysql,$query);
      

  5.   

    $text 不能得到 xx的值 貌似??为什么
      

  6.   

    xx 是我随便写的 你只要保证两个页面中的名称是一样的就可以
    你的$message[0]是有值的吗? 你鼠标放上去的时候 看下浏览器的状态栏
      

  7.   

    浏览器显示 del.php/xx=   是空的  也就是说 页面显示完成后,这个$message[0]的值 是空的了? 之前$message[0]必定是有值的,页面现实的就是他的值,然后下面跟个删除的连接。
      

  8.   

    你那删除按钮应该传个id比较好,把你的显示代码改为如下,你试试看。好不好使:
    //显示$query='select * from guestbook';
    $result=mysqli_query($mysql,$query);
    //$rows=mysqli_num_rows($result);
    $k=0;
    //for($i=0;$i<$rows;$i++ ){
    while($message=mysqli_fetch_assoc($result)){
    echo '<p class="plocation">num'.$k.' : '.$message[message].' </p>';
    echo '<p class="plocation"><a href="del.php?id="'.$message[id].'"/>删除本条</a>';</p>';
    $k++;
    }在del.php页 
    $text = $_GET["id"];
    来执行 
    $mysql=db_connect();
    $query="delete from guestbook where id='".$text."'";
    $result=mysqli_query($mysql,$query);
      

  9.   

    就是这个页面显示的时候 必定已经跳出while循环了。此时$message[id]已经为空了。 所以在点击连接时,不会得到相应的id的值吧.显示效果如下
    num0 : 的佛挡杀佛删除本条num1 : 得得得得得删除本条num2 : sdfsd删除本条num3 : cxg删除本条num4 : sdfsdf删除本条num5 : dsfds删除本条
      

  10.   

    del.php代码 加了两个输出(echo):
    <?php
    require('methods.php');
    $text = $_GET["id"];$mysql=db_connect();
    $query="delete from guestbook where id='".$text."'";echo '$text:'.$text.'<br>';
    echo '$query'.$query.'<br>';
    $result=mysqli_query($mysql,$query);
    ?>但是点击连接后 数据库中的记录没有删除。
    跳转页面后显示:
    $text:
    $querydelete from guestbook where id=''就是说 值没传过来吧。
      

  11.   

    对 值没传过来 你表中有id这个字段吧?
    你前面 num4 : sdfsdf 已经有输出内容了 
      

  12.   

    表中有 id这个字段 我尝试过 这个id字段可以正常输出 就是值传不过来 我觉得能不能找个数组来存这个值 一个变量id不行吧
      

  13.   

    成功了。谢谢大家了。大家的思路是对的,只是 引号那里有点问题。现附上成功的代码段
    $query='select * from guestbook';$result=mysqli_query($mysql,$query);$rows=mysqli_num_rows($result);for($i=0;$i<$rows;$i++ ){
    //while($message=mysqli_fetch_assoc($result)){
    $message=mysqli_fetch_assoc($result);
    echo '<p class="plocation">num'.$i.' : '.$message[message].' </p>';
    echo '<p class="plocation"><a href="del.php?id='.$message[id].'"/>删除本条</a>';}
    del.php为:
    <?php
    header("location:guestbook.php");require('methods.php');
    $text = $_GET["id"];$mysql=db_connect();
    $query="delete from guestbook where id='".$text."'";//echo '$text:'.$text.'<br>';
    //echo '$query'.$query.'<br>';
    $result=mysqli_query($mysql,$query);?>