【问题标题】:PHP update SQL from multiple HTML checkboxPHP 从多个 HTML 复选框更新 SQL
【发布时间】:2019-09-18 03:53:13
【问题描述】:

所以,我需要确保在按下按钮时为 DB 设置了正确的参数。我只想正确地调用和比较,以便在我按下按钮时它会做它应该做的事情。数据库中的每一行都应该有一个删除按钮,当我按下它时更新页面。

应该可以通过更改表单并按保存按钮来更新 MySQL 呈现的表单中的文本/数字,然后刷新页面。

$counter = 1;

if(isset($_POST['save']) and something is changed from the forms compared to DB) {
Update MySQL

Refresh page
<script>
window.location.replace("thispage.php");
</script>

}

if(isset($_POST['del'])) {
DELETE MySQL

Refresh page
<script>
window.location.replace("thispage.php");
</script>

}

echo "<tr><td>ID</td><td>Namn</td><td>Platser</td><td>Fullbokad</td><td>Ta bort</td></tr>";

$sqlListSections = "SELECT * FROM avdelningar WHERE user = {$_SESSION['id']}";
$queryListSections = mysqli_query($mysqli, $sqlListSections);
$del = [];

while($rowListSections = mysqli_fetch_array($queryListSections)) 
{   

    if($counter%2) 
    {
        echo "\n<tr bgcolor=#F1F1F2>\n\n";
    }else
    {
        echo "\n<tr bgcolor=#FFFFFF>\n\n";
    }

    $counter++;



    echo "

    <td>".$rowListSections['id']."</td>
    <td>
        <input type=text value=".$rowListSections['namn']."></td>

    <td>
        <input type=text value=".$rowListSections['platser']."></td>
    <td>";
    if($rowListSections['prio'] == 1) 
    {
        echo "<select name=platser>
            <option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
            <option value='0'>0</option>".$rowListSections['prio'];
    }elseif($rowListSections['prio'] == 0)
    {
        echo "<select name=platser>
            <option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
            <option value='1'>1</option>".$rowListSections['prio'];
    }
    echo "</td>
    <td>
        <form method=post action=thispage.php>
        <input type=submit value=Delete name=del>";
    </td>
    </form>
    </tr>";


}

echo "<form method=post action=thispage.php>
<input type=submit value=Save name=save>";
`






【问题讨论】:

  • 您能详细说明一下吗?你想达到什么目标?
  • 我要标记我要删除的部分,按保存按钮并删除所有选中的部分并显示新结果

标签: php html checkbox


【解决方案1】:

在您的复选框中将命名更改为数组。

&lt;input type=checkbox name="del[]" value={$rowListSections['id']}&gt;

喜欢

echo $rowListSections["id"].' '.$rowListSections["namn"].' '.$rowListSections["platser"].' '.

$rowListSections["prio"].'';

在您的if(isset($_POST)) 中,您可以获得一个del 数组,因此您可以像下面这样循环该数组。

foreach($del as $val){
    $id = $val;
    $sql_query_for_update = "update table_name set field = 1 where id= '$id' ";
}

【讨论】:

  • 我不明白。我不能在我的复选框行中写 name=del[] 而没有错误。你能给我所有的代码示例吗?
  • 我还是不明白。我不能在 中写 del[]?那是一个没有$的数组吗?
  • 是的!如果将 [] 放在名称中,它将被视为数组。例如:如果您提交此表单,那么您将获得所有选中的复选框作为一个数组。 ( $_POST['del'] = [0 =&gt; 'val', 1 =&gt; 'val1'])
  • 如果您不明白,请告诉我 :)
  • 看起来很棒!晚上回家后我会试试的!我希望 del[] 现在可以工作了!谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 2013-01-01
  • 2014-10-25
相关资源
最近更新 更多