Afaq Ahmad Afaq Ahmad - 3 months ago 21
MySQL Question

Delete query not working in php/MYSQL

I have wrote a PHP script just to delete a single row from a MySQL DB by id. The value of id has taken from the

$_POST
of a button when clicked i tried different ways to pass that variable having the
$_POST
value but it doesn't worked. every thing seems fine i tried the same query with static value in the DB and that worked fine there. here is my complete code.
Note: i also have tried
$_GET
and also tried '.
$delete_id
.' and '
$delete_id
'

include ("db_conection.php");

if (isset ( $_GET ['view'] )) {
$view_id = $_GET ['view'];

$query_run = null;

$query1 = "SELECT * FROM question where testid ='" . $view_id . "'";
$run = mysqli_query ( $conn, $query1 ); // here run the sql query.
while ( $row = mysqli_fetch_array ( $run ) ) // while look to fetch the result
// and store in a array $row.
{

echo '

<tr>
<td>' . $row ['qid'] . '</td>
<td>' . $row ['testid'] . '</td>
<td>' . $row ['questions'] . '</td>
<td>
<a href="remove_question.php?del=' . $row ['qid'] . '" class = "btn btn-danger btn-block">Delete</a>

</td>
</tr>

';

}

if (isset ( $_POST ['del'] )) {
$delete_id = $_POST ['del'];
$delete_query = "Delete from question WHERE qid='".$delete_id."'"; // delete
// query

// $run=mysqli_query($conn,$delete_query);
$run = $conn->query ( $delete_query );
if ($run) {
// bootstrap class to open in the same window
/*
* echo "<script>window.open('Admin Delete
* member.php','_self')</script>";
*/

echo "<script>window.open('remove_question.php','_self')</script>";

echo '

<div align = "center" style = "margin-top:10px;" class = "alert alert-success">deleted...</div>

';
} else {

echo "deletion field";
}

}

}

Answer

Just write your delete query outside of if block like below.

include ("db_conection.php");

if (isset ( $_GET ['view'] )) {
    $view_id = $_GET ['view'];

    $query_run = null;

    $query1 = "SELECT * FROM question where testid ='" . $view_id . "'";
    $run = mysqli_query ( $conn, $query1 ); // here run the sql query.
    while ( $row = mysqli_fetch_array ( $run ) )
    {
        echo ' <tr>  <td>' . $row ['qid'] . '</td>  <td>' . $row 'testid'] . '</td>  <td>' . $row ['questions'] . '</td>  <td>  <a href="remove_question.php?del=' . $row ['qid'] . '" class = "btn btn-danger btn-block">Delete</a>  </td>  </tr>    ';
    }
}
if (isset ( $_GET ['del'] )) {
    $delete_id = $_GET ['del'];
    $delete_query = "Delete  from question WHERE qid='".$delete_id."'"; // delete
                                                                    // query

    // $run=mysqli_query($conn,$delete_query);
    $run = $conn->query ( $delete_query );
    if ($run) {
        // bootstrap class to open in the same window
        /*
         * echo "<script>window.open('Admin Delete
         * member.php','_self')</script>";
         */

        echo "<script>window.open('remove_question.php','_self')</script>";

        echo '<div align = "center" style = "margin-top:10px;" class = "alert alert-success">deleted...</div>';
    } else {
        echo "deletion field";
    }

}