php - 在 mysql 中插入的复选框值

标签 php mysql checkbox

为了在我的代码中获取复选框的值,我现在苦苦挣扎了几天。

基本上我有一个管理页面,客户可以在其中选择和取消选择将放在网上的图像。

您可以选择和取消选择将显示在主页上并在图库页面上分开的图像。两者都勾选也是可以的。

我有另一个复选框,可以选中它以从列表中删除图像 (image_deleted)。 仍然有一个数据库条目,图像仍在文件系统上,但稍后我将创建一个清理作业。

这是我的代码:

    <?php

    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    ob_start();

    require('../../lib/dbconnection.php');
    require("../../lib/checklogin.php");
    require("includes/upload.inc.php");

    $query = 'SELECT * FROM gallery where image_deleted != 1 order by id desc';
    $result=$conn->query($query);
    $count=$result->num_rows;

    ?>      
    <!DOCTYPE html>
    <html>
        <head>
            <title>Classic Nails - CMS</title>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
            <meta name="description" content="ClassicNails">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="../css/screen.css">
            <link rel="stylesheet" href="../css/libs/magnific-popup.css"> 
            <script src="../js/libs/min/jquery-min.js" type="text/javascript"></script>
            <script src="../js/min/custom-min.js" type="text/javascript"></script>
            <script src="js/jquery.magnific-popup.js"></script> 
            <script>
                    $(document).ready(function() {
                    $('.image-link').magnificPopup({
                            type:'image',
                                    gallery:{
                                            enabled:true
                                    }
                            });
                    });
            </script>
        </head>
        <body>
            <?php include('includes/header.inc.php'); ?>
            <?php include('includes/nav.inc.php'); ?>
                <div class="wrapper">
                    <article class="content">
                    <h1>Foto gallery</h1>
                    <?php
                    if (isset($uploadResult)) {
                    echo "<p><strong>$uploadResult</strong></p>";
                    }
                    ?>
                    <form action="" method="post" enctype="multipart/form-data" name="uploadImage" id="uploadImage">
                        <p>
                                <label for="image">Upload image:</label>
                                <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
                                <input type="file" name="images" id="imagesd" /> 
                        </p>
                        <p>
                                <input type="submit" name="upload" id="upload" value="Upload" />
                        </p>
                    </form>
                        <div id="maincontent">
                            <h2>Foto informatie</h2>
                            <form name="FotoInformatie" id="fotoInformatie" method="post" action="">
                                <table>
                                    <tr>
                                        <td align="center"><strong>Foto<strong></td>
                                        <td align="center"><strong>Titel</strong></td>
                                        <td align="center"><strong>Beschrijving</strong></td>
                                        <td align="center"><strong>Homepage</strong></td>
                                    </tr>
                                    <?php
                                    while ($rows=$result->fetch_assoc()) {
                                    ?>
                                    <tr>
                                        <td class="hide" align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
                                        <td><a href="../img/uploads/<?php echo $rows['filename'];?>" class="image-link"><img src="../img/thumbs/<?php echo $rows['filename']; ?>"></a></td>
                                        <td align="center"><input name="title[]" type="text" id="title" value="<?php echo $rows['title']; ?>"></td>
                                        <td align="center"><input name="caption[]" type="text" id="caption" value="<?php echo $rows['caption']; ?>"></td>
                                        <td><input type="checkbox" name="checkboxHome[]" id="checkBoxHome" value="<?php echo ($rows['home'] == 1) ? 'checked="checked"' : ''; ?>"/></td>
                                    </tr>
                                    <?php
                                    }
                                    ?>
                                    <tr>
                                    <td colspan="4" align="center">
                                        <input type="submit" name="submit" value="Submit">
                                    </tr>
                                </table>
                            </form> 
                        </div>
                    </article> <!-- end of content -->
                </div> <!-- end of container -->
            <?php include('includes/footer.inc.php'); ?>
        </body>
    </html>

    <?php

    if(isset($_POST['submit'])) {
        $title = $_POST['title'];
        $caption = $_POST['caption'];
        if ($_POST['checkboxHome'] == "") {
            $checkboxHome[] = '0';
        } else {
            $checkboxHome[] = '1';
        }
        for($i=0;$i<$count;$i++){
            $result1=mysqli_query($conn, "UPDATE gallery SET title='$title[$i]', caption='$caption[$i]', home='$checkboxHome[$i]' WHERE id='$id[$i]'");
            header("location:/admin/foto-admin.php");
            }
    }
    ?>

该复选框仅适用于我的数据库中的第一行。当我选择另一条记录时,只会更新数据库中的第一条记录。

另一个问题是我的复选框不会被选中,所以根据我的屏幕我不知道图片是否在线。在数据库中,我看到 0 中的 1。

我知道 sql 注入(inject)是可能的,我必须准备语句,但这是我解决此复选框问题后的下一步。

希望有人能帮助我处理我的代码。这让我很头疼。

最佳答案

检查这些

Attribute name="id[]" for id field is not given. And it should get inside 
if(isset($_POST['submit'])) {
     $id = $_POST['id'];
    }

获取 Post 值时拼写错误

改变

$checkboxHome = $_POST['checkboxHome']; 
$checkboxFotoboek= $_POST['checkboxFotoboek']; 
$checkboxDelete = $_POST['image_deleted'];

$checkboxHome = $_POST['checkBoxHome']; 
$checkboxFotoboek= $_POST['checkBoxFotoboek'];
$checkboxDelete = $_POST['checkboxDelete'];

关于php - 在 mysql 中插入的复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24901954/

相关文章:

mysql - 如何获得 subject with and or 条件?

jquery - 从后面的代码加载时将复选框设置为选中状态

php - 有人能解释一下 Laravel 关系是如何运作的吗?

php - 语法错误,意外的 T_GOTO,期待 T_STRING

php - 使用 mysql 只插入一次行(在一个查询中)

php - 使用 PDO 从 MySQL 数据库获取信息

创建 SQL 表的 PHP 表单

php - 如何使用第一个结果在第二个中启动两个 sql 查询?

javascript - 如何在 Jquery 中的复选框上发布 id?

javascript - 根据复选框检查从另一个数组中删除数组