我正在尝试更新或编辑数据库中的数据,但我不希望编辑页面位于另一个页面中,我只是希望它位于我可以查看不同新闻用户已添加。但是当我点击编辑链接时,表单就不会显示。
帮助我找到错误或缺失的内容?
这是 edit_news.php
<?php
date_default_timezone_set('Asia/Manila');
include_once('db.php');
if($isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['edit'])) {
$title = $_POST['title'];
$body = $_POST['body'];
$date = date('Y-m-d H:i:s');
$title = mysql_real_escape_string($title);
$body = mysql_real_escape_string($body);
$servername = "localhost";
$username="root";
$password = "";
$database = "zchs_alumni";
$connection = new mysqli($servername, $username, $password, $database);
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$sql = ("UPDATE news SET title = '$title', body = '$body', name = '$name', date = '$date' WHERE id='$id'")or die();
mysql_query($sql);
echo "<script type='text/javascript'>alert('Changes saved!'); window.location.assign('/zchs-alumni/news.php');</script>";
}
}?>
<?php
if($isset($_GET['id']))
{
$id=$_GET['id'];
$query=mysql_query("SELECT * FROM news WHERE id='$id'");
while($row = mysql_fetch_array($query)) {
$title=$row['title'];
$body=$row['body'];
?>
<form action="" method="post">
<p>
<label for="title" id="title">Title</label>
<input type="text" name="title" value="<?php echo $row['title']; ?>"/>
</p><br/>
<p>
<label for="body" id="body">Body</label>
<input type="text" name="body" value="<?php echo $row['body']; ?>"/>
</p><br/>
<p>
<input type="submit" name="update" value="Save Changes" style="float: right"/>
</p>
</form>
<?php
} }?>
这是我的news.php,其中显示新闻以及我想要编辑数据的位置。
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("zchs_alumni") or die(mysql_error());
$query = mysql_query("SELECT * FROM news ORDER BY date DESC LIMIT $start, $limit");
while($row = mysql_fetch_array($query)) {
?>
<p> <span><h3><?php echo $row['title']; ?></h3></span></p>
<p> <span><?php
$img = $row['photo'];
if($img != ""){
$image = 'news/'.$img;
echo '<center><img src="'.$image.'" width="750" height="350" alt=""></center>';
}
?></span></p>
<br/>
<p> <span><?php echo $row['body']; ?></span></p>
<br/>
<p> <span><h6>Posted at
<?php
$row_date = strtotime($row['date']);
echo date("F j, Y, g:i a", $row_date);
?></h6></span></p>
<br/>
<p><span><a href="edit_news.php?id=<?php echo $row['id']; ?>"><span class="edit" title="Edit">EDIT</span></a></p>
<?php
}
?>
答案 0 :(得分:-1)
我见过这些人这样做。简单检查是否定义了post变量if(isset($ _ POST))如果是,那么它是更新提交,如果没有则显示表单!