Still new to MySQL.
I have the following MySQL table named secrets
:
+----+--------+----------------+--------------------+
| id | userid | name | secret_ingredients |
+----+--------+----------------+--------------------+
| 72 | 1 | Nigella Lawson | Pixie dust |
| 99 | 2 | Delia Smith | Ground beatles |
| 32 | 3 | Anjum Anand | Minced fairies |
+----+--------+----------------+--------------------+
I also have an HTML table that lists all the secret ingredients for each logged in user based on their SESSIONID which is their DB userid
.
The table has a form button that posts the displayed row id
to a php file to show all the row data on a new (details) page.
I've used the following query in the php file to pull the data:
$id = $_POST["secret_id"];
$userid = $_SESSION['uid'];
{
$result = mysql_query("SELECT * FROM secrets WHERE id=" .$id );
$data = mysql_fetch_array($result);
$secretstuff = $data['secret_ingredients'];
$name = $data['name'];
}
The problem with the above is that a user can alter the 'secret_id'
value posted to the php file, and hence view another users secret ingredients.
How do I prevent this from happening.? How do I construct a query that fetches the ingredient id
only if the userid
matches the current SESSION ID
?
Should I fetch all records WHERE userid=" .$userid
and then fetch the web selected $id
variable from the fetched array?
Or am I going about this all wrong?