SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with button click to delete data from database

    i want my code to delete data from the database when the button is clicked on, but the below code does not work, someone please help me.

    <form name="form2" action="filename.php" method="post" id="form2"><input type="image" name="submit2" src="imagename.gif"/>


    database connection code.....

    if(isset($_POST['submit2']))
    {

    $result1 = mysql_query('delete from staff');

    if($result1.mysql_affected_rows() >0)
    { ?>
    <SCRIPT type="text/javascript">window.open('http://localhost/name.html',"sremoved","width=250,height=150");</SCRIPT>
    <?php }
    }

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Usually your delete query would specify what rows to delete:

    PHP Code:
    $result1 mysql_query("DELETE FROM staff WHERE staff_id = 1"); 
    Your if statement also has syntax error, try replacing with:

    PHP Code:
    <?php
    if (!$result) die("Database error: " mysql_error());
    if(
    mysql_affected_rows() > 0)
    ?>
    <SCRIPT type="text/javascript">window.open('http://localhost/name.html',"sremoved","width=250,height=150");</SCRIPT>
    <?php }
    }
    else
    {
      echo 
    "Zero records deleted";
    }

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print_r($_POST);

    Browsers vary in what variables they send for different types of submit buttons.

    This could work:
    PHP Code:
    if (isset($_POST['submit2_x')) {


    You could alternatively use a hidden input field and check for its presence in post, or just test for some other non-optional field in the form(like a type=text input)

  4. #4
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    STeeL_LT, im trying to delete the whole data in the table not just 1 row.

    crmalibu, thanks its worked


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
  •