-6ren">
gpt4 book ai didi

php - 来自数据库的数据未显示在我的编辑页面中

转载 作者:可可西里 更新时间:2023-11-01 08:10:31 24 4
gpt4 key购买 nike

现在一切正常。我现在可以查看和更新​​我的数据库中的数据。谢谢你帮我。我已经更新了下面的代码。谢谢:)

edit_news.php//这是我的数据库中要显示的内容能够更新的地方

    <?php
mysql_connect("localhost", "root");
mysql_select_db("alumni");
if(isset($_GET['edit_id']))
{

$sql = "SELECT * FROM news WHERE id=".$_GET['edit_id'];
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

?>
<form action="/admin/update_news.php" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $row['id'] ?>"/>
<input type="text" name="title" value="<?php echo $row['title']; ?> "/>
<input type="text" name="body" value="<?php echo $row['body']; ?>"/>

<input type="submit" name="update" value="Save Changes"/>
<input type="submit" name="cancel" value="Cancel"/>

</div>
</form>

<?php
}
?>

update_news.php//这是更新函数发生的地方

    <?php 
date_default_timezone_set('Asia/Manila');
include_once('db.php');
if(isset($_POST['update']))
{
$title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$body = mysql_real_escape_string(htmlspecialchars($_POST['body']));

$sql = ("UPDATE news SET title='$title', body='$body' WHERE id=".$_POST['id']) or die(mysql_error());

if(mysql_query($sql))
{
echo "<script type='text/javascript'>alert('Changes saved!!');
window.location.assign('admin_profile.php');</script>";
}
else
{
echo "<script type='text/javascript'>alert('Error while updating data.');
window.location.assign('admin_profile.php');</script>";
}
}
if (isset($_POST['cancel']))
{
header("Location: admin_profile.php");
}
?>

//这是用户点击编辑链接的地方,这将引导他们到edit_news.php

<a href="/admin/edit_news.php?edit_id=<?php echo $row[0]; ?>">EDIT</a>

最佳答案

您将通过

在 edit_news.php 中获得 id
 $id = $_GET['id']

然后使用该 id 从数据库中获取数据,使用对变量 $title 和 $body 的选择查询

$sql = "SELECT * FROM news WHERE id = $id";

然后使用您通过使用任何 mysql_fetch 函数发出上述查询获得的值填充表单元素

关于php - 来自数据库的数据未显示在我的编辑页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308191/

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