Author Topic: Removing from the DB...  (Read 322 times)

bugz

  • Guest
Removing from the DB...
« on: July 28, 2010, 04:50:36 PM »
This is my first project with PHP and till this point I can't say I've done horribly bad. How would I go about passing a variable to a custom function through a link? I'll post my code so you can see what I mean. Also, if you see anything that's an amateur thing to do let me know, the only way I can better myself is with criticism.
The link inside the table should send the ID to the remove_from_DB(). When I tested it, it removed everything in the DB.


PHP Code: [Select]

<?php 
        
//setting DB variables
        
$host "localhost";
        
$user "xxxxx";
        
$pass "xxxxx";
        
$dbase "test";


        
$conn mysql_connect($host,$user,$pass) or die("Could not connect");        
        
$db mysql_select_db($dbase);


        
$query mysql_query("SELECT * FROM phones");    

        echo 
"<table border='1' cellpadding='1' cellspacing='1'>";
        echo 
"<tr><td>ID</td><td>Name</td><td>Phone</td><td>Notes</td></tr>";

        while( 
$row mysql_fetch_array($query)){
            echo 
"<tr><td>ID: " .$row['ID'].  "</td><td>" .$row['name']. "</td><td> " .$row['phone']. "</td><td>" .stripslashes($row['notes']). "</td><td><a href='" .remove_from_DB($row['ID']). "'>Remove?</a></td></tr>";
    
        }    
        
        echo 
"</table>";

?>



<?php
        
function remove_from_DB(){
            echo 
$id;
            
//setting DB variables
            
$host "localhost";
            
$user "xxxxx";
            
$pass "xxxxx";
            
$dbase "test";
            
$sql "DELETE FROM phones WHERE ID=$id";
            
            
//connecting to & setting the DB
            
$conn mysql_connect($host,$user,$pass) or die("Could not connect");        
            
$db mysql_select_db($dbase);    
        
            
//removing data
            
mysql_query($sql);
            
mysql_close($conn);
        }
?>    

PHP Coder

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 130
  • Karma: +0/-0
    • View Profile
    • PHP coder
Re: Removing from the DB...
« Reply #1 on: August 18, 2010, 02:46:11 AM »
You need to link to a php script, you cannot link to function. Have your link constructed like this:

PHP Code: [Select]
<a href='remove.php?id=".$row['ID']. "'>Remove</a>


and then, create file named remove.php and put there this code:

PHP Code: [Select]
<?php

$id 
= (int)$_GET['id'];

// delete record from db
remove_from_DB($id);

// redirect back to main page
header('Location: index.php');
exit;

function 
remove_from_DB($id){
  
//setting DB variables
  
$host "localhost";
  
$user "xxxxx";
  
$pass "xxxxx";
  
$dbase "test";
  
$sql "DELETE FROM phones WHERE ID=$id";
            
  
//connecting to & setting the DB
  
$conn mysql_connect($host,$user,$pass) or die("Could not connect");        
  
$db mysql_select_db($dbase);    
        
  
//removing data
  
mysql_query($sql);
  
mysql_close($conn);
}
?>
PHP coder for hire