Jump to content

How do I NOT delete a record if a subrecord is associated to it ??


spacepoet

Recommended Posts

Hello:

 

I am making a dynamic food menu system for a project so a client can manage his food and drink menus.

 

I gave him the ability to add, edit, and delete menus.

 

However, for the "Delete Menu" option, I would like to first check and see if a Category (menuCatID) or a Menu Item (menuItemID) is associated to the Main Menu (menuID) and - if so - to *NOT* allow that Menu to be deleted. I would like to send them back to the a_Menus.php page and display a message like "You must delete all Categories and Menu Items associated with this Menu before the Menu can be deleted."

 

This is the code:

a_Menus.php

<a href="a_MenusDelete.php?menuID=".$menuData['menuID'].">Delete Menu</a>

 

a_MenusDelete.php

<?php

include('../include/myConn.php');
include('../include/myCodeLib.php');
include('include/myCheckLogin.php');

$menuID = $_REQUEST['menuID'];

mysql_query("DELETE FROM theMenus WHERE menuID = $menuID");

header("Location: a_Menus.php");

?>

<html>
<head>

<title></title>

</head>

<body>


</body>
</html>

 

 

How would I be able to accomplish this?

 

Thanks.

I'm sure those with more experience will have a neater answer and not knowing your table structures makes it hard for an exact but I would think you could just add some queries to your page.  Something like

IF ($_GET['menuID']){
$checkforsubs = mysql_query("SELECT COUNT(menuCatID) FROM menuCats WHERE menuID=$_GET[menuID]"); 
$chkforsubs = mysql_result($checkforsubs,0);
IF ($chkforsubs<1){
$subs="good";
}
$checkforitems = mysql_query("SELECT COUNT(menuItemID) FROM menuItems WHERE menuID=$_GET[menuID]"); 
$chkforitems = mysql_result($checkforitems,0);
IF ($chkforitems<1){
$items="good";
}
IF ($subs=="good" && $items="good"){
mysql_query("DELETE FROM theMenus WHERE menuID = $menuID");
header("Location: a_Menus.php");
}
ELSE{
header("Location: a_Menus.php?error1=t");
} 
}// END IF ($_GET['menuID'])

On the a_Menus.php page you would pick up the error1

IF ($_GET['error1']=='t'){
echo "You must delete all Categories and Menu Items associated with this Menu before the Menu can be deleted.";
}

 

Nothing tested just off the top if you know what I mean.  Best to ya.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.