简体   繁体   中英

multiple checkbox check if value in database?

code:

<?php
    $id = $_GET['id'];
    $sql = "select * from admin_menu where id = '$id'";
    $result = mysqli_query($link,$sql);
    while ($row = mysqli_fetch_array($result)) 
    {
        $menu_name = $row['menu_name'];
        $menu_link = $row['menu_link'];
        $priority = $row['priority'];
        $admin_id = explode(",", $row['admin_id']);
    }
    if(isset($_POST['update']))
    {
        $admin_id = $_POST['admin_id'];
        $chk=""; 
        foreach($admin_id as $chk1)  
        {  
            $chk .= $chk1.",";  
        } 
        $menu_name = $_POST['menu_name'];
        $menu_link = $_POST['menu_link'];
        $priority = $_POST['priority'];
        $sql = "update admin_menu set menu_name = '$menu_name', menu_link = '$menu_link', priority = '$priority', admin_id = '$chk' where id = '$id'";
        $result = mysqli_query($link,$sql);
        if($result == true)
        {
            $msg .= "<h3 style='color:green;'>update</h3>";
        }
        else
        {
            $msg .= "<h3 style='color:red;'>Error!</h3>";   
        }
    }   
?>
<form name="myform" method="post" >
        <div class="row">
            <label for="Producer_firstname">Admin Name</label> 
            <?php
                foreach ($admin_id as $admin_id) 
                {
                    $chk = "";
                    if (in_array($chk, $admin_id)) 
                    {
                        $chk = 'checked="checked" ';
                    }
                    echo '<input type="checkbox" name="admin_id[]" value="'.$admin_id.'" '.$chk.'/><br/>';
                }
            ?>    
        </div>

        <div class="row">
            <label for="Producer_firstname">Menu Name </label>      
            <input size="60" maxlength="255" name="menu_name" id="menu_name" value="<?php echo $menu_name; ?>" type="text" />           
        </div>

        <div class="row">
            <label for="Producer_lastname" >Menu Link </label>      
            <input size="60" maxlength="255" name="menu_link" id="menu_link"  type="text" value="<?php echo $menu_link; ?>" />          
        </div>

        <div class="row">
            <label for="Producer_lastname" >Priority</label>        
            <select name="priority" id="priority">
                <option value="<?php echo $priority; ?>"><?php echo $priority; ?></option>
                <option value="">choose any one</option>
                <option value="1">1</option>
                <option value="0">0</option>
            </select>   
        </div>

        <div class="row buttons">
            <button type="submit" name='update' id='update'>update Menu</button>
        </div>
    </form>

In this code I am fetching multiple checkbox value from table admin2 and I want when I update form value checkbox check if the value of checkbox is exist into database. How can I fix it ?

Thank You

Your code has few issues,
1. Update should be done before select query
2. List of admin not managed separately
3. Priority radio buttons not managed properly

Additional Suggestions,
1. Use prepare query statements
2. use implode for appending multiple values instead of foreach
3. print admin names before checkboxes

<?php
    $id = $_GET['id'];
    if(isset($_POST['update']))
    {
        $chk = implode(',', $_POST['admin_id']);
        $menu_name = $_POST['menu_name'];
        $menu_link = $_POST['menu_link'];
        $priority = $_POST['priority'];
        $sql = "update admin_menu set menu_name = '$menu_name', menu_link = '$menu_link', priority = '$priority', admin_id = '$chk' where id = '$id'";
        $result = mysqli_query($link,$sql);
        $msg = "";
        if($result == true)
        {
            $msg .= "<h3 style='color:green;'>update</h3>";
        }
        else
        {
            $msg .= "<h3 style='color:red;'>Error!</h3>";   
        }
        echo $msg;
    }

    $sql = "select * from admin_menu where id = '$id'";
    $result = mysqli_query($link,$sql);
    $row = mysqli_fetch_array($result);
    $menu_name = $row['menu_name'];
    $menu_link = $row['menu_link'];
    $priority = $row['priority'];
    $admin_id = explode(",", $row['admin_id']);
    $admins = array('admin1', 'admin2', 'admin3', 'admin4', 'admin5', 'admin6', 'admin7', 'admin8');
?>

<form name="myform" method="post" >
    <div class="row">
        <label for="Producer_firstname">Admin Name</label>
        <?php
            foreach ($admins as $admin) 
            {
                $chk = "";
                if (in_array($admin, $admin_id)) 
                {
                    $chk = 'checked="checked" ';
                }
                echo $admin.' <input type="checkbox" name="admin_id[]" value="'.$admin.'" '.$chk.'/><br/>';
            }
        ?>    
    </div>

    <div class="row">
        <label for="Producer_firstname">Menu Name </label>      
        <input size="60" maxlength="255" name="menu_name" id="menu_name" value="<?php echo $menu_name; ?>" type="text" />           
    </div>

    <div class="row">
        <label for="Producer_lastname" >Menu Link </label>      
        <input size="60" maxlength="255" name="menu_link" id="menu_link"  type="text" value="<?php echo $menu_link; ?>" />          
    </div>

    <div class="row">
        <label for="Producer_lastname" >Priority</label>        
        <select name="priority" id="priority">
            <option value="1" <?php if($priority == 1) echo "selected='selected'"; ?>>1</option>
            <option value="0" <?php if($priority == 0) echo "selected='selected'"; ?>>0</option>
        </select>   
    </div>

    <div class="row buttons">
        <button type="submit" name='update' id='update'>update Menu</button>
    </div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM