gpt4 book ai didi

php - 单选按钮,查找隐藏、显示或删除是否被点击并执行操作

转载 作者:行者123 更新时间:2023-11-29 12:50:15 24 4
gpt4 key购买 nike

我有一个简单的表单,显示来自 mysql 数据库的条目,每个条目都有一组单选按钮 - 隐藏、显示和删除。正如我现在的代码所示,当我提交表单时没有任何反应。

这是我的表单代码

<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);}}}

最佳答案

试试这个

形式:

<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);
}
}
}

关于php - 单选按钮,查找隐藏、显示或删除是否被点击并执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24864030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com