Jump to content

Recommended Posts

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 attached and the website is online and said page can be viewed at:

 

www.joey-barrett.com/newDelete.php

 

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

 

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.

 

My issue is that it wont delete the record chosen in the dropdown because I am not sure what code to use. I have the code Attached, I hope people can help.

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/
Share on other sites

try adding an " or die(mysql_error()) after these queries

    $del_master = "delete from master_name where id = $_POST[sel_id]";
    mysql_query($del_master);

    $del_address = "delete from address where id = $_POST[sel_id]";
    mysql_query($del_address);

    $del_tel = "delete from telephone where id = $_POST[sel_id]";
    mysql_query($del_tel);


    $del_fax = "delete from fax where id = $_POST[sel_id]";
    mysql_query($del_fax);

    $del_email = "delete from email where id = $_POST[sel_id]";
    mysql_query($del_email);

    $del_note = "delete from personal_notes where id = $_POST[sel_id]";
    mysql_query($del_master);

 

On a side not, any reason you made so many tables? You could have made one table with these fields in the one table.

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/#findComment-1166302
Share on other sites

you did the die like so:

 

    $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());

 

If it is giving an error, what is it?

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/#findComment-1166322
Share on other sites

ya the die function is here:

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());
}

 

the error is this:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[sel_id]' at line 1

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/#findComment-1166324
Share on other sites

here is the code in its entirity.

 <?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>     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>
   ";
}
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>

 

as it has changed a small bit since the upload of the file earlier.

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/#findComment-1166340
Share on other sites

I have hit a wall.... I think there may be some error with the <form> or <input>elements.... in that there is an input named 'op' im not sure if this is a reserved word but I am using this in the confirm button, which is an image. there is something not right with that.... I think that my button is not excecuting the delete statements and instead just returning to delentry.php main page with the dropdown selector.

 

any help greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/#findComment-1166344
Share on other sites

What should I put in to make it know what to delete? I have been trying different things and I think u are right, it doesent know what to delete.... ? current code below

 

<?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>     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>

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/#findComment-1166505
Share on other sites

try changing

if ($_POST[op] == "delete"){

 

to

 

if (isset($_POST["delete"])){

 

NOPE not gonna work, he has no input named "delete".

 

Should be:

if(isset($_POST['op']) && $_POST['op'] == 'delete')

 

You should also have an input field that holds the unique ID of the row you want to delete out of the DB. 

 

OR,

 

Just populate the input with $_POST['sel_id'].

Link to comment
https://forums.phpfreaks.com/topic/225903-php-college-help/#findComment-1166611
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.