无法使用PHP从MSSQL数据库中删除查询数据

时间:2014-06-16 11:52:30

标签: php html sql-server

我在从MSSQL数据库中删除查询数据时遇到问题。我的程序所做的是你可以选择查找特定数据然后如果你想删除所选数据你只需要点击删除按钮。我成功地显示了查询数据,但我的删除功能总是失败。

这是我的ReadProcess.php代码(查询功能):

<?php

include_once("ConnectProcess.php");

class ReadData extends Connection {

  public $Name;

  public function doReadData(){

      $this->Name = $_POST["selectNAME"];

      //declare the SQL statement that will query the database
      $query = "SELECT * FROM dbo.ProfileTable WHERE Name = '$this->Name' OR Sex = '$this->Name' ";

      //keyset cursor needed in order to run sqlsrv_num_rows function
      $params = array();
      $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

      //execute the SQL query and return records
      $result = sqlsrv_query($this->conn, $query, $params, $options)
         or die( print_r( sqlsrv_errors(), true));

      //Show results in table, if statement for show results of data or no records found
      if(sqlsrv_num_rows($result)>0){
        $o = '<table id="myTable">
            <thead>
            <tr>
            <th>Name</th>
            <th>Age </th>
            <th>Sex</th>
            </tr>
            </thead><tbody>';
        while ( $record = sqlsrv_fetch_array($result) )
            {

                $o .= '<tr><td>'.$record ['Name'].'</td><td>'.$record ['Age'].'</td><td>'.$record ['Sex'].'</td></tr>';
            }               
        $o .= '</tbody></table>';

        //result is shown here
        echo $o;

        //delete button form if you want to delete shown data
        $d = '<form method="post" action="DeleteProcess.php">';
        $d .= '<input type="submit" name="Delete" value="Delete">';
        $d .= '</form>';
        echo $d;

        // Include the class definition:
        require('DeleteProcess.php');

        //if delete is hit
        if (isset($_POST['Delete'])){ 

          //Create a new object:
          $i = new DeleteData();

          //Assign the data:
          $i->setData($this->Name);

          //Connect to Database
          $i->connectDatabase();

          //Start deletion
          $i->doDeleteData();
        }
      }

      else{
        echo "No records found";
      }

    //free result set memory
        sqlsrv_free_stmt($result);

    //close the connection
        sqlsrv_close($this->conn);

  }
}

if (isset($_POST['formView'])){ 
  $i = new ReadData();
  $i->connectDatabase();
  $i->doReadData();
}

?>

这是我的DeleteProcess.php代码(删除功能):

<?php

include_once("ReadProcess.php");

class DeleteData extends ReadData{

  public $setName;

  function setData($name = 0) {
         $this->setName = $name;
      }

  public function doDeleteData(){

        //declare the SQL statement that will delete item in the database
        $sql = "DELETE FROM dbo.ProfileTable WHERE Name = '$this->setName'";

        //execute the SQL query and delete records
        $res = sqlsrv_query($this->conn, $sql);

        if (!$res) {
          print("SQL statement failed with error:\n");
          print("   ".sqlsrv_errors()."\n");
        } 
        else {
          $number_of_rows = sqlsrv_rows_affected($res);
          print("$number_of_rows rows deleted.\n");
        }

        //free result set memory
        sqlsrv_free_stmt($res);

        //close the connection
        sqlsrv_close($this->conn);
  }
}

?>

这是我的简单程序的屏幕,其中包含数据选择和删除按钮。

Image screen

点击“删除”按钮时输出是空白页,并且没有删除。 有谁可以帮助我吗?新手在这里编码,谢谢!

0 个答案:

没有答案