So heres my issue... I am a student studying php and as a project am making an online php phonebook.
It is database driven, and currently I am having issues with the delete page. the code for this is below and the website is online and said page can be viewed at:

www.joey-barrett.com/delentry.php

from the above page you will notice that I cannot delete the record... please help!!

It is something to do with the code not knowing when the button is clicked.


As I said, I am a student and do not claim to be an expert. I do however want to learn where I have gone wrong.

<?php
include('connectToDb.php');
include('menuInclude.php');
// make methods to include for getting details for dropdown so it can be used with edir entry and selentry. 


if ($_POST[op] != "view") {
   //haven't seen the form, so show it
   $display_block = "<h1>Select an Entry To Delete</h1>";

   //get parts of records
    $get_list = "select id, concat_ws(', ', l_name, f_name) as display_name
        from master_name order by l_name, f_name ";
   $get_list_res = mysql_query($get_list) or die(mysql_error());

   if (mysql_num_rows($get_list_res) < 1) {
       //if no records
       
      $display_block .= "
      <img src='/images/redExclamation.png/' width='50' height='80' alt='Sorry, no records to Edit!'>  
      <em> &nbsp;&nbsp;&nbsp; Sorry, There are no records In The Database to Modify!</em>
      ";
   }
   else{
       //has records, so get results and print in a form
       $display_block .= "
       <form method='post' action='$_SERVER[PHP_SELF]'>
      <P><strong>Select a Record Below:</strong><br>
       <select name='sel_id'>
       <option value=' '>Select One</option>
      ";
   }
       while ($recs = mysql_fetch_array($get_list_res)) {
           $id = $recs['id'];
           $display_name = stripslashes($recs['display_name']);

           $display_block .= "
         <option value='$id'>$display_name</option> 
         ";
       }
       $display_block .="
       </select>
       <input type='hidden' name='op' value='view'>
       <p><input type='submit' name='submit'
           value='Modify Details'></p>
       </FORM>
       ";
    }

else if ($_POST[op]=='view'){
    //check for required fields
    if ($_POST[sel_id]==""){
        header('Location: delentry.php');
        exit;
    }

    //get master_info
    $get_master = "select concat_ws(' ', f_name, l_name) as display_name
         from master_name where id = $_POST[sel_id]";
    $get_master_res = mysql_query($get_master);
    $display_name = stripslashes(mysql_result($get_master_res, 0,'display_name'));
   $display_block ="<h1>Do you want to delete $display_name ?</h1><table>";
   //get all addresses
    $get_addresses = "select address, city, county, type
         from address where master_id = $_POST[sel_id]"; //gets address details from masterid stored as sel_id from item selected in list.
    $get_addresses_res = mysql_query($get_addresses);

     if (mysql_num_rows($get_addresses_res) > 0) {

        $display_block .= "<tr><td><strong>Address:</strong></td>";

        while($add_info = mysql_fetch_array($get_addresses_res))
            {
            $address = $add_info[address];
            $city = $add_info[city];
            $county = $add_info[county];
            $address_type = $add_info[type];
            $display_block .= "<td>$address $city $county</td>
                <td>($address_type)</td></tr>";
        }

      $display_block .="<tr></tr>";
      
    }
      
   else
      $display_block .= "<tr><td><strong>Address:</strong></td><td>Sorry!, No Address to Display for $display_name.</td></tr>";
      


    //get all tel
    $get_tel = "select tel_number, type from telephone where master_id = $_POST[sel_id]";
    $get_tel_res = mysql_query($get_tel);

    if (mysql_num_rows($get_tel_res) > 0) {
        $display_block .= "<tr><td><strong>Telephone:</strong></td>";

        while ($tel_info = mysql_fetch_array($get_tel_res)) {
            $tel_number = $tel_info[tel_number];
            $tel_type = $tel_info[type];

            $display_block .= "<td>$tel_number</td> <td>($tel_type)</td></tr>";
        }
    }
   else
      $display_block .= "<tr><td><strong>Telephone:</strong></td><td>Sorry, No Telephone number On File For $display_name</td>"; 
      //if no telephone number in database for this entry

       //get all fax
    $get_fax = "select fax_number, type from fax where master_id = $_POST[sel_id]";
    $get_fax_res = mysql_query($get_fax);

    if (mysql_num_rows($get_fax_res) > 0) {

        $display_block .= "<tr><td><strong>Fax:</strong></td>";

        while ($fax_info = mysql_fetch_array($get_fax_res)) {
           $fax_number = $fax_info[fax_number];
            $fax_type = $fax_info[type];
 
            $display_block .= "<td>$fax_number</td><td> ($fax_type)</td></tr>";
        }

        
    }
   else
      $display_block .= "<tr><td><strong>Fax:</strong></td><td>Sorry, No Fax number On File For $display_name</td>"; 
         // if no fax number on file displays this

          //get all email
       $get_email = "select email, type from email where master_id = $_POST[sel_id]";
       $get_email_res = mysql_query($get_email);

    if (mysql_num_rows($get_email_res) > 0) {

        $display_block .="<tr><td><strong>Email:</strong></td>
        ";

        while ($email_info = mysql_fetch_array($get_email_res)) {
            $email = $email_info[email];
            $email_type = $email_info[type];

         $display_block .="
            <td>$email </td><td>($email_type)</td></tr>
   ";
        }
    }
    else
    $display_block .="
       <tr><td><strong>E-Mail:</strong></td><td>Sorry, No E-mail Address On File For $display_name</td>
        "; //if no email on file for this entry

    //get personal note
    $get_notes = "select note from personal_notes where
         master_id = $_POST[sel_id]
       ";
    $get_notes_res = mysql_query($get_notes);

    if (mysql_num_rows($get_notes_res) == 1) {
        $note = nl2br(stripslashes(mysql_result($get_notes_res,0,'note')));

        $display_block .="
        <tr><td><strong>Personal Notes:</strong></td><td>$note</td></tr></table>
      ";
    }
   else
      $display_block .= "<tr><td><strong>Personal Notes:</strong></td><td>Sorry, No Personal Notes On File For $display_name</td></table>
        ";
      //end display all details.
   
   $display_block .="
   <P align='center'>
   <form method='POST' action='$_SERVER[PHP_SELF]'>
   <table>
   <tr>
   <td>Are You Sure You Want to Delete <b>$display_name 's</b> record?</td></tr>
   <tr>
   <td>
        <input type='hidden' name='op' value='delete'>
        <input type='image' value='delete' src='images\confirm.gif' alt='YES, Delete record For $display_name' border='0' \>


        <input type='image' value='dontDelete' src='\images\decline.gif' alt='No!, Do not Delete record For $display_name' border='0' \>
   </td>
   </table>
   </p>
   ";}
   echo $_POST['sel_id'];

if ($_POST[op] == "delete"){
    $del_master = "DELETE FROM master_name WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_master)
    or die(mysql_error());

    $del_address = "DELETE FROM address WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_address)
    or die(mysql_error());

    $del_tel = "DELETE FROM telephone WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_tel)
    or die(mysql_error());


    $del_fax = "DELETE FROM fax WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_fax)
    or die(mysql_error());

    $del_email = "DELETE FROM email WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_email)
    or die(mysql_error());

    $del_note = "DELETE FROM personal_notes WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_master)
    or die(mysql_error());

}
else if ($_POST[op] == "dontDelete") {
    header("Location: editentry.php");
    exit;
}

?>
<HTML>
<HEAD>
<TITLE>Editing record For: <? print $display_name?></TITLE>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="\images\confirm.gif" >
</HEAD>
<BODY>
<div align="center"
<? menuInclude.php; ?>
<? print $display_block; ?>
</div>
</BODY>
</HTML>

Recommended Answers

All 3 Replies

Hi looks like your missing a closing form tag </form> near line 185. Just a note looking at your table structure from your query's you have separate tables for fields/attributes that could all be in 1 record of 1 table just seems a very strange way of storing your data. Doing it like this really not a good idea.

N

hi there. we were told that we had to use seperate tables to store data. I also fixed that form element and still it is bypassing and not deleteing.

<?php
include('connectToDb.php');
include('menuInclude.php');
// make methods to include for getting details for dropdown so it can be used with edir entry and selentry. 
if ($_POST[op] != "view") {
   //haven't seen the form, so show it
   $display_block = "<h1>Select an Entry To Delete</h1>";

   //get parts of records
    $get_list = "select id, concat_ws(', ', l_name, f_name) as display_name
        from master_name order by l_name, f_name ";
   $get_list_res = mysql_query($get_list) or die(mysql_error());

   if (mysql_num_rows($get_list_res) < 1) {
       //if no records       
	   $display_block .= "
	   <img src='/images/redExclamation.png/' width='50' height='80' alt='Sorry, no records to Edit!'>  
	   <em> &nbsp;&nbsp;&nbsp; Sorry, There are no records In The Database to Modify!</em>
	   ";

           }
   else{
       //has records, so get results and print in a form
       $display_block .= "
       <form method='post' action='$_SERVER[PHP_SELF]'>
	   <P><strong>Select a Record Below:</strong><br>
       <select name='sel_id'>
       <option value=' '>Select One</option>
	   ";
   }
       while ($recs = mysql_fetch_array($get_list_res)) {
           $id = $recs['id'];
           $display_name = stripslashes($recs['display_name']);

           $display_block .= "
		   <option value='$id'>$display_name</option> 
		   ";
       }
       $display_block .="
       </select>
       <input type='hidden' name='op' value='view'>
       <p><input type='submit' name='submit'
           value='Modify Details'></p>
       </FORM>
       ";
    }

else if ($_POST[op]=='view'){
    //check for required fields
    if ($_POST[sel_id]==""){
        header('Location: delentry.php');
        exit;
    }

    //get master_info
    $get_master = "select concat_ws(' ', f_name, l_name) as display_name
         from master_name where id = $_POST[sel_id]";
    $get_master_res = mysql_query($get_master);
    $display_name = stripslashes(mysql_result($get_master_res, 0,'display_name'));
	$display_block ="<h1>Do you want to delete $display_name ?</h1><table>";
   //get all addresses
    $get_addresses = "select address, city, county, type
         from address where master_id = $_POST[sel_id]"; //gets address details from masterid stored as sel_id from item selected in list.
    $get_addresses_res = mysql_query($get_addresses);

     if (mysql_num_rows($get_addresses_res) > 0) {

        $display_block .= "<tr><td><strong>Address:</strong></td>";

        while($add_info = mysql_fetch_array($get_addresses_res))
            {
            $address = $add_info[address];
            $city = $add_info[city];
            $county = $add_info[county];
            $address_type = $add_info[type];
            $display_block .= "<td>$address $city $county</td>
                <td>($address_type)</td></tr>";
        }

		$display_block .="<tr></tr>";
		
    }
		
	else
		$display_block .= "<tr><td><strong>Address:</strong></td><td>Sorry!, No Address to Display for $display_name.</td></tr>";
		


    //get all tel
    $get_tel = "select tel_number, type from telephone where master_id = $_POST[sel_id]";
    $get_tel_res = mysql_query($get_tel);

    if (mysql_num_rows($get_tel_res) > 0) {
        $display_block .= "<tr><td><strong>Telephone:</strong></td>";

        while ($tel_info = mysql_fetch_array($get_tel_res)) {
            $tel_number = $tel_info[tel_number];
            $tel_type = $tel_info[type];

            $display_block .= "<td>$tel_number</td> <td>($tel_type)</td></tr>";
        }
    }
	else
		$display_block .= "<tr><td><strong>Telephone:</strong></td><td>Sorry, No Telephone number On File For $display_name</td>"; 
		//if no telephone number in database for this entry

    	//get all fax
    $get_fax = "select fax_number, type from fax where master_id = $_POST[sel_id]";
    $get_fax_res = mysql_query($get_fax);

    if (mysql_num_rows($get_fax_res) > 0) {

        $display_block .= "<tr><td><strong>Fax:</strong></td>";

        while ($fax_info = mysql_fetch_array($get_fax_res)) {
           $fax_number = $fax_info[fax_number];
            $fax_type = $fax_info[type];
 
            $display_block .= "<td>$fax_number</td><td> ($fax_type)</td></tr>";
        }

        
    }
	else
		$display_block .= "<tr><td><strong>Fax:</strong></td><td>Sorry, No Fax number On File For $display_name</td>"; 
			// if no fax number on file displays this

    		//get all email
    	$get_email = "select email, type from email where master_id = $_POST[sel_id]";
    	$get_email_res = mysql_query($get_email);

    if (mysql_num_rows($get_email_res) > 0) {

        $display_block .="<tr><td><strong>Email:</strong></td>
        ";

        while ($email_info = mysql_fetch_array($get_email_res)) {
            $email = $email_info[email];
            $email_type = $email_info[type];

         $display_block .="
            <td>$email </td><td>($email_type)</td></tr>
	";
        }
    }
    else
	 $display_block .="
       <tr><td><strong>E-Mail:</strong></td><td>Sorry, No E-mail Address On File For $display_name</td>
        "; //if no email on file for this entry

    //get personal note
    $get_notes = "select note from personal_notes where
         master_id = $_POST[sel_id]
		 ";
    $get_notes_res = mysql_query($get_notes);

    if (mysql_num_rows($get_notes_res) == 1) {
        $note = nl2br(stripslashes(mysql_result($get_notes_res,0,'note')));

        $display_block .="
        <tr><td><strong>Personal Notes:</strong></td><td>$note</td></tr></table>
		";
    }
	else
		$display_block .= "<tr><td><strong>Personal Notes:</strong></td><td>Sorry, No Personal Notes On File For $display_name</td></table>
        ";
		//end display all details.
   
   $display_block .="
   <P align='center'>
   <form method='POST' action='$_SERVER[PHP_SELF]'>
   <table>
   <tr>
   <td>Are You Sure You Want to Delete <b>$display_name 's</b> record?</td></tr>
   <tr>
   <td>
        <input type='hidden' name='op' value='delete'>
        <input type='image' value='delete' src='images\confirm.gif' alt='YES, Delete record For $display_name' border='0' \>


        <input type='image' value='dontDelete' src='\images\decline.gif' alt='No!, Do not Delete record For $display_name' border='0' \>
   </td>
   </table>
   </form>
        </p>

        ";}
   echo $_POST['sel_id'];

if ($_POST[op] == "delete"){
    $del_master = "DELETE FROM master_name WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_master)
    or die(mysql_error());

    $del_address = "DELETE FROM address WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_address)
    or die(mysql_error());

    $del_tel = "DELETE FROM telephone WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_tel)
    or die(mysql_error());


    $del_fax = "DELETE FROM fax WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_fax)
    or die(mysql_error());

    $del_email = "DELETE FROM email WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_email)
    or die(mysql_error());

    $del_note = "DELETE FROM personal_notes WHERE id = '" . $_POST['sel_id'] . "'";
    mysql_query($del_master)
    or die(mysql_error());

}
else if ($_POST[op] == "dontDelete") {
    header("Location: editentry.php");
    exit;
}

?>
<HTML>
<HEAD>
<TITLE>Editing record For: <? print $display_name?></TITLE>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="\images\confirm.gif" >
</HEAD>
<BODY>
<div align="center"
<? menuInclude.php; ?>
<? print $display_block; ?>
</div>
</BODY>
</HTML>

Try changing the input type to a button as follows

<form method='POST' action= <?php $_SERVER['PHP_SELF']?>>
<button type="submit" name="delete" value="delete">
<img src="button_image.gif"  width="32" height="32" alt="delete" border="0" />
</button>
</form>

<?php 
if (isset($_POST['delete']))
{
	echo"button pressed";
}?>

line 178/179

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.