gpt4 book ai didi

php - 试图以html形式显示数据库数据

转载 作者:行者123 更新时间:2023-11-30 21:27:30 25 4
gpt4 key购买 nike

我正在尝试将数据库中的数据显示为 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(); ?>

最佳答案

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

如果您正在使用输入字段

值=""

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

为什么会这样?

cat's eye is "beautiful"

成为

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

并包含在呈现的 HTML 中

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

关于php - 试图以html形式显示数据库数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58177041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com