...

Deleting records from two tables

PRodgers4284
03-16-2008, 02:20 PM
I want to delete records from two tables, im using the following query:

mysql_query("DELETE FROM employers && job WHERE username='$username'");


Not sure if this is the correct way of doin this, can anyone help?

Lee Stevens
03-16-2008, 02:35 PM
mysql_query("DELETE FROM employers e, job j WHERE e.username='".$username."' AND j.username='".$username."'")

PRodgers4284
03-16-2008, 03:26 PM
mysql_query("DELETE FROM employers e, job j WHERE e.username='".$username."' AND j.username='".$username."'")



Hey thanks for the reply, i updated my code with the above query, but it still doesnt delete the records from the database:

Here is the basic php code im using:

<?php
$username = $_GET['username'];

if (isset($_POST['submit'])) {


mysql_query("DELETE FROM employers e, job j WHERE e.username='".$username."' AND j.username='".$username."'")
?>

<br />
<a href="Indexadmin.php">Back to main page</a>
<br />
<br />
<br />
The Employer account and all jobs for <b><?php echo $username; ?></b> has been deleted from the system.
<?php
}
else
{

$account = mysql_fetch_array(mysql_query("SELECT * FROM employers WHERE username='$username'"));

?>

Lee Stevens
03-16-2008, 03:29 PM
Are you posting or getting?

Change $_GET['username'] to $_POST['username']

abduraooft
03-16-2008, 03:32 PM
DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;

Or:

DELETE FROM t1, t2 USING t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id; Do you have any reason to omit a die(mysql_error()) after your mysql_query() ?

PRodgers4284
03-16-2008, 03:52 PM
Are you posting or getting?

Change $_GET['username'] to $_POST['username']

Im getting the username, im using link below from the previous page:

<td class="Body"><?php echo "<a href='admindeleteemployer.php?username=$username'>Delete Account</a>"?></td>


Then i get the username in the delete page.

Digicoder
03-16-2008, 06:33 PM
Try using this one, tell me if it didn't work. If your query is bad, you should get some error reporting from the first one, if so then let me know what the error is and I'll help you out with it.

<?php
function escape($sql) //proper string escaping method, some hosts enable magic_quotes others don't safe to check first.
{
if (get_magic_quotes_gpc() == 1)
{
$sql = stripslashes($sql);
}

$sql = mysql_real_escape_string($sql);
return $sql;
}

if(isset($_GET['username']) && !empty($_GET['username']))
$username = escape($_GET['username']);
else
$username = '';

if(isset($_POST['submit']) && !empty($username))
{
//It is easier to just split the one query into two. It's not "simultaneous " but thats only by a few milliseconds.
mysql_query('DELETE FROM employers WHERE username="'.$username.'"') or
die('Error #: ' . mysql_errno() . '<br />SQL Stated: ' . mysql_error());

mysql_query('DELETE FROM job WHERE username="'.$username.'"') or
die('Error #: ' . mysql_errno() . '<br />SQL Stated: ' . mysql_error());
?>

<br />
<a href="Indexadmin.php">Back to main page</a>
<br />
<br />
<br />
The Employer account and all jobs for <b><?php echo $username; ?></b> has been deleted from the system.
<?php
}
else
{
if(isset($_GET['check']))
{
$account = mysql_query("SELECT * FROM employers WHERE username='$username'");
if(mysql_num_rows($account))
echo 'The user: "'.$username.'" is still storred in the database.';
else
echo 'The user: "'.$username.'" is no longer storred in the database.';
}
}
?>



Decided, what I posted earlier wasn't good enough for my own tastes. I've reconstructed the script and tested it on my local server. It seems to be running just fine.
I've added a few things to test it with, you may remove them if you need to. If you need help with something that I've added, just ask.

StupidRalph
03-17-2008, 12:51 PM
Are you using InnoDB tables? If so you can just have it cascade on delete which will handle this for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum