0

I have a problem deleting a queried data from my MSSQL database. What my program does is that you have the option to look for a specific data then if you desire to delete that chosen data you just have to hit the delete button. I have success in showing the queried data but my delete function always fails.

here's my ReadProcess.php code (query function):

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

?>

And here's my DeleteProcess.php code (delete function):

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

?>

Here's the screen of my simple program with a data selected and delete button.

Image screen.

The output is a blank page when hitting the Delete button and no deletion is being done. can someone help me please? newbie in coding here, Thanks!

8
  • Just a Quick Check: Check whether the data is being deleted in DB table after you press the delete button. select * from dbo.ProfileTable where Name = 'Celina'. If not you need to concentrate bit more on delete operation. Jun 16, 2014 at 12:05
  • The data Name = Celina still exist as I query it from the PHP code that I've created and also exist in the MSSQL database when I double checked it. Jun 16, 2014 at 12:13
  • @knkarthick24 what do you think is wrong, I'm stuck with this code.. cant move on with this problem.. help please? Jun 16, 2014 at 12:32
  • print this string $sql = "DELETE FROM dbo.ProfileTable WHERE Name = '$this->setName'"; and make sure you are escaping the variable properly and that the SQL doesn't say "DELETE FROM dbo.ProfileTable WHERE Name = '$this->setName'" It wouldn't throw an error, but it also would not delete anything.
    – Daniel E.
    Jun 16, 2014 at 12:41
  • I dont do much PHP but this makes more sense to me. $sql = "DELETE FROM dbo.ProfileTable WHERE Name = '"$this->setName"'";
    – Daniel E.
    Jun 16, 2014 at 12:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.