1

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 {............}

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

Comments

2

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";
 }
 }

1 Comment

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

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

Comments

-1

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

2 Comments

Like he said - table name is username, so using $x variable is ok. There are no $table and $sessdionid variables, your answer is useless.
@kartik you have the solution on the top in the answer with most votes.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.