So I'm currently playing around with PHP and making a "blog" system. I want to user to be able to edit the topic name of their own posts. Currently when you edit a topic name, all of the other topic names changes no matter which user made the post.
- Only edit the current topic name.
- Only the editor who made the post can edit that post.
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.";
}
?>
Thanks beforehand! //E