【问题标题】:PHP - Updating / Editing Data / Records In The Database Table [duplicate]PHP - 更新/编辑数据库表中的数据/记录[重复]
【发布时间】:2017-08-17 11:28:12
【问题描述】:

所以我目前正在使用 PHP 并制作一个“博客”系统。我希望用户能够编辑自己帖子的主题名称。目前,当您编辑主题名称时,无论哪个用户发帖,所有其他主题名称都会更改。

  • 仅编辑当前主题名称。
  • 只有发布帖子的编辑才能编辑该帖子。

topic.php

<?php 

session_start();
require('connect.php');
if (@$_SESSION["username"]) {

 ?>

 <!DOCTYPE html>
 <html>
 <head>
    <title>Home page</title>
 </head>
 <body>
<?php include('header.php'); ?>

<center>

<?php 
    if (@$_GET['id']) {
        $check = mysql_query("SELECT * FROM topics WHERE topic_id='".$_GET['id']."'");

        if (mysql_num_rows($check) > 0) {
            while ($row = mysql_fetch_assoc($check)) {
                $check_u = mysql_query("SELECT * FROM users WHERE username='".$row['topic_creator']."'");
                while ($row_u = mysql_fetch_assoc($check_u)) {
                    $user_id = $row_u['id'];
                }

                echo "<h1>".$row['topic_name']."</h1>";
                echo "<h5>By <a href='profile.php?id=$user_id'>".$row['topic_creator']."</a><br />Date: ".$row['date']."</h5>";
                echo "<br />".$row['topic_content'];
                echo "<br /><br /><img src='img/".$row['image']."' width='300' />";
                echo "<br /><br /><a href='edit.php?edit=".$row['topic_id']."'>Edit</a>";
            }

        }else {
            echo "Topic not found.";
        }
    }


?>

</center>

 </body>
</html>

 <?php 

}else {
    echo "You must be logged in.";
}

?>

edit.php

<?php 

session_start();
require('connect.php');
if (@$_SESSION["username"]) {

 ?>

 <!DOCTYPE html>
 <html>
 <head>
    <title>Home page</title>
 </head>
 <body>
<?php include('header.php'); ?>

<center>

<?php 

if( isset($_GET['edit']) )
    {
        $id = $_GET['edit'];
        $res= mysql_query("SELECT * FROM topics");
        $row= mysql_fetch_assoc($res);
    }

    if( isset($_POST['newTn']) )
    {
        $newTn = $_POST['newTn'];
        // $id       = $_POST['id'];
        $sql     = "UPDATE topics SET topic_name='$newTn'";
        $res     = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=index.php'>";
    }

?>

<form action="edit.php" method="POST">
Name: <input type="text" name="newTn" value=<?php echo $row['topic_name']; ?>><br />
<input type="hidden" name="id" value="">
<input type="submit" value=" Update "/>
</form>

</center>

</body>
</html>

<?php


if (@$_GET['action'] == "logout")   {
    session_destroy();
    header("Location: login.php");
}

}else {
    echo "You must be logged in.";
}

  ?>

先谢谢了! //E

【问题讨论】:

  • 在 UPDATE 语句中使用 WHERE 子句 (WHERE topic_id =?);并使用占位符 (?),不要将值插入 SQL 语句中。另外,不要使用 msql_ 系列函数,而是使用 mysqli_* 或 PDO。
  • 这段代码不应该在实际环境中使用,我希望你意识到这一点,并且有很多巨大的 sql 注入漏洞。如果在实时环境中使用,您的数据库可能会被黑客入侵,并可能丢失您的所有信息,和/或您的用户的重要信息被盗。
  • 如果您正在使用 php,请考虑使用 php 框架,例如 codeigniter.comlaravel.com 。两者都带有内置查询构建器和安全功能来帮助您。
  • 您还使用了 PHP 7.0 不再支持的已弃用 api。如果您的服务器升级到该版本的 PHP,您将需要完全重写。保持领先,停止你正在做的事情,现在使用准备好的语句重写它。
  • 这个问题重复的答案太多了,真的没必要。

标签: php html mysql input


【解决方案1】:

在edit.php中

您需要在查询中指定要编辑的帖子ID。

if( isset($_POST['newTn']) )
    {
        $newTn = $_POST['newTn'];
        $id       = $_POST['id'];
        //notice here the $id is added as where clause to filter the edit on one row only
        $sql     = "UPDATE topics SET topic_name='$newTn' WHERE post_id = '$id'";
        $res     = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=index.php'>";
    }

【讨论】:

    【解决方案2】:
    $sql     = "UPDATE topics SET topic_name='$newTn' where topic_id = '".$_GET['edit]."'"; 
    

    您已从 Grid 传递了主题 ID,您需要将其附加到查询中

    【讨论】:

      【解决方案3】:

      你需要在查询UPDATE中指定topic的id:

       $sql     = "UPDATE topics SET topic_name='$newTn' where id=$session[yourtopicID]" ;
      

      【讨论】:

        【解决方案4】:

        edit.php 更改UPDATE topics SET topic_name='$newTn' 查询到下面

        UPDATE topics SET topic_name = '$newTn' where `yourTopicId` = '$_GET[edit]'
        

        【讨论】:

          猜你喜欢
          • 2020-12-12
          • 2018-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-20
          • 2021-09-14
          • 1970-01-01
          相关资源
          最近更新 更多