检查发现post为空 应该怎么写 求助

den123454pm 2015-07-24 12:51:08
新手一个 刚自学了两天 过程中卡在不少地方过 这里是好不容易各种查各种找才搞出来的代码 现在卡在这里 求助啊
<?php
$myconn=mysql_connect("localhost","root","");
mysql_select_db("login_test",$myconn);
$strSql="select * from table_test";
$result=mysql_query($strSql,$myconn);
?>
<html>
<head>
<title>Print test</title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<br>
显示:
<br>
<form METHOD="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table id="table1">
<tr class="table_head">
<td>objname</td>
<td>objinfo</td>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['objname']?></td>
<td><?php echo $row['objinfo']?></td>
<td><input type="hidden" name="id" value="<?php echo $row['id']?>"><input type="submit" value="delete"/></td>
</tr>
<?php
}
?>
</table>
<div><a href="http://localhost/test2/submit.php">点此添加信息</a></div>
</form>
<?php
echo empty($_POST);
/*if(!empty($_POST)){
$id = $_POST['id'];
$sql = "delete from student where objid='$id'";
$query = mysql_query($sql);
if($query){
echo "<script>alert('删除成功');history.back();</script>";
}
}*/
?>
</body>
<?php mysql_close($myconn); ?>
</html>
...全文
184 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
富川龙 2015-07-26
  • 打赏
  • 举报
回复
而且提交到当前页面的话表单action可以为空
富川龙 2015-07-26
  • 打赏
  • 举报
回复
你可以改为使用复选框来让用户选中数据,复选框的名字为lst[],所有的复选框都叫这个,然后值为$row[id],然后通过一个按钮来提交数据到删除界面,php接收lst就能获取到所有的选中数据
lirenbing008 2015-07-24
  • 打赏
  • 举报
回复
引用 14 楼 den123454pm 的回复:
[quote=引用 12 楼 lirenbing008 的回复:] [quote=引用 10 楼 den123454pm 的回复:] [quote=引用 7 楼 xuzuning 的回复:] 你点击 delete 按钮了吗? 贴出浏览器中的 html
之前的错误只是之一啊 我数据库里写的是objid $row提的是id 这里错了 改了之后value得到值了 但是为啥post还是空啊

<html>
	<head>
		<title>Print test</title>
		<link href="css.css" rel="stylesheet" type="text/css" />
	</head>	
	<body>
		<br>
		显示:
		<br>

		<table id="table1">
			<tr class="table_head">
			<td>objname</td>
			<td>objinfo</td>
			</tr>
						<form METHOD="POST">
			<tr>
			<td>obj1</td>
			<td>this is obj1</td>
			<td><input type="hidden" name="id" value="1">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj2</td>
			<td>this is obj2</td>
			<td><input type="hidden" name="id" value="2">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj3</td>
			<td>this is obj3</td>
			<td><input type="hidden" name="id" value="3">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
					</table>
		<div><a href="http://localhost/test2/submit.php">点此添加信息</a></div>
		1Array
(
)
	</body>
</html>
[/quote] 额、、、 你while还是放form里面去吧。。。[/quote] 还是放里面才对吗?[/quote] 这么多form 看着不别扭。。。
den123454pm 2015-07-24
  • 打赏
  • 举报
回复
引用 12 楼 lirenbing008 的回复:
[quote=引用 10 楼 den123454pm 的回复:] [quote=引用 7 楼 xuzuning 的回复:] 你点击 delete 按钮了吗? 贴出浏览器中的 html
之前的错误只是之一啊 我数据库里写的是objid $row提的是id 这里错了 改了之后value得到值了 但是为啥post还是空啊

<html>
	<head>
		<title>Print test</title>
		<link href="css.css" rel="stylesheet" type="text/css" />
	</head>	
	<body>
		<br>
		显示:
		<br>

		<table id="table1">
			<tr class="table_head">
			<td>objname</td>
			<td>objinfo</td>
			</tr>
						<form METHOD="POST">
			<tr>
			<td>obj1</td>
			<td>this is obj1</td>
			<td><input type="hidden" name="id" value="1">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj2</td>
			<td>this is obj2</td>
			<td><input type="hidden" name="id" value="2">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj3</td>
			<td>this is obj3</td>
			<td><input type="hidden" name="id" value="3">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
					</table>
		<div><a href="http://localhost/test2/submit.php">点此添加信息</a></div>
		1Array
(
)
	</body>
</html>
[/quote] 额、、、 你while还是放form里面去吧。。。[/quote] 还是放里面才对吗?
den123454pm 2015-07-24
  • 打赏
  • 举报
回复
引用 11 楼 xuzuning 的回复:
你是指 <div><a href="http://localhost/test2/submit.php">点此添加信息</a></div> 1Array ( ) 这个是空的吗? 这里要提交(点击 delete 按钮)后才会有值!
点了delete之后发现post有值了 万分感谢!
lirenbing008 2015-07-24
  • 打赏
  • 举报
回复
引用 10 楼 den123454pm 的回复:
[quote=引用 7 楼 xuzuning 的回复:] 你点击 delete 按钮了吗? 贴出浏览器中的 html
之前的错误只是之一啊 我数据库里写的是objid $row提的是id 这里错了 改了之后value得到值了 但是为啥post还是空啊

<html>
	<head>
		<title>Print test</title>
		<link href="css.css" rel="stylesheet" type="text/css" />
	</head>	
	<body>
		<br>
		显示:
		<br>

		<table id="table1">
			<tr class="table_head">
			<td>objname</td>
			<td>objinfo</td>
			</tr>
						<form METHOD="POST">
			<tr>
			<td>obj1</td>
			<td>this is obj1</td>
			<td><input type="hidden" name="id" value="1">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj2</td>
			<td>this is obj2</td>
			<td><input type="hidden" name="id" value="2">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj3</td>
			<td>this is obj3</td>
			<td><input type="hidden" name="id" value="3">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
					</table>
		<div><a href="http://localhost/test2/submit.php">点此添加信息</a></div>
		1Array
(
)
	</body>
</html>
[/quote] 额、、、 你while还是放form里面去吧。。。
xuzuning 2015-07-24
  • 打赏
  • 举报
回复
你是指 <div><a href="http://localhost/test2/submit.php">点此添加信息</a></div> 1Array ( ) 这个是空的吗? 这里要提交(点击 delete 按钮)后才会有值!
den123454pm 2015-07-24
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
你点击 delete 按钮了吗? 贴出浏览器中的 html
之前的错误只是之一啊 我数据库里写的是objid $row提的是id 这里错了 改了之后value得到值了 但是为啥post还是空啊

<html>
	<head>
		<title>Print test</title>
		<link href="css.css" rel="stylesheet" type="text/css" />
	</head>	
	<body>
		<br>
		显示:
		<br>

		<table id="table1">
			<tr class="table_head">
			<td>objname</td>
			<td>objinfo</td>
			</tr>
						<form METHOD="POST">
			<tr>
			<td>obj1</td>
			<td>this is obj1</td>
			<td><input type="hidden" name="id" value="1">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj2</td>
			<td>this is obj2</td>
			<td><input type="hidden" name="id" value="2">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj3</td>
			<td>this is obj3</td>
			<td><input type="hidden" name="id" value="3">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
					</table>
		<div><a href="http://localhost/test2/submit.php">点此添加信息</a></div>
		1Array
(
)
	</body>
</html>
den123454pm 2015-07-24
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
你点击 delete 按钮了吗? 贴出浏览器中的 html
中了hidden的招了 30行的value定错了 真是太感谢了 谢谢帮助!
den123454pm 2015-07-24
  • 打赏
  • 举报
回复

<html>
	<head>
		<title>Print test</title>
		<link href="css.css" rel="stylesheet" type="text/css" />
	</head>	
	<body>
		<br>
		显示:
		<br>

		<table id="table1">
			<tr class="table_head">
			<td>objname</td>
			<td>objinfo</td>
			</tr>
						<form METHOD="POST">
			<tr>
			<td>obj1</td>
			<td>this is obj1</td>
			<td><input type="hidden" name="id" value="<br />
<b>Notice</b>:  Undefined index: id in <b>C:\xampp\htdocs\test2\test.php</b> on line <b>30</b><br />
">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj2</td>
			<td>this is obj2</td>
			<td><input type="hidden" name="id" value="<br />
<b>Notice</b>:  Undefined index: id in <b>C:\xampp\htdocs\test2\test.php</b> on line <b>30</b><br />
">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
						<form METHOD="POST">
			<tr>
			<td>obj3</td>
			<td>this is obj3</td>
			<td><input type="hidden" name="id" value="<br />
<b>Notice</b>:  Undefined index: id in <b>C:\xampp\htdocs\test2\test.php</b> on line <b>30</b><br />
">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
					</table>
		<div><a href="http://localhost/test2/submit.php">点此添加信息</a></div>
		1Array
(
)
	</body>
</html>
引用 7 楼 xuzuning 的回复:
你点击 delete 按钮了吗? 贴出浏览器中的 html
xuzuning 2015-07-24
  • 打赏
  • 举报
回复
你点击 delete 按钮了吗? 贴出浏览器中的 html
den123454pm 2015-07-24
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
如果你想通过表单提交来实现删除,那么每行(tr)都应有一个表单(form) <form METHOD="POST" action="<?php echo $_SERVER['PHP_SELF'];?>"> <tr> <td><?php echo $row['objname']?></td> <td><?php echo $row['objinfo']?></td> <td><input type="hidden" name="id" value="<?php echo $row['id']?>"><input type="submit" value="delete"/></td> </tr> </form> 而包围在外面的表单是不需要的 action="<?php echo $_SERVER['PHP_SELF'];?>" 是提交到本页,可以不写 这样提交后,$_POST['id'] 才是待删除的 id
<?php
	$myconn=mysql_connect("localhost","root","");
	mysql_select_db("login_test",$myconn);
	$strSql="select * from table_test";
	$result=mysql_query($strSql,$myconn);
?>
<html>
	<head>
		<title>Print test</title>
		<link href="css.css" rel="stylesheet" type="text/css" />
	</head>	
	<body>
		<br>
		显示:
		<br>

		<table id="table1">
			<tr class="table_head">
			<td>objname</td>
			<td>objinfo</td>
			</tr>
			<?php
				while($row=mysql_fetch_array($result))
				{
			?>
			<form METHOD="POST">
			<tr>
			<td><?php echo $row['objname']?></td>
			<td><?php echo $row['objinfo']?></td>
			<td><input type="hidden" name="id" value="<?php echo $row['id']?>">
				<input type="submit" value="delete"/></td>
			</tr>
			</form>
			<?php
				}
			?>
		</table>
		<div><a href="http://localhost/test2/submit.php">点此添加信息</a></div>
		<?php
				echo empty($_POST);
				print_r($_POST);
				/*if(!empty($_POST)){
			        $id = $_POST['id'];
			        $sql = "delete from student where objid='$id'";
			        $query = mysql_query($sql);
			        if($query){
			            echo "<script>alert('删除成功');history.back();</script>"; 
			        }
			    }*/
		?>
	</body>
<?php mysql_close($myconn); ?>
</html>
我改成这样以后 echo的post依然是空啊 是哪出问题了?
xuzuning 2015-07-24
  • 打赏
  • 举报
回复
如果你想通过表单提交来实现删除,那么每行(tr)都应有一个表单(form) <form METHOD="POST" action="<?php echo $_SERVER['PHP_SELF'];?>"> <tr> <td><?php echo $row['objname']?></td> <td><?php echo $row['objinfo']?></td> <td><input type="hidden" name="id" value="<?php echo $row['id']?>"><input type="submit" value="delete"/></td> </tr> </form> 而包围在外面的表单是不需要的 action="<?php echo $_SERVER['PHP_SELF'];?>" 是提交到本页,可以不写 这样提交后,$_POST['id'] 才是待删除的 id
den123454pm 2015-07-24
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
提交后,$_POST 不会为空 但 $_POST['id'] 只用一个,并且是最后一个 $row['id'] 这显然是不对的
版主大大 那如果我要给每一行附一个ID 用于之后的定位和删除应该怎么写呢
xuzuning 2015-07-24
  • 打赏
  • 举报
回复
提交后,$_POST 不会为空 但 $_POST['id'] 只用一个,并且是最后一个 $row['id'] 这显然是不对的
den123454pm 2015-07-24
  • 打赏
  • 举报
回复
引用 1 楼 fdipzone 的回复:
没看到你有submit。 你可以加上print_r($_POST); 看看有没有post
我在29行后面有个submit 用print_r($_POST)查看后是空的Array
傲雪星枫 2015-07-24
  • 打赏
  • 举报
回复
没看到你有submit。 你可以加上print_r($_POST); 看看有没有post

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧