Schmoopy
01-14-2009, 01:16 AM
Hi, I've currently got 2 fields (email and id) in a table (subscriptions), and the email field is the primary key.
To unsubscribe to a newsletter, the user needs to enter a valid email, but the id also has to correspond to that email, ensuring that not anyone can delete someone else off the database.
The code for the subscribing part is the important bit here:
mysql_select_db("bristoldnb_mail",$con);
$result = mysql_query("SELECT * FROM subscribers");
while($row = mysql_fetch_array($result))
{
if ($row['Email'] == $email)
{
header("Location: $server_url" . "php/index.php?pass=taken");
die();
}
elseif ($row['ID'] == $id)
{
while ($row['ID'] == $id)
{
$id = mt_rand(1000,100000000);
}
}
}
mysql_query("INSERT INTO subscribers (Email, ID)
VALUES ('$email','$id')");
I am not sure how to accomplish this, but what I want to happen in the "elseif ($row['ID'] == $id)" part is for the script to keep generating a random number until it is not equal to any other IDs in the table. This would be much easier if I could make two primary keys, but I can't - anyone know how to achieve such an effect?
Thanks,
Jack.
To unsubscribe to a newsletter, the user needs to enter a valid email, but the id also has to correspond to that email, ensuring that not anyone can delete someone else off the database.
The code for the subscribing part is the important bit here:
mysql_select_db("bristoldnb_mail",$con);
$result = mysql_query("SELECT * FROM subscribers");
while($row = mysql_fetch_array($result))
{
if ($row['Email'] == $email)
{
header("Location: $server_url" . "php/index.php?pass=taken");
die();
}
elseif ($row['ID'] == $id)
{
while ($row['ID'] == $id)
{
$id = mt_rand(1000,100000000);
}
}
}
mysql_query("INSERT INTO subscribers (Email, ID)
VALUES ('$email','$id')");
I am not sure how to accomplish this, but what I want to happen in the "elseif ($row['ID'] == $id)" part is for the script to keep generating a random number until it is not equal to any other IDs in the table. This would be much easier if I could make two primary keys, but I can't - anyone know how to achieve such an effect?
Thanks,
Jack.