PHP - 更新/编辑数据库表中的数据/记录

时间:2017-08-17 11:28:13

标签: php html mysql input

所以我现在正在玩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( 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.";
}

  ?>

edit.php

class DistrictSerializer(serializers.ModelSerializer):
    class Meta:
        model = District
       fields = ('id', 'name')

class CitySerializer(serializers.ModelSerializer): 
    districts = DistrictSerializer(many=True, read_only=True)

    class Meta:
        model = City
        fields = ('id', 'name', 'districts')

class CityForProvinceSerializer(serializers.ModelSerializer): 

    class Meta:
        model = City
        fields = ('id', 'name')

class ProvinceSerializer(serializers.ModelSerializer):
    cities = CityForProvinceSerializer(many=True, read_only=True)

    class Meta:
        model = Province
        fields = ('id', 'name', 'cities')

事先谢谢! //ë

4 个答案:

答案 0 :(得分:2)

$sql     = "UPDATE topics SET topic_name='$newTn' where topic_id = '".$_GET['edit]."'"; 

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

答案 1 :(得分:2)

在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 :(得分:1)

您需要在查询UPDATE:

中指定主题的ID
 $sql     = "UPDATE topics SET topic_name='$newTn' where id=$session[yourtopicID]" ;

答案 3 :(得分:0)

edit.phpUPDATE topics SET topic_name='$newTn'查询更改为

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