<?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>
//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>
function del($text){
$mysql=db_connect();
$query="delete from guestbook where message='".$text."'";
$result=mysqli_query($mysql,$query);
}再附上调用的del()函数代码
你可以通过一个a链接到其它页面来删除 或者用js来跳转页面
然后把 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);
你的$message[0]是有值的吗? 你鼠标放上去的时候 看下浏览器的状态栏
//显示$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);
num0 : 的佛挡杀佛删除本条num1 : 得得得得得删除本条num2 : sdfsd删除本条num3 : cxg删除本条num4 : sdfsdf删除本条num5 : dsfds删除本条
<?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=''就是说 值没传过来吧。
你前面 num4 : sdfsdf 已经有输出内容了
$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);?>