kartik
kartik

Reputation: 3

unable to delete data in table using sql

I am trying to delete row from table. Table name is username of user (which user logged in )

delete page is here

require('db.php');
if(isset($_POST["deletebtn"])){
   $x = $_SESSION['username'];
   $id=$_POST["statusid"];
   $row=$conn->query("select id from $x where id='$id'");
   if ($conn->query($row) === TRUE) {
      $sql = "delete from $x where id ='$id'";
      //  echo 'success';
      header( "Refresh:3; url=admin2.php", true, 303);
   }else{
      echo "not";
   }
 }

and here is my content code

$sql = "SELECT id,date,status FROM $x order by id DESC;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
   while($row = $result->fetch_assoc()) {
      $id = $row['id'];
      echo "<div class='border'> ";
      echo "";
      echo "<form action='deletepage.php?action=$id'>";
      echo "<input type='hidden' name='statusid' value='$id' >";
      echo  "<p> " . $row["status"] ."</p><br>";
      echo "<input type='submit' value='delete' class='btn1 pull-right' name='deletebtn'> ";
      echo "<a class=' btn1 pull-right' href='#'>edit &nbsp;&nbsp;</a>";
      echo  "<small> " . $row["date"]. "</small><br>";
      echo "</form>";
      echo "</div>";            
    } 
} else {............}

Upvotes: 1

Views: 149

Answers (4)

reza
reza

Reputation: 1507

There are several problems with your code.

  • there is no query execute for delete query.(which you may be solved later)

  • in your while loop you put $conn->query ($row) which is wrong. $row is not a query... It is the result set return from previous query.

  • and lastly $conn->query does not return true in SELECT, SHOW, DESCRIBE or EXPLAIN query. Check php manual

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

You forgot to execute query

require'db.php';
if(isset($_POST["deletebtn"])){
$x = $_SESSION['username'];
$id=$_POST["statusid"];
 $row="select id from $x where id='$id'"; //<---remove query execution from here
 if ($conn->query($row) === TRUE) {
   $sql = "delete from $x where id ='$id'";
    $conn->query($sql); //<-------- Add this line
 //  echo 'success';
   header( "Refresh:3; url=admin2.php", true, 303);
  }else{
 echo "not";
 }
 }

Upvotes: 2

Obodugo Prince
Obodugo Prince

Reputation: 1

Change this $sql = "delete from $x where id ='$id To$sql = "delete from $table where id ='$sessionid'

Upvotes: -1

user3526204
user3526204

Reputation: 509

You are not executing query on the sql

$sql = "delete from $x where id ='$id'";

Execute it like

$del= $conn->query($sql);

After the query

Upvotes: 5

Related Questions