Jump to content

[SOLVED] Trying to Delete from two MYSQL tables in php script


rahish

Recommended Posts

I am creating a very simple cms system and have a delete statement which is supposed to delete an item from a menu table and a page table which (has an identical id) when an admin user deletes the page

 

There is an if statement that sends and integer "1" if the admin user has clicked the html checkbox on a form to ask for the page to be deleted from the menu as well as the page itself.

 

I can't work out the syntax of the mysql statement to do this

 

I am not sure if this is the right forum for this question.

 

But here is the code I am using

<?php


include('../dbconnection.php');



$submit=$_GET['submit'];
$id=$_GET['id'];
$menu=$_GET['menu'];

if ( $menu=='1' )
{


$query=mysql_query("DELETE FROM menu, pages WHERE menu.id AND pages.page_id=$id")  or die ("Couldn't execute query.");

//$query = mysql_query("DELETE FROM menu WHERE id=$id") or die (mysql_error());

echo "You have now deleted entry $id from the database and entry $id from the menu<br />";

echo " <a href='posts.php?id=$id'>Click here to go back</a>";    


}
else
{
       
  $query=mysql_query("DELETE FROM pages WHERE page_id=$id")  or die ("Couldn't execute query.");  
  
  echo "You have now deleted entry $id from the database<br />";
  echo " <a href='posts.php?id=$id'>Click here to go back</a>";  
}
  
?>

 

Link to comment
Share on other sites

erm try this not sure about the syntax of the mysql

<?php


include('../dbconnection.php');



$submit=$_GET['submit'];
$id=$_GET['id'];
$menu=$_GET['menu'];

if ( $menu=='1' )
{


mysql_query("DELETE FROM menu, pages WHERE menu.id AND pages.page_id= '$id'")  or die ("Couldn't execute query.");

//$query = mysql_query("DELETE FROM menu WHERE id=$id") or die (mysql_error());

echo "You have now deleted entry $id from the database and entry $id from the menu<br />";

echo " <a href='posts.php?id=$id'>Click here to go back</a>";    


}
else
{
       
mysql_query("DELETE FROM pages WHERE page_id= '$id'")  or die ("Couldn't execute query.");  
  
  echo "You have now deleted entry $id from the database<br />";
  echo " <a href='posts.php?id=$id'>Click here to go back</a>";  
}
  
?>

Edited

If that  does not work you could just make two individual mysql query's that would be easy to do

Link to comment
Share on other sites

Thanks I tried both but I am still getting a can't execute query error.

 

This is what I am now using

<?php


include('../dbconnection.php');



$submit=$_GET['submit'];
$id=$_GET['id'];
$menu=$_GET['menu'];

if ( $menu=='1' )
{


$query=mysql_query("DELETE FROM menu WHERE id = '$id'")  or die ("Couldn't execute query.");
$query2=mysql_query("DELETE FROM pages WHERE pages_id = '$id'")  or die ("Couldn't execute query.");



echo "You have now deleted entry $id from the database and entry $id from the menu<br />";

echo " <a href='posts.php?id=$id'>Click here to go back</a>";    


}
else
{
       
  $query=mysql_query("DELETE FROM pages WHERE page_id=$id")  or die ("Couldn't execute query.");  
  
  echo "You have now deleted entry $id from the database<br />";
  echo " <a href='posts.php?id=$id'>Click here to go back</a>";  
}
  
?>

Link to comment
Share on other sites

try this

<?php


include('../dbconnection.php');



$submit=$_GET['submit'];
$id=$_GET['id'];
$menu=$_GET['menu'];

if ( $menu=='1' )
{

mysql_query("DELETE FROM menu WHERE id = '$id'")  or die(mysql_error());
mysql_query("DELETE FROM pages WHERE pages_id = '$id'")  or die(mysql_error());



echo "You have now deleted entry $id from the database and entry $id from the menu<br />";

echo " <a href='posts.php?id=$id'>Click here to go back</a>";    


}
else
{
       
mysql_query("DELETE FROM pages WHERE page_id = '$id'")  or die(mysql_error());  
  
  echo "You have now deleted entry $id from the database<br />";
  echo " <a href='posts.php?id=$id'>Click here to go back</a>";  
}
  
?>

this will give you some error reporting

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.