John
John

Reputation: 4944

Deleting a row from a MySQL database

I have a MySQL table called "privatemessage" with a primary key field called "messageid."

I would like to have a PHP query that deletes any row from "privatemessage" where "messageid = $messageid."

What sort of query does this?

Thanks in advance,

John

Upvotes: 1

Views: 162

Answers (3)

SlavaNov
SlavaNov

Reputation: 2485

$query = "DELETE FROM `privatemessage` WHERE `messageid` = '$messageid'";
mysql_query($query);

But first of all, check that $messageid is a number (See SQL Injection).

Read more: about MySQL delete

Upvotes: 1

Martin
Martin

Reputation: 425

You might want to use a preprared statement, so you're on the safe side regarding SQL-injection...

$conn = new mysqli($host, $user, $password, $database);
if ($conn->connect_error) {
    die('could not connet to database.');
}
$statement = $conn->prepare("DELETE FROM privatemessage WHERE messageid = ?");
$statement->bind_param("messageid", $messageid);
$statement->execute();

Upvotes: 2

BoltClock
BoltClock

Reputation: 724472

DELETE FROM `privatemessage` WHERE `messageid` = $messageid

That's just the raw SQL query. Things like validating and escaping user input should certainly be considered.

For example, given messageid as an AUTO_INCREMENT integer primary key I'd simply run this in PHP (converting $messageid to an int using intval()):

mysql_query('DELETE FROM `privatemessage` WHERE `messageid` = ' . intval($messageid));

Upvotes: 4

Related Questions