Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete button which deletes database row

Tags:

function

php

I just made a php function

function deletebooking($orderID){

    $sql="DELETE FROM bs_reservations WHERE id='".$orderID."'";
    $result=mysql_query($sql) or die("oopsy, error when tryin to delete events 2");

}

I have a .php file which process a submitted form and then displays a HTML page. How do i add a delete booking button which calls the above function to the HTML page. The $orderID variable is already set.

Suggestions Welcome

like image 376
Simon Avatar asked Dec 22 '22 14:12

Simon


2 Answers

You can use a javascript code on the button which you click to delete the record as

<input type="button" name-"abc" id="abc" onclick="return Deleteqry(<?php echo $orderID ?>);"/>

Now you can write the javascript function inside the <head></head> as

function Deleteqry(id)
{ 
  if(confirm("Are you sure you want to delete this row?")==true)
           window.location="yourpagename.php?del="+id;
    return false;
}

Now you can write on the top of your page as

 if($_GET['del'])
   {
    deletebooking($orderID);

   }    

Here we are using a message alert for the conformation of the deletion using the javascript alert box...

like image 168
Varada Avatar answered Dec 24 '22 03:12

Varada


Two things. First, look up "SQL injection." Sanitize the input of your query.

Second: don't do this. It's not a good idea. If the orderID is already set on the page, then presumably it can be changed by the user to anything they want and then deleted. Instead, you should associate the orderID deleting with authentication, or have a status for order IDs instead ("Deleted," for instance) that prevents loss of any data.

Other than that, the other answer will do what you need. This is not complicated.

like image 23
Explosion Pills Avatar answered Dec 24 '22 03:12

Explosion Pills