使用模态Bootstrap更新查询获取未定义的索引

时间:2016-06-09 06:25:18

标签: php mysql twitter-bootstrap undefined-index

我正在尝试完成我的新个人CMS(内容管理系统),我编写了一个部分,其中包括使用PHP,MySql和bootstrap Modal在一个页面中插入,选择和删除项目。 显然我有更新查询的麻烦。这个过程是当我点击编辑按钮,模态Bootstrap将显示,然后关联数据将通过value属性检索形式Mysql。之后,我改变它们,然后单击提交按钮。问题出在这里!当流程完成时,我的$ _POST [' input_result']会出现错误。 var_dump得到NULL,最后我有未定义的索引。 有没有人弄清楚这个问题???谢谢你们。 这是我用PHP的模态Bootstrap代码:

<?php
    $selectForUpdate = "SELECT * FROM ring WHERE id='4'";
    $resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate);

    $first = $second = $third= '';
    $first = $_POST['u_ringCode'];
    $second = $_POST['u_ringWeight'];
    $third = $_POST['u_ringComment'];

    $updateQuery = "UPDATE ring SET ring_code='".$first."', ring_weight='".$second."', ring_comment='".$third."' WHERE id='4'";
    $resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery);
    while ( $showUpdateRows = mysqli_fetch_assoc( $resultSelectForUpdate ) ) {
?>
    <!-- Update Modal -->
    <div class="modal fade bs-example-modal-sm" id="update-4" tabindex="-1" role="dialog" aria-labelledby="update-4-label" >
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h5 class="modal-title text-center" id="update-4-label" style="color:#ddd;" >
                        Edit Code
                    </h5>
                </div>
                <div class="modal-body">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <form method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                            <div class="form-group">
                                <label for="">Ring Code :</label>
                                <input type="text" class="form-control" id="" name="u_ringCode" placeholder="" value="<?=$showUpdateRows['ring_code']?>">
                            </div>
                            <div class="form-group">
                                <label for="">Ring Weight :</label>
                                <input type="text" class="form-control" id="" name="u_ringWeight" placeholder="" value="<?=$showUpdateRows['ring_weight']?>">
                            </div>
                            <div class="form-group">
                                <label for="">Comment :</label>
                                <input type="text" class="form-control" id="" name="u_ringComment" placeholder="" value="<?=$showUpdateRows['ring_comment']?>">
                            </div>
                        </form>
                    </div>
                </div>
                <div class="modal-footer">
                    <a type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
                    <a type="submit" class="btn btn-default pull-left" name="ringDelete" style="color:black;" href="?update=4">Submit</a>
                </div>
            </div>
        </div>
    </div>
<?php
    }
?>

2 个答案:

答案 0 :(得分:0)

在表单提交之前,您的$ _POST变量为空。你需要检查一下。此外,在不转义字符串的情况下进行数据库查询也不是安全的方法。您可以通过搜索MySQL注入来阅读更多相关信息。第三件事是在更新查询之前获取项目。在这种情况下,您将在页面上看到未更改的数据。只有在页面刷新后,它才会显示新值。 代码的第一部分可能如下所示:

<?php
if(!empty($_POST['u_ringCode'])&&!empty($_POST['u_ringWeight'])&&!empty($_POST['u_ringComment']))
{
    $first = $connect_to_db->escape_string($_POST['u_ringCode']);
    $second = $connect_to_db->escape_string($_POST['u_ringWeight']);
    $third = $connect_to_db->escape_string($_POST['u_ringComment']);
    $updateQuery = "UPDATE ring SET ring_code='$first', ring_weight='$second', ring_comment='$third' WHERE id='4'";
    $resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery);
}
$selectForUpdate = "SELECT * FROM ring WHERE id='4'";
$resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate);


while ( $showUpdateRows = mysqli_fetch_assoc( $resultSelectForUpdate ) ) {
    ?>

答案 1 :(得分:0)

<?php
    /*Code Start For Connnect Database*/
        $connect_to_db=mysqli_connect("localhost", "root", "", "db_name" );
    /*Code End For Connnect Database*/
    /*Code Start For Update Modal Form*/
        if (isset($_REQUEST['ringUpdate'])) {
            $first = $_POST['u_ringCode'];
            $second = $_POST['u_ringWeight'];
            $third = $_POST['u_ringComment'];
            $updateQuery = "UPDATE ring SET ring_code='".$first."', ring_weight='".$second."', ring_comment='".$third."' WHERE id='4'";
            $resultUpdateQuery = mysqli_query($connect_to_db, $updateQuery);
        }
    /*Code End For Update Modal Form*/
        $selectForUpdate = "SELECT * FROM ring WHERE id='4'";
        $resultSelectForUpdate = mysqli_query($connect_to_db, $selectForUpdate);
            while ( $showUpdateRows = mysqli_fetch_assoc( $resultSelectForUpdate ) ) {

?>
    <!-- Update Modal -->
    <div class="modal fade bs-example-modal-sm" id="update-4" tabindex="-1" role="dialog" aria-labelledby="update-4-label" >
        <div class="modal-dialog modal-sm" role="document">
           <div class="modal-content">
                <form method="post" action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h5 class="modal-title text-center" id="update-4-label" style="color:#ddd;" >
                    Edit Code
                </h5>
            </div>
            <div class="modal-body">
                <div class="col-md-12 col-sm-12 col-xs-12">
                        <div class="form-group">
                            <label for="">Ring Code :</label>
                            <input type="text" class="form-control" id="" name="u_ringCode" placeholder="" value="<?=$showUpdateRows['ring_code']?>">
                        </div>
                        <div class="form-group">
                            <label for="">Ring Weight :</label>
                            <input type="text" class="form-control" id="" name="u_ringWeight" placeholder="" value="<?=$showUpdateRows['ring_weight']?>">
                        </div>
                        <div class="form-group">
                            <label for="">Comment :</label>
                            <input type="text" class="form-control" id="" name="u_ringComment" placeholder="" value="<?=$showUpdateRows['ring_comment']?>">
                        </div>
                 </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Cancel</button>
                <input type="submit" class="btn btn-default pull-left" name="ringUpdate" style="color:black;" value="submit">
            </div>
            </form>
        </div>
    </div>
</div>
  <?php
}
?>