/    Sign up×
Community /Pin to ProfileBookmark

Delete from database

I’m doing a sort of blogpost/chat site and ran in to a problem where I want to delete a certain post.
And the requirements is that only the user who posted it is able to delete it. Problem is that when I press the “Delete post” button from blogpost.php, it doesnt get deleted and the echo “Error.” gets hit. Can anyone help me understand why?

[I]blogpost.php[/I] – Shows the blogpost in its whole.
$id = intval($_GET[“id”]);
$query = “SELECT * FROM posts WHERE id=$id;”;
$result = mysqli_query($db, $query) or die(‘SQL error’);

//Variables
$row = mysqli_fetch_array($result);
$name = $row[‘name’];
$message = $row[‘message’];
$date = $row[‘date’];

//Echo on screen
echo “<p class=’postmessage’>”;
echo “<span class=’boldtext’>Name:</span> $name – <span class=’boldtext’>Date:</span> $date”;
echo “<br><span class=’boldtext’>Message:</span> ” . $message;
echo “</p>”;
echo “<a class=’submitbutton’ href=’deletepost.php?id=$id’>Delete post</a>”;

//Close database
mysqli_close($db);

[I]deletepost[/I].php – So the idea here is that when the user clicks the “Delete post” button it will take him to the deletepost.php file with the posts id. “deletepost.php?id=61” for example.

if(!isset($_SESSION[’email’])){
echo “<p><b>You have to be logged in to delete this.”;
}

if(isset($_SESSION[’email’]))
{
$name = $_
SESSION[’email’];
$id = intval($_GET[“id”]);

$check = mysql_query(“SELECT * FROM posts WHERE id = $id AND name = ‘$name'”);
if($check){
$query1=mysql_query(“delete FROM posts WHERE id = $id AND name=’$name'”);
echo “<p>Post has been deleted. <a href=’index.php’>Home.</a>”;
}
else{
echo “<p><b>Error.”;
}

}

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@phpmillionNov 09.2017 — First of all, I would recommend rewriting your code. You are using MySQL extension, which is deprecated, which means your code will NOT work on current PHP version (it will continue working on extremely old versions of PHP only). You should switch from MySQL to MySQLi or PDO as soon as possible.

Second, what exactly does your query display (print) when you debug it? You did a debug, right?
Copy linkTweet thisAlerts:
@rootNov 09.2017 — When you post any code, please use the forum BB Code tags to wrap around code blocks you post, this has several advantages in making it easier to read, format remains as posted and some tags have syntax highlighting to improve readability.
Copy linkTweet thisAlerts:
@rootNov 09.2017 — also, instead of testing if the item exists, just go for a straight forward delete and then check if any error occurred and if it does, you can always test what the error was and deal with it as you need or not...
Copy linkTweet thisAlerts:
@NogDogNov 09.2017 — Some of your code is using the mysql[B][COLOR="#FF0000"]i[/COLOR][/B]_*() functions, but the code you said is not working is using the deprecated mysql_*() functions. Maybe that's the problem.
Copy linkTweet thisAlerts:
@NifelauthorNov 09.2017 — Solved it! Thank you all for the help!

Im fairly new to PHP so I learned something new today about mysql/mysqli ?

if(!isset($_SESSION['email'])){

echo "<p>Log in to delete post.</p>";

}

if(isset($_SESSION['email']))

{

$name = $_
SESSION['email'];

$id = intval($_GET['id']);

$sql = "DELETE FROM posts WHERE id = $id AND name = '$name'";
$result = mysqli_query($db, $sql) or die('Fel vid SQL-fråga');

if($result) {
echo "<p>Post has been deleted. <a href='index.php'>Home.</a>";

}

else{

echo "<p>Error.</p>";

}

}
Copy linkTweet thisAlerts:
@rootNov 09.2017 — Please remember to use forum BB Code tags (see my signature for examples) when posting code blocks.
Copy linkTweet thisAlerts:
@NifelauthorNov 09.2017 — Mea culpa, is there anyway to edit posts?
Copy linkTweet thisAlerts:
@TrainNov 09.2017 — Mea culpa, is there anyway to edit posts?[/QUOTE]
Not after a given time period. Which I do not know.
×

Success!

Help @Nifel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...