ユーザーがサインアップするときに、一意の ID を割り当てて (またはデータベースに自動インクリメントさせて)、データベースに保存することをお勧めします。その後、ユーザーがログインするたびに、その user_id をデータベースから取得して、セッション変数に保存できます。シャウトが作成されると、シャウト自体と一緒にシャウトを作成した人の user_id をデータベースのシャウト テーブルに保存します。ユーザーがシャウトを削除しようとした場合、そのシャウトの削除を許可する前に、まずそのシャウトが自分のものであることを確認します。
例:
<?php
//when user logs in
$email = 'example@example.com';
$password = 'default';
$sql = "SELECT id FROM user_table WHERE email = '$email' AND password = '$password'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$_SESSION['user_id'] = $row['id'] //'id' is the user's id; assign it to the session variable
//user creates the shout
$user_id = $_SESSION['user_id']; //get the user_id from the logged-in user
$shout = $_POST['shout'];
$sql = "INSERT INTO shout_table (user_id, shout) VALUES ('$user_id','$shout')"; //store user id alongside the shout for future queries
mysql_query($sql);
//user about to delete the shout
$id = $_GET['id'];
$user_id = $_SESSION['user_id'];
//the sql to check in the shout_table to see if the shout they are deleting belongs to them
$sql = "SELECT * FROM shout_table WHERE user_id = '$user_id' AND id = '$id'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
if ($row)
{
//everything is alright; this user can delete the shout, so prepare the DELETE query to do so
}
else
{
//the user is not allowed to delete the shout because it's not theirs; tell them so with an echo or whatever you're using for error handling
}
?>
上記の例は、SQL インジェクションが蔓延しています。もちろん、検証してサニタイズします。同様に、mysql_query 関数は PHP 5.5 で非推奨になるため、代わりにmysqli_query 関数を使用してください。さらに良いことに、 PDOを使用できるかどうかを確認してください。:)