Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry unable to delete a record using php

    I am having a form which displays a couple of values from database and a delete button which is used to delete that particular record. Below is the code that I am using, but I am unable to delete the record. Please help me to get the record deleted. Thanks in advance!

    with the below php code I am displaying the username and password on the page along with a delete button
    When clicked on the delete button I used a javascript function which takes me to another page where I am actually executing the delete record functionality. My problem is when I used input type as "button", I am expecting that the javascript function should get executed which is not happening now. If I use the input type as submit it is taking me to the next page which is specified in the action property. Here when button is clicked, first a confirm button should popup which should ask for the confirmation of the user. If the user continues, then the record should be deleted. If the user cancels, then control should be in the currentpage only.

    <?php
    @session_start();
    include("dbconnect.php");
    $nbr = $_GET['Nbr'];
    $Query = "Select * from tab where nbr = $nbr";
    $res = mysql_query($Query);
    if(mysql_num_rows($res) >0)
    {
    $row = mysql_fetch_assoc($res);
    Email : $row[eMail]";
    $uname = "$row[uname]";
    $pwd = "$row[pwd]";
    }
    ?>

    <html>
    <head>
    <script language="javascript" type="text/javascript" >
    function deleteNbr()
    {
    if(confirm("This cannot be undone!"))
    {
    location.href = "DelRec.php";
    }
    }
    </script>
    </head>
    <body>
    <form name="deletefrm" method="POST" action="delrec.php">
    <table>
    <tr>
    <td >username </td>
    <td width="5">:</td>
    <td><?php echo "$uname"?></td>
    <td >&nbsp;</td>
    <td >password:</td>
    <td ><?php echo "$pwd"?></td>
    <td><input name="delete" type="Submit" value="Delete " onClick="return deleteNbr();"/>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

  • #2
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ok, basically at no point if the confirmation fails do you tell it to stop, the way i do it is without the external function:

    <input name="delete" type="Submit" value="Delete " onClick="if (!confirm(\'This cannot be undone!\')) return false">

    Just swap your submit button line for that, has always worked for me :P
    Mike

  • #3
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    I think your JavaScript is the cause of the problem...as you are performing a re-direct when you use location.href = "DelRec.php";

    instead, if the user answers yes, you should "return true" which would tell the browser to send a "POST" request based on the "action" specified in the form tag.

    so, change your javascript to the following.

    <script language="javascript" type="text/javascript" >
    function deleteNbr()
    {
    if(confirm("This cannot be undone!"))
    {
    return true; // submit form is user says yes.
    } else {
    return false; // don't submit form if user says no
    }
    }
    </script>

    let's know if that worked or not.

    cheers,
    ess

  • #4
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    above two solutions are not working.
    If I use "Submit", record is deleted without confirmation.
    If I use "Button", javascript error saying that property not supported.

    My problem is there is a record displayed on the browser with a "delete" button below the record. If user wishes to delete the record, user should get confirmation and is yes, then that record should be deleted otherwise the control should stay there. can I do this without redirection. Then this is very much helpful for me. any help is appreciated.

  • #5
    New Coder
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It is my mistake. above code is working perfectly fine. I did not close a HTML tag. Thanks for the help.

  • #6
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    865
    Thanks
    7
    Thanked 29 Times in 28 Posts
    sesc....do you use JS Debugger?

    if not, I recommend that you give FireBug a test drive...which is a plugin that you can add to Firefox. It is very handy when writing JS code...as it allows you to create break points etc...and hence, trace the line that is throwing the error.

    you can download firebug from the following location
    http://www.getfirebug.com/

    Cheers,
    Ess

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    why i cannot delete yg record???help meee

    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="ADUAN"; // Database name
    $tbl_name="adu"; // Table name

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>
    <table width="400" border="1" cellspacing="0" cellpadding="3">


    <tr>
    <td align="center"><strong>Nama</strong></td>
    <td align="center"><strong>Sekolah</strong></td>
    <td align="center"><strong>Masalah</strong></td>
    <td align="center"><strong>Maklumbalas</strong></td>
    <td align="center"><strong>Delete</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>

    <tr>
    <td><?php echo $rows["Nama"]; ?></td>
    <td><?php echo $rows["Sekolah"]; ?></td>
    <td><?php echo $rows["Masalah"]; ?></td>
    <td><?php echo $rows["Maklumbalas"]; ?></td>


    <td align="center"><a href="delete_ac.php?id=<?php echo $rows['id']; ?>">delete</a></td>
    </tr>
    <?php
    }
    ?>
    </table>
    </td>
    </tr>
    </table>
    <?php
    mysql_close();
    ?>



    delete_ac

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php

    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="ADUAN"; // Database name
    $tbl_name="test_mysql"; // Table name

    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    // get value of id that sent from address bar
    $id=$_GET['id'];

    // Delete data in mysql from row that has this id
    $sql="DELETE FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);

    // if successfully deleted
    if($result){
    echo "Deleted Successfully";
    echo "<BR>";
    echo "<a href='delete.php'>Back to main page</a>";
    }
    <form name="form1" method="post" action="checklogin.php">
    else {
    echo "ERROR";
    }

    // close connection
    mysql_close();

    ?>
    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •