2017-08-17 125 views
-2

所以我目前正在使用PHP并制作一个“博客”系统。我想让用户能够编辑他们自己帖子的主题名称。目前,当您编辑主题名称时,无论哪个用户发布帖子,所有其他主题名称都会更改。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."; 
} 

    ?> 

感谢事先! //è

+1

在UPDATE语句中使用WHERE子句(WHERE'= topic_id'?);并使用占位符('?'),不要将值插入到SQL语句中。另外,不要使用'msql_'系列函数,而是使用'mysqli_ *'或PDO。 –

+2

这段代码不应该在实时环境中使用,我希望你能意识到这一点,并且有相当多的sql注入漏洞。如果在实时环境中使用,则可能会导致数据库被黑客入侵,并可能丢失所有信息,并且/或者您的用户的重要信息被盗。 –

+0

如果你正在玩PHP看看使用PHP的框架,如https://www.codeigniter.com/或https://laravel.com/。它们都内置了查询构建器和安全功能来帮助你。 –

回答

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

您已通过从电网主题ID,你需要附上查询

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'>"; 
    } 
1

你需要指定主题的ID查询UPDATE:

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

你需要这样做

$check = mysql_query("SELECT * FROM topics WHERE id='.$id.'"); 

,而不是这个

$check = mysql_query("SELECT * FROM topics WHERE topic_id='".$_GET['id']."'"); 
0

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

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