Hi:
I have one database with four tables in it. I want to delete from all four tables at the same time. I have tried many things and nothing is working right now. Please advise.
This is what I am trying to do (in one query)
$sql = "DELETE FROM logintest WHERE username = '$username";
$sql = "DELETE * FROM contactbooktest WHERE username = '$username'";
$sql = "DELETE * FROM contactbooktest WHERE contactuser = '$username'";
$sql = "DELETE * FROM sentusermessagestest WHERE frommbr = '$username'";
$sql = "DELETE * FROM sentusermessagestest WHERE tombr = '$username'";
$sql = "DELETE * FROM usermessagestest WHERE frommbr = '$username'";
$sql = "DELETE * FROM usermessagestest WHERE tombr = '$username'";
with this:
$connection = mysql_connect("$server", "$db_user", "$db_pass") or die(mysql_error());
$db = mysql_select_db("$database", $connection) or die(mysql_error());
$sql = "delete * from logintest, contactbooktest, sentusermessagestest, usermessagestest where logintest.username = '$username' and contactbooktest.username = '$username' and contactbooktest.contactuser = '$username' and sentusermessagestest.frommbr = '$username' and sentusermessagestest.tombr = '$username' and usermessagestest.frommbr = '$username' and usermessagestest.tombr = '$username'";
$result = mysql_query($sql, $connection);
echo "$username and its associated account information and messages have been permanently removed from the server.";
I get an error message as follows:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'where logintest.username = 'admin' and contactbooktest.username
When I echo $sql, I get the following:
delete from logintest, contactbooktest, sentusermessagestest, usermessagestest where logintest.username = 'admin' and contactbooktest.username = 'admin' and contactbooktest.contactuser = 'admin' and sentusermessagestest.frommbr = 'admin' and sentusermessagestest.tombr = 'admin' and usermessagestest.frommbr = 'admin' and usermessagestest.tombr = 'admin'
This is the complete code:
<?
$server = "localhost";
$db_user = "username";
$db_pass = "password";
$database = "database";
$connection = mysql_connect("$server", "$db_user", "$db_pass") or die(mysql_error());
$db = mysql_select_db("$database", $connection) or die(mysql_error());
$sql = "SELECT * FROM login WHERE username='$username' AND password='$password' AND password2='$password' AND emaila='$emaila'";
$result = mysql_query($sql, $connection);
if($username=='' OR $password=='' OR $password2=='' OR $emaila=='')
{
echo "All fields are required for security reasons.";
}
elseif($password != $password2)
{
echo "Passwords do not match.";
}
elseif (!eregi("^([a-z0-9]+)([._-]([a-z0-9]+))*[@]([a-z0-9]+)([._-]([a-z0-9]+))*[.]([a-z0-9]){2}([a-z0-9])?$", $emaila))
{
echo "Email address does not seem to be formatted correctly. Please rectify.";
}
elseif (!eregi("^[a-z0-9_]+$", $username))
{
echo "There are illegal characters in the username field. I cannot proceed.";
}
elseif (strlen($password) < 4)
{
echo "Braille School passwords are only a minimum of 4 characters.";
}
elseif (strlen($password) > 20)
{
echo "Braille School passwords are only a maximum of 20 characters long.";
}
elseif (strlen($username) > 15)
{
echo "Braille School usernames are only a maximum of 15 characters long.";
}
elseif (strlen($username) < 5)
{
echo "Braille School usernames are only a minimum of 6 characters long.";
}
//start deleting from tables
$connection = mysql_connect("$server", "$db_user", "$db_pass") or die(mysql_error());
$db = mysql_select_db("$database", $connection) or die(mysql_error());
$sql = "delete * from logintest, contactbooktest, sentusermessagestest, usermessagestest where logintest.username = '$username' and contactbooktest.username = '$username' and contactbooktest.contactuser = '$username' and sentusermessagestest.frommbr = '$username' and sentusermessagestest.tombr = '$username' and usermessagestest.frommbr = '$username' and usermessagestest.tombr = '$username'";
$result = mysql_query($sql, $connection) or die(mysql_error());
echo "$username and its associated account information and messages have been permanently removed from the server.";
mail("admin@brailleschool.com", "$username Terminated Account","Dear Paddy,\n$username used the Braille School form to terminate their account today. All settings were removed (login information, profile information, address book, as well as, inbox and sent messages contents.)\nCordially,
Blindness Related Learning.", "From: Braille School Accounts <accounts@brailleschool.com>");
?>