删除数据库中的记录

时间:2013-09-19 13:00:40

标签: php

我制作了一个程序,它将删除数据库中的记录,但它正在运行,但程序会在没有确认的情况下自动删除记录。我希望我的程序在删除之前先确认,我该怎么做?

内容功能代码:

 //*********************************************************************
 function Content() 
{
    if(isset($_POST['btnAdd']))  //if in case 'Add Entry' button is click 
    {
        return DataEntryForm('');
    }
    else if(isset($_POST['btnSave'])) //if in case 'Save' button is click during adding new entry or editing
    {
        if($_POST['uname']=='')
        {
            return saveRecord();
        }
        else
        {
            return updateRecord();
        }
    }
    else if(isset($_GET['edituname'])) //if in case 'Edit' is click 
    {
        return DataEntryForm('Edit');
    }
    else if(isset($_GET['deluname']))  //if in case 'Delete' is click 
    {
        return deleteRecord();
    }
    else if(isset($_POST['btnSearch'])) //if in case 'Search' is click 
    {
        return viewRecord();
    }
    else
    {
        return viewRecord('');
    }

}

删除记录功能代码:

//***************************************************
// delete record
//***************************************************
function deleteRecord()
{
    $uname=$_GET['deluname'];

    $sql = "DELETE FROM users WHERE UserName='$uname'";
    $result = mysql_query($sql) or die(mysql_error());



      //-------------------------------------------
      // Display notification if successful
      //-------------------------------------------
      $code = <<< htmlcode
          <br/>
          <br/>
          <p align="center" border=0 style="font-family:verdana,helvetica; font-size:15px; color:green"> 
                User successfully deleted.
          </p>
          <br/>
          <center>
            <form name="frmDataEntry" method="POST" action="" style="font-family:verdana,helvetica; font-size:12px;">
              <table border=0 style="font-family:verdana,helvetica; font-size:12px;">
                <tr>
                  <td>
                    <input type="submit" name="btnBack" value="&nbsp&nbsp&nbsp OK &nbsp&nbsp&nbsp" onClick ="frmDataEntry.action='DataEntry_List_Search.php'"/>                  
                  </td>
                </tr>
              </table>    
            </form>
          </center>
          <br/>
          <br/>
htmlcode;

    return $code;

}

2 个答案:

答案 0 :(得分:0)

这里看不到调用代码,但通常只需要一个Javascript confirm() - 函数:

<form action="delete.php" onsubmit="return confirm('Do you really want to delete user?');">

答案 1 :(得分:0)

请尝试使用此表单,然后删除我在提交

时添加的表单行
//***************************************************
// delete record
//***************************************************
function deleteRecord()
{
    $uname=$_GET['deluname'];

    $sql = "DELETE FROM users WHERE UserName='$uname'";
    $result = mysql_query($sql) or die(mysql_error());



      //-------------------------------------------
      // Display notification if successful
      //-------------------------------------------
      $code = <<< htmlcode
          <br/>
          <br/>
          <p align="center" border=0 style="font-family:verdana,helvetica; font-size:15px; color:green"> 
                User successfully deleted.
          </p>
          <br/>
          <center>
            <form name="frmDataEntry" method="POST" action="" onsubmit="return confirm('Do you really want to delete??');"> style="font-family:verdana,helvetica; font-size:12px;">
              <table border=0 style="font-family:verdana,helvetica; font-size:12px;">
                <tr>
                  <td>
                    <input type="submit" name="btnBack" value="&nbsp&nbsp&nbsp OK &nbsp&nbsp&nbsp" onClick ="frmDataEntry.action='DataEntry_List_Search.php'"/>                  
                  </td>
                </tr>
              </table>    
            </form>
          </center>
          <br/>
          <br/>
htmlcode;

    return $code;

}