943,097 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7791
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 11th, 2010
0

delete row from mysql using HTML button

Expand Post »
I have been working on this a while and have tried just about way trying to delete a row from a database using a button. What I have is a list of websites and data about each on each row. Then next to the row is a Delete button. I just want it to delete that one row and refresh the page when clicked. I already have the table made but when I click the button, it just refreshes the page but nothing happens.
I have tried different things but here is what I have now. I know it is terribly wrong.
PHP Syntax (Toggle Plain Text)
  1. echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
  2.  
  3. echo '<input type="submit" name="delete" value="Delete ">';
PHP Syntax (Toggle Plain Text)
  1. break;
  2.  
  3. case 'delete';
  4. {
  5. $sql = "DELETE FROM UserSites WHERE 'SiteNumber'=$SiteNumber";
  6. $result = mysql_query($sql);
  7. echo "Site deleted!";
  8. }

I know I need to include SiteNumber somewhere in my button code. Any help would be greatly appreciated!
Similar Threads
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
PHP Syntax (Toggle Plain Text)
  1. echo '<form action="'.$_SERVER['PHP_SELF'].'"sitenumber="'.$SiteNumber(site number from database).'" method="post">';
.
.
..
..

PHP Syntax (Toggle Plain Text)
  1. case 'delete';
  2.  
  3. {
  4.  
  5. $sql = "DELETE FROM UserSites WHERE 'SiteNumber'=$_Request[sitenumber]";
  6.  
  7. $result = mysql_query($sql);
  8.  
  9. echo "Site deleted!";
  10.  
  11. }


i am not sure this will work or not . try it once.
Last edited by rajabhaskar525; Jan 11th, 2010 at 1:16 am.
Reputation Points: 12
Solved Threads: 27
Junior Poster
rajabhaskar525 is offline Offline
179 posts
since Nov 2009
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
Thanks, but no luck. Still does the exact same thing.
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
What's between the parenthesis of the switch statement? I think what you are looking for is:
PHP Syntax (Toggle Plain Text)
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Input Name Test</title>
  5. </head>
  6. <body>
  7. <?php
  8. if(isset($_POST['submit']))
  9. {
  10. switch($_POST['submit'])
  11. {
  12. case "Delete":
  13. mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber']);
  14. echo 'Site Deleted!<br>'."\n";
  15. break;
  16. case "Other Action":
  17. //Other actions can be added with submit buttons with the name submit and a value that corresponds to a case in this switch
  18. break;
  19. }
  20. echo '<a href="'.$SERVER['PHP_SELF'].'">Go Back</a><br>'."\n";
  21. }
  22. else
  23. {
  24. echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
  25. echo '<label for="SiteNumber">Site Number:</label> <input type="text" name="SiteNumber"><br>'."\n";
  26. echo '<input type="submit" name="submit" value="Delete"><br>'."\n";
  27. echo '<input type="submit" name="submit" value="Other Action"><br>'."\n";
  28. echo '</form>'."\n";
  29. }
  30. ?>
  31. </body>
  32. </html>
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
PHP Syntax (Toggle Plain Text)
  1. <?
  2. ob_start();
  3. extract($_POST);
  4. extract($_REQUEST);
  5. if($_POST['Delete']=='Delete')
  6. {
  7. echo $_REQUEST['sitenumer'];exit;
  8. }
  9. ?>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  14. <title>Untitled Document</title>
  15. </head>
  16.  
  17. <body>
  18.  
  19. <?
  20. $a="?sitenumer=raju";
  21. echo '<form action="'.$_SERVER['PHP_SELF'].$a.'" method="post" name="formx">';
  22.  
  23. echo '<input type="submit" name="Delete" value="Delete">';
  24. echo '</form>';?>
  25. </body>
  26. </html>
. i am trying to get sitenumber value. i got sitenumber value. modify above code as per your need.
Last edited by rajabhaskar525; Jan 11th, 2010 at 2:06 am.
Reputation Points: 12
Solved Threads: 27
Junior Poster
rajabhaskar525 is offline Offline
179 posts
since Nov 2009
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
PHP Syntax (Toggle Plain Text)
  1. <?
  2. ob_start();
  3. extract($_POST);
  4. extract($_REQUEST);
  5. if($_POST['Delete']=='Delete')
  6. {
  7. echo $_REQUEST['sitenumer'];exit;
  8. }
  9. ?>
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  14. <title>Untitled Document</title>
  15. </head>
  16.  
  17. <body>
  18.  
  19. <?
  20. $a="?sitenumer=raju";
  21. echo '<form action="'.$_SERVER['PHP_SELF'].$a.'" method="post" name="formx">';
  22.  
  23. echo '<input type="submit" name="Delete" value="Delete">';
  24. echo '</form>';?>
  25. </body>
  26. </html>
. i am trying to get sitenumber value. i got sitenumber value. modify above code as per your need.
But does it execute the Delete query? Also I believe you meant exit() . And what is the point of the exract() 's? You don't seem to use them anywhere (I would be especially careful with extracting $_GET and $_POST variables as it could pose a major security vulnerability!)
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
PHP Syntax (Toggle Plain Text)
  1. echo $_REQUEST['sitenumer'];exit;
comment this one and write delete query definetely excute i am sure about it. if you want know about extract. see this link
Reputation Points: 12
Solved Threads: 27
Junior Poster
rajabhaskar525 is offline Offline
179 posts
since Nov 2009
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
I fully understand what extract is. Regardless, it poses a security flaw to extract $_GET and $_POST variables. The user would have free roam to set a variable, such as maybe a database to delete, a boolean that determines whether the user is an admin or not, or any other crucial value, to any value including ones that could compromise the script. Extracting $_GET, $_POST, or $_REQUEST is an idea with terrible consequences. I would advise against it.
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
thanks for your info. the above code works fine without .
PHP Syntax (Toggle Plain Text)
  1. extract($_POST);
  2. extract($_REQUEST);
functions. remove them and see is it works or not.
Reputation Points: 12
Solved Threads: 27
Junior Poster
rajabhaskar525 is offline Offline
179 posts
since Nov 2009
Jan 11th, 2010
0
Re: delete row from mysql using HTML button
If short tags aren't enabled, then the code won't work. It also doesn't seem that you have mentioned the sitenumber problem in a query. I would still suggest:
PHP Syntax (Toggle Plain Text)
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Input Name Test</title>
  5. </head>
  6. <body>
  7. <?php
  8. if(isset($_POST['submit']))
  9. {
  10. switch($_POST['submit'])
  11. {
  12. case "Delete":
  13. mysql_query("DELETE FROM UserSites WHERE SiteNumber=".mysql_real_escape_string($_POST['SiteNumber']);
  14. echo 'Site Deleted!<br>'."\n";
  15. break;
  16. case "Other Action":
  17. //Other actions can be added with submit buttons with the name submit and a value that corresponds to a case in this switch
  18. break;
  19. }
  20. echo '<a href="'.$SERVER['PHP_SELF'].'">Go Back</a><br>'."\n";
  21. }
  22. else
  23. {
  24. echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
  25. echo '<label for="SiteNumber">Site Number:</label> <input type="text" name="SiteNumber"><br>'."\n";
  26. echo '<input type="submit" name="submit" value="Delete"><br>'."\n";
  27. echo '<input type="submit" name="submit" value="Other Action"><br>'."\n";
  28. echo '</form>'."\n";
  29. }
  30. ?>
  31. </body>
  32. </html>
Reputation Points: 47
Solved Threads: 47
Posting Whiz
FlashCreations is offline Offline
393 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php send email based on records from two tables
Next Thread in PHP Forum Timeline: Registration errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC