尝试以html格式显示数据库数据

时间:2019-10-01 01:28:22

标签: php html mysql

我试图将数据库中的数据显示为html形式,以便用户可以编辑信息并将其更新为wordpress中的数据表。问题是,当我尝试加载页面时,所有html都消失了。问题似乎是进入和退出php。我尝试将两个}}标记移到表单顶部之后,并且html将显示,但没有数据将插入到文本框中。 这是我的代码。

<?php get_header('settings'); ?>
<?php


$loc_id = get_query_var('id');

update_location($loc_id);


function update_location($loc_id){

    loc_update($loc_id);

    global $wpdb;
    $table_name= $wpdb-> prefix. 'dbp_tb_locations';

    if(isset($_POST['delete'] ) ){
        $wpdb->delete($table_name,
                      array(
                            'id' => $loc_id
                      )
        );    

        wp_redirect(site_url('/ts-locations-list/'));
        exit;
        }

    $DBP_result = $wpdb -> get_results("SELECT * FROM $table_name WHERE id= $loc_id");
        foreach( $DBP_result as $DBP_cols){
            $id = $DBP_cols -> id;
            $name = $DBP_cols -> location_name;
            $abbreviation = $DBP_cols -> abbreviation;
            $directions = $DBP_cols -> directions;


 ?>

<div class="container p-3">
 <h3>Add / Edit Location</h3>
    <div class="card">

        <div class="card-body">
            <div class="container">
                <form action="" method="post">
                    <div class="row background">
                        <div class="col-4 outline">
                            <div class="row">
                                <div class="col-12 text-right background p-2">
                                <label class="pt-1">Name: </label>
                                </div>
                            </div>
                        </div>
                        <div class="col-8 outline">
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group pt-2">
                                        <input class="form-control" type="text"  name="location_name" value="<?php echo $name; ?>"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row background">
                        <div class="col-4 outline">
                            <div class="row">
                                <div class="col-12 text-right background p-2">
                                <label class="pt-1">Abbreviation: </label>
                                </div>
                            </div>
                        </div>
                        <div class="col-8 outline">
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group pt-2">
                                        <input class="form-control" type="text" name="abbreviation" value="<?php echo $abbreviation; ?>" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row background">
                        <div class="col-4 outline">
                            <div class="row">
                                <div class="col-12 text-right background p-2">
                                <label class="pt-1">Directions: </label>
                                </div>
                            </div>
                        </div>
                        <div class="col-8 outline">
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group pt-2">
                                        <textarea class="form-control" name="Directions" rows="10" cols="75" ><?php echo $directions ?></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row background">
                        <div class="col-4 outline">
                            <div class="row">
                                <div class="col-12 text-right background p-2">
                                <label class="pt-1">Options:</label>
                                </div>
                            </div>
                        </div>
                        <div class="col-8 outline">
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-check pt-2 pl-3">
                                        <input class="form-check-input" type="checkbox" value="" id="customCheck">
                                        <label class="form-check-label" for="customCheck">Make this location my default selection when creating classes</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row background outline">
                        <div class="col my-3 text-center">                                                         
                            <a class="btn btn-primary text-center" href="/index.php/ts-locations-list">Back       </a>
                            <button type="submit" name="submit" class="btn btn-primary text-center">Update Location</button>
                            <button type="submit" name="delete" class="btn btn-primary text-center">Delete Location</button>
                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>

<?php

      }
}

function loc_update($loc_id){

    global $wpdb;
    $table_name= $wpdb-> prefix. 'dbp_tb_locations';

    $name = $_POST['location_name'];
    $abbreviation = $_POST['abbreviation'];
    $directions = $_POST['Directions'];

    if ( isset($_POST['submit'] ) ) {
        $wpdb->update($table_name,
                     array(
                        'location_name' => $name,
                        'abbreviation' => $abbreviation,
                        'directions' => $directions
                     ),
                    array(
                        'id' => $loc_id
                    ),
                    array(
                        '%s',
                        '%s',
                        '%s'
                    )


        );
     wp_redirect(site_url('/ts-locations-list/'));
        exit;
    }
}
?> 

<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:0)

在FORMS上显示数据时必须小心。

如果您正在使用INPUT FIELD

value =“”

您必须使用PHP的htmlentities()函数包含引号并成为字段内的一部分。如果数据上有\ n或“下一行”,则必须替换它,因为输入字段是一个衬里。

为什么行得通?

cat's eye is "beautiful"

成为

cat's eye is &quot;beautiful&quot;

并包含在呈现为HTML的HTML中

VALUE="cat's eye is &quot;beautiful&quot;"