[SOLVED] Please help with delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

[SOLVED] Please help with delete

Post by melindaSA »

I am going crazy can someone please take a look at the following, It will not work, and the result is a blank page -- does not show the error!!

Code: Select all

<?php

  include('../newsletter_inc_fns.php');
  $id = $HTTP_GET_VARS['id'];
  
   $query = "delete from site_page_items where id='$id'";
   $result = mysql_query($query) or die(mysql_error()); 
   if(!$result == TRUE)
   {
     header("location:sitemanager.php?msg=suc");
   }
   else
   {
     header("location:sitemanager.php?msg=fail");
   }


?>
Last edited by melindaSA on Sun Aug 15, 2004 5:59 pm, edited 3 times in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The include might be failing.
Try putting this at the top:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//rest of your code below
?>
..see if that shows what the problem is.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

Thank you, It was my include!
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

It is still not working, I changed to the following, and get my success message, but it has not deleted from the database:

Code: Select all

<?php
 include('../database/dbconnect.php');
   if ($_POST['id'])
 {
  
   $query = "delete from site_page_items where id='$id'";
   $result = mysql_query($query) or die(mysql_error()); 
  
     header("location:sitemanager.php?msg=suc"); 
   } 
   else 
   { 
     header("location:sitemanager.php?msg=fail"); 
   } 
   

?>
Please help! Thank you!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If you have register_globals Off (which you should) then you'll need to use:
$query = "delete from site_page_items where id='".$_POST['id']."'";

as $id won't be set. Leaving the error_reporting(E_ALL); at the top would catch this.
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

That still does not work, I still get the success message, but not deleted from database.

I tried the following, and now it deleted all the records from the table??

Code: Select all

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

  include('../database/dbconnect.php');
  
   if ($_POST['id'])
 {
  
   $query = "delete from site_page_items where id=".$_POST['id'];
   $result = mysql_query($query) or die(mysql_error()); 
  
     header("location:sitemanager.php?msg=suc"); 
   } 
   else 
   { 
     header("location:sitemanager.php?msg=fail"); 
   } 
?>
??now I am not sure??
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Okie, after this line:
$query = "delete from site_page_items where id=".$_POST['id'];
put:
echo $query;
and see what that outputs, make sure it 'looks ok'.
I'd also comment out the actual mysql_query() line whilst testing to avoid any mass db deletions ;)
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

I get:
delete from site_page_items where id='id'
So it seems like I am not pulling the id from the form. Here is the form code:

Code: Select all

<?php
 <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr> 
                <td>
                  <form name="frmsite" action="" method=post>
                <table width="95%" border="0" align="center" cellpadding="5" cellspacing="5">
                <tr> 
                         <td colspan="3"><font face="verdana, arial, sans-serif" size=2>Select Item :</font></td>
                         <td width="61%"> 

<?if($_SESSION["usrname"]=="****"){?>
                         <select name="mmenu" >
                                <option value='-1'><font face="verdana, arial, sans-serif" size=2>Select Item</font></option>
                 <?
                         $sql = "select * from  site_page_items";
                         $res = mysql_query($sql) or die("Error :".mysql_error());
                         if (mysql_num_rows($res) > 0)
                         {
                                 while($result = mysql_fetch_array($res))
                                 {
                                        
                                         echo "<option value='".$result['id']."'>".$result['item_title']."</option>";

                                        
                                 }
                         }

                         ?>
										echo "<option value='1'>New Newsletter</option>";
                          </select>
<?}?>

                         </td>
                </tr>
                
                <tr> 
                        <td colspan="3">&nbsp;</td>
                         <td width="61%">&nbsp;</td>
                </tr>
                <tr> 
                         <td width="4%">&nbsp;</td>
                         <td width="26%">&nbsp;</td>
                        <td width="9%"> 
                      <input type="button" name="Button" value="Edit/Add" onclick="javascript:editsite()";>
                    </td>
                    <td width="61%">
                      <input type="button" name="Submit2" value="Delete" onclick="javascript:deletesite()";>
                      

                    </td>
                  </tr>
                </table>
                </form>
                </td>
        </tr>
      </table>

?>
and here is the javascript code:

Code: Select all

function deletesite()
&#123;
        
        if (document.frmsite.mmenu.value == '-1')
        &#123;
                alert("Select the Menu Item");
                return false;
        &#125;
         if (document.frmsite.mmenu.value == '1')
	        &#123;
	                alert("Cannot delete this item");
	                return false;
        &#125;
   if (confirm("Deleting this page cause this page unavailable when clicking any link related to it from anywhere in the site - \nAre you sure delete this Web Site Page ?"))
   &#123;
        document.frmsite.action = "deletesitepage.php";
        document.frmsite.submit();
   &#125;
&#125;
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It looks like mmenu is the value you're trying to use, so maybe you need to be using $_POST['mmenu'] rather than $_POST['id'].
melindaSA
Forum Commoner
Posts: 99
Joined: Thu Oct 02, 2003 7:34 am

Post by melindaSA »

OH!! I am so dumb!! :oops: I missed that!!

Thank you :D
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It's alot easier to debug someone elses code rather than your own ... easily done ;)
Post Reply