Closed Thread
Results 1 to 3 of 3

Thread: PHP Delete onClick

  1. #1
    Alex_j is offline Newbie
    Join Date
    Nov 2009
    Posts
    29
    Rep Power
    0

    PHP Delete onClick

    Hi, I want to be able to delete a record in a MySQL table, by just clicking on a delete link or by using a checkbox system for multiple rows. How would I go about doing this?

    i have this code so far, would I have to put the code for the delete in a separate PHP file?

    Code:
    while($row mysql_fetch_array($result)) 
      { 
      echo 
    "<tr>"
      echo 
    "<td>" $row['userID'] . "</td>"
      echo 
    "<td>" $row['FirstName'] . "</td>"
      echo 
    "<td>" $row['Surname'] . "</td>"
      echo 
    "<td>" $row['Email'] . "</td>"
      echo 
    "<td>" "<a href='delete.php'>Delete</a>" "</td>"
      echo 
    "</tr>"
      } 
    echo 
    "</table>"

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Bioshox is offline Programmer
    Join Date
    Oct 2009
    Location
    Manchester, UK
    Posts
    196
    Rep Power
    10

    Re: PHP Delete onClick

    Hey there, I have a simlar piece of code that you wish, but I use it to delete members and entries on my CMS system, here it is;

    Code:
    <?php

    function get_content($id '') {

        
    //Gets the ID number from the SQL Database
            
    $id mysql_real_escape_string($id);
            
    $sql "SELECT * FROM cms";
            
    $res mysql_query($sql) or die(mysql_error());
            while(
    $row mysql_fetch_assoc($res)) {
            
    //Makes members names into links
            
    echo "<h3>" $row['title'] . " <br> <a href='?delete=" .  $row['id']  . "'>Delete Post</a></h3><br>";
            
        }
        
        } 
    //Ends Our Class

    //Displays the posts
    ?>


    <?php


    function delete_content($id '') {
            
            if (!
    $id) {
            return 
    false;
            }else {
        
    //Gets the ID number from the SQL Database
            
    $id mysql_real_escape_string($id);
            
    $sql "DELETE FROM cms WHERE id = '$id'";
            
    $res mysql_query($sql) or die(mysql_error());

            echo 
    "<h3>Content Deleted</h3>";
            
        }
        
        } 
    //Ends Our Class
    ?>

            <?php
                    
    if(isset($_GET['delete'])):
                    
    delete_content($_GET['delete']);
                    else: 
                    
                    
    //If a post hasent been selected, display everything
                    
    get_content();
                    endif;
            
    ?>

  4. #3
    Jaan Guest

    Re: PHP Delete onClick

    Just do something like this:

    Code:
    while($row mysql_fetch_array($result)) 
      { 
      echo 
    "<tr>"
      echo 
    "<td>" $row['userID'] . "</td>"
      echo 
    "<td>" $row['FirstName'] . "</td>"
      echo 
    "<td>" $row['Surname'] . "</td>"
      echo 
    "<td>" $row['Email'] . "</td>"
      echo 
    "<td>" "<a href='delete.php?id=".$row['userID']."'>Delete</a>" "</td>"
      echo 
    "</tr>"
      } 
    echo 
    "</table>"
    And then in delete.php file just remove a record that has this "userID" that you will take from your url like this:

    Code:

    if($_GET['id'] != ""){

    $userID $_GET['id'];

    $sql "DELETE FROM table_name WHERE userID='".$userID."'";
    $query  mysql_query($sql);



Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Preventing onclick() inheritance
    By RHochstenbach in forum JavaScript and CSS
    Replies: 0
    Last Post: 06-02-2011, 03:34 AM
  2. Copy href onClick
    By rsnider19 in forum JavaScript and CSS
    Replies: 1
    Last Post: 11-15-2010, 04:02 PM
  3. Replies: 5
    Last Post: 11-06-2010, 05:10 AM
  4. onClick
    By ezcat in forum PHP Development
    Replies: 2
    Last Post: 01-17-2009, 01:40 PM
  5. prompt to to appear after onclick
    By freddyw2 in forum JavaScript and CSS
    Replies: 19
    Last Post: 11-21-2008, 10:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts