2

My Update.php file:

<?php ob_start();
 session_start();


//declare the basic variables
$servername = "localhost";
$username = "bilal";
$password = "super";
$dbname = "coursework";

 //create connection
 $link =  mysqli_connect($servername, $username, $password, $dbname);


//check connection
if($link->connect_error){
die ("connection failed: " . $link->connect_error);
}

//Security purpose, handiling escape string
  // $crested_by = $_POST ['created_by'];
  $title = $_POST['title'];
  $catagory = $_POST['catagory'];
  $contact = $_POST['contact'];
  $comment = $_POST ['description'];
   $ses = $_SESSION['email'];
  $date = date('Y-m-d');
  $availability = $_POST ['availability'];
  $price = $_POST ['price'];
  $id = $_POST['wow'];
 // $created_by_id = $_SESSION['created_by_id'];

 // $username = $_SESSION['firstname'];
 if (isset($_POST['del'])){
  $deletequery=mysqli_query($link,"DELETE FROM new_post WHERE id='$id'");
  header ("Location: added_posts.php");
   }

    else if(isset($_POST['update'])){

   $sql = "UPDATE new_post SET title='$title', contact='$contact',        availability='$availability', price='$price', comment='$comment' WHERE  id='$id'";
 if (mysqli_query($link, $sql)){
 echo "awesoke";
  header("Location:added_posts.php");
  }else{
   echo "Error: Sign up Unsuccessfull";
}  
}

$link->close();
 ?>

This is my update.php file I am showing the id inside as textbox and the doing where id = the text field value. It is deleting but the recently added item deletes like if i have 3 posts id like ( 4, 5, 6) id delete the 6 first then 5 then 4. I am displaying data in bootstrap cards. but every delete button under post it delete the recent one which is recently added.

Nere
  • 4,097
  • 5
  • 31
  • 71
bilal
  • 79
  • 1
  • 1
  • 9
  • print value of `$id` before deleting and check that you are passing proper ID value. – sandeepsure Nov 01 '15 at 12:42
  • what is `$_POST['wow']` and how is it gotten? do u really need to show the id's inside text boxes, it can be a hidden post field or you can pass the value through the url – danidee Nov 01 '15 at 12:58
  • well i am showing the id of the post in a textfield to make sure that it is correct! and the name of the textfield in wow. so i ust added it as a post and make it equals to id. but still it is deleting the recent post like. first 3rd then if i press again then 2nd and even if i want to delete 1st and click delete from the first card then it deletes the 3rd – bilal Nov 01 '15 at 13:55
  • this is my button: ( ) – bilal Nov 01 '15 at 13:57

1 Answers1

2

So you're trying to delete the most recent record in your database?

Your problem is the sql statement you have

"DELETE FROM new_post WHERE id='$id'"

This is deleting the record in new_post where the id is equal to the id posted from your form.

You must consider that the latest entry in the table will have the largest id.

So use something like this:

DELETE FROM new_post ORDER BY id DESC LIMIT 1

Attempt 2:

You want to delete a specific row based on the id?

I suggest you use prepared statements for security

Firstly try this:

$stmt = $link->prepare("DELETE FROM new_post WHERE id= ?");
$stmt->bind_param('i', $_POST['wow']); // can also use $id 
$stmt->execute(); 
$stmt->close();
Community
  • 1
  • 1
  • No No No!! i am just trying to delete a specific record. like i have made cards and in all cards there is details and every card have delete button. but if i press delete from any card the most recent post delete then if i press agaian the second recent gets delete. – bilal Nov 01 '15 at 13:56
  • Okay well you need to make sure your post variables are being assigned properly, echo them out so echo $id –  Nov 01 '15 at 14:31
  • echo is perfectly correct! i am showing id ina text field and then the name of the textfield i am using in query like where id= "the name of the text field" – bilal Nov 01 '15 at 14:36
  • Then I see nothing wrong with your query "DELETE FROM table_name WHERE id='$id'" –  Nov 01 '15 at 15:01
  • yup! yup i did! it was method problem in my form – bilal Nov 04 '15 at 11:16
  • Well done, close the question. So either accept or create an answer @bilal –  Nov 04 '15 at 12:18