【问题标题】:The edit form in the same page won't show up when I click the EDIT link当我单击编辑链接时,同一页面中的编辑表单不会显示
【发布时间】:2016-05-16 10:24:18
【问题描述】:

我正在尝试更新或编辑数据库中的数据,但我不希望编辑页面位于另一个页面中,我只是希望它位于同一页面中,以便我可以查看不同的 新闻 用户添加的。但是当我单击 编辑链接 时,表单不会显示。

帮我找出问题或遗漏的地方?

这是我的 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
}
 ?>

【问题讨论】:

  • 请不要使用 mysql_ 数据库扩展,它已被弃用(在 PHP7 中永远消失了)尤其是如果您只是学习 PHP,请花精力学习 PDOmysqli_ 数据库扩展, and here is some help to decide which to use
  • 我正在尝试更新或编辑数据库中的数据,但我不希望编辑页面位于另一个页面中 那么您将不得不学习如何给我们 AJAX。这段代码不尝试使用 AJAX,所以你离你的目标还有很长的路要走。我能否提醒您,SO 不是免费的编码或教程服务您必须表明您已经为解决自己的问题付出了一些努力。
  • 我只是在学习 php,这就是为什么我问我是否错过了什么,因为我认为我没有错过,因为代码没有错误。我尝试解决它几个小时,但由于我不能在这里寻求帮助。我不知道 AJAX,所以我想用 php 来做,既然不可能,那么我会尝试使用 AJAX。感谢你! @RiggsFolly

标签: php mysql sql web sql-update


【解决方案1】:

我见过这些人这样做。简单检查是否定义了 post 变量 if(isset($_POST)) ,如果是,则它是更新提交,如果没有,则显示表单!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多