我有一个简单的表单来自mysql db的显示条目,每个条目都有一组单选按钮 - 隐藏,显示和删除。正如我的代码现在所说,当我提交表单时没有任何反应。
这是我的表单代码
<form class='removeform' action='advertremoveedit.php' method='post'>
<?php
include '../inc/connect.php';
$q = mysqli_query($link, "SELECT * FROM adverts ORDER BY id ASC");
while($row = mysqli_fetch_array( $q )){
echo"<input type='radio' name='edit[{$row['id']}]' value='remove'>Remove";
if ($row['status'] == 1){
echo"<input type='radio' name='edit[{$row['id']}]' value='hide'>Hide";
}
}
echo"<input type='submit' name='submit' value='Submit' />";
?>
</form>
这是处理表单的php
if(isset($_POST['remove'])){
$chk = (array) $_POST['remove'];
$p = implode(',',array_keys($chk));
$t = "SELECT * FROM adverts WHERE id IN ($p)";
$result = $link->query($t);
$url=mysqli_fetch_array($result);
$image=$url['file'];
if ($result){
unlink($image);
$q = "DELETE FROM adverts WHERE id IN ($p)";
$delete = $link->query($q);
}
}
//if hide was clicked, change `status` to 1
if(isset($_POST['show'])){
$edit= $_POST['show'];
$chk = (array) $_POST['show'];
$p = implode(',',array_keys($chk));
$t = "SELECT * FROM adverts WHERE id IN ($p)";
$result = $link->query($t);
if ($result){
$q = "UPDATE adverts SET status = 1 WHERE id IN ($p)";
$show = $link->query($q);
}
}
编辑...提交代码
if(isset($_REQUEST['edit'])){
if($val=="hide"){
$chk = (array) $_POST['hide'];
$p = implode(',',array_keys($chk));
$t = "SELECT * FROM adverts WHERE id = ($p)";
$result = $link->query($t);
if ($result){
$q = "UPDATE adverts SET status = 2 WHERE id = ($p)";
$hide = $link->query($q);}}}
答案 0 :(得分:1)
试试这个
<强>形式:强>
<form class='removeform' action='advertremoveedit.php' method='post'>
<?php
include '../inc/connect.php';
$q = mysqli_query($link, "SELECT * FROM adverts ORDER BY id ASC");
while($row = mysqli_fetch_array( $q )){
echo"<input type='radio' name='edit[".$row['id']."]' value='remove'>Remove";
if ($row['status'] == 1){
echo"<input type='radio' name='edit[".$row['id']."]' value='hide'>Hide";
}
}
echo"<input type='submit' name='submit' value='Submit' />";
?>
</form>
并提交页面
if(isset($_REQUEST['edit']))
{
$arr_submit = $_REQUEST['edit'];
foreach($arr_submit as $id=>$val)
{
if($val=="remove")
{
// do your remove task for $id
}
else if($val=="hide")
{
// do you hide task for $id
}
}
}
更新:2
if(isset($_REQUEST['edit']))
{
$arr_submit = $_REQUEST['edit'];
foreach($arr_submit as $id=>$val)
{
if($val=="remove")
{
// delete task on $id
$t = "SELECT * FROM adverts WHERE id='$id' LIMIT 1";
$result = $link->query($t);
if($url=mysqli_fetch_array($result))
{
$image=$url['file'];
$q = "DELETE FROM adverts WHERE id='$id' LIMIT 1";
$delete = $link->query($q);
}
}
else if($val=="hide")
{
$q = "UPDATE adverts SET status = 2 WHERE id='$id' LIMIT 1";
$show = $link->query($q);
}
else if($val=="show")
{
$q = "UPDATE adverts SET status = 1 WHERE id='$id' LIMIT 1";
$show = $link->query($q);
}
}
}
答案 1 :(得分:0)
将数组绑定到IN
条件时,这是一个经典问题。
将$p
ID绑定到查询中时,会得到以下SQL:
SELECT * FROM adverts WHERE id IN ('1,2,3');
显然,没有广告具有荒谬的身份'1,2,3'
!
您有两个选择:
1)为$p
中的每个值运行单独的查询。
2)使用直接连接的值动态构建查询,或者作为首选选项,动态数量的占位符并分别绑定每个值。