提交后更新复选框

时间:2015-05-10 15:19:34

标签: php forms checkbox

我目前遇到了一个问题,我的表单中包含复选框。我从数据库中获取复选框的用户首选项值。一切都很好,并做了应该做的事情,但是在我更改并检查一些框然后点击提交按钮后,它仍然会再次向表单显示旧值。如果我再次在页面中再次单击它将显示新值。

代码如下所示,带有注释。

<form action="myprofile.php" method="post">
<?php $usr_cats=array();
$qry_usrcat="SELECT category_id_fk 
             FROM user_categories 
             WHERE user_id_fk='".$_SESSION['user_id']."';";
$result = mysqli_query($conn,$qry_usrcat);
while($row = mysqli_fetch_array($result)){
        $usr_cats[] = $row[0]; // getting user categories from db stored in array
}

$query_allcats="SELECT category_id,category_name, portal_name 
                FROM categories
                INNER JOIN portals on categories.portal_id=portals.portal_id
                ORDER BY category_id;";  // select all category queries
$result = mysqli_query($conn,$query_allcats);
while($row = mysqli_fetch_array($result)){
    echo $row['portal_name'] . "<input "; //print categories
    if(in_array($row['category_id'], $usr_cats)){ // if in array from db, check the checkbox
        echo "checked ";
     }
    echo "type='checkbox' name='categories[]' value='";
    echo $row['category_id']."'> ". $row['category_name']."</br>\n\t\t\t\t\t\t";
}
?>
<input type="submit" name="submit" value="Submit"/>
 <?php
$qry_del_usrcats="DELETE FROM user_categories
                WHERE user_id_fk='".$_SESSION['user_id']."';"; //delete all query
if(isset($_POST['submit'])){
    if(!empty($_POST['categories'])){
        $cats= $_POST['categories'];
        $result = mysqli_query($conn,$qry_del_usrcats); //delete all
        for ($x = 0; $x < count($cats); $x++) {
            $qry_add_usrcats="INSERT INTO `user_categories` (`user_id_fk`, `category_id_fk`) 
                     VALUES ('".$_SESSION['user_id']."', '".$cats[$x]."');";
           $result = mysqli_query($conn,$qry_add_usrcats);    
        }
        echo "success";
    }
    elseif(empty($_POST['categories'])){ //if nothing is selected delete all
        $result = mysqli_query($conn,$qry_del_usrcats);
    }
    unset($usr_cats);
    unset($cats);
}
?>

我不确定导致这样做的原因。在提交后,有些事情导致不更新表单。但是,正如我所说的,在提交之后一切都很有意义,值存储并保存在DB中,但未在表单上显示/更新。如果您需要任何澄清,请告诉我。

谢谢

2 个答案:

答案 0 :(得分:1)

你的程序逻辑是倒退的,你正在做一堆你不需要的INSERT查询。正如@sean所说,改变顺序。

<?php

if(isset($_POST['submit'])){
    if(isset($_POST['categories'])){
        $cats= $_POST['categories'];

        // don't do an INSERT for each category, build the values and do only one INSERT query with multiple values

        $values = '';
        for($x = 0; $x < count($cats); $x++) {
            // add each value...
            $values .= "('".$_SESSION['user_id']."', '".$cats[$x]."'),";

        }
        // trim the trailing apostrophe and add the values to the query
        $qry_add_usrcats="INSERT INTO `user_categories` (`user_id_fk`, `category_id_fk`) VALUES ". rtrim($values,',');
        $result = mysqli_query($conn,$qry_add_usrcats); 
        echo "success";
    }
    elseif(!isset($_POST['categories'])){ //if nothing is selected delete all
        // you may want to put this query first, so if something is checked you delete all, so the db is clean and ready for the new data.
        // and if nothing is checked, you're still deleting....
        $qry_del_usrcats="DELETE FROM user_categories WHERE user_id_fk='".$_SESSION['user_id']."';"; //delete all query
        $result = mysqli_query($conn,$qry_del_usrcats);
    }
    unset($usr_cats);
    unset($cats);
}
?>

<form action="myprofile.php" method="post">
<?php $usr_cats=array();
$qry_usrcat="SELECT category_id_fk FROM user_categories WHERE user_id_fk='".$_SESSION['user_id']."';";
$result = mysqli_query($conn,$qry_usrcat);
while($row = mysqli_fetch_array($result)){
    $usr_cats[] = $row[0]; // getting user categories from db stored in array
}

$query_allcats="SELECT category_id,category_name, portal_name FROM categories INNER JOIN portals on categories.portal_id=portals.portal_id ORDER BY category_id;";  // select all category queries
$result = mysqli_query($conn,$query_allcats);
while($row = mysqli_fetch_array($result)){
    echo $row['portal_name'] . "<input "; //print categories
    if(in_array($row['category_id'], $usr_cats)){ // if in array from db, check the checkbox
        echo "checked ";
     }
    echo "type='checkbox' name='categories[]' value='";
    echo $row['category_id']."'> ". $row['category_name']."</br>\n\t\t\t\t\t\t";
}
?>
<input type="submit" name="submit" value="Submit"/>

答案 1 :(得分:1)

通常,这是由于脚本中查询的顺序而发生的。

如果您想在提交后显示更新后的结果,则应使updateinsert个查询成为有条件的,并让脚本自行调用。脚本的顺序很好,但您只需要执行以下操作:

采取此查询:

$qry_del_usrcats="DELETE FROM user_categories
            WHERE user_id_fk='".$_SESSION['user_id']."';"

并将其放在if语句中,所以看起来像这样:

if (isset($_POST['submit'] {

  $qry_del_usrcats="DELETE FROM user_categories
                    WHERE user_id_fk='".$_SESSION['user_id']."';"
$result = mysqli_query($conn,$qry_del_usrcats); 

[along with the other updates you have]
}

此外,您需要将整个条件移到表单本身之上;通常任何更新,插入或删除应该出现在表单的顶部,然后调用后面的选择(在条件之外)