unknown00 Posted July 11, 2011 Share Posted July 11, 2011 for my button <form action="index.php" method="post" name="add" id="add" enctype="multipart/form-data"> <input name="name" id="name" type="text" size="30"/> <input type="submit" value="Add"/> <input id="delbutton" name="delbutton" type="submit" value="Delete"> </form> then to delete <?php if(isset($_POST['delbutton'])) { $result = mysql_query("DELETE FROM 'locations' WHERE id='2'"); } ?> i also have an add that works, this code is above my delete code. no idea why the delete doesn't work <?php // adding a new location to the db if (isset($_POST['name'])) { $name = $_POST['name']; $result = mysql_query("insert into locations(`name`) values ('$name')"); } ?> i'm not even passing any parameters yet because i want to test just a simple delete command works but it doesn't work. suggestions? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted July 11, 2011 Share Posted July 11, 2011 $result = mysql_query("DELETE FROM locations WHERE id='2'"); Quote Link to comment Share on other sites More sharing options...
unknown00 Posted July 11, 2011 Author Share Posted July 11, 2011 $result = mysql_query("DELETE FROM locations WHERE id='2'"); nope, i actually originally had locations with the '' an added them to try. doesn't work Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted July 11, 2011 Share Posted July 11, 2011 did you put or die(mysql_error()); after that see if it errors out? Quote Link to comment Share on other sites More sharing options...
unknown00 Posted July 11, 2011 Author Share Posted July 11, 2011 did you put or die(mysql_error()); after that see if it errors out? yes, it never error out. seems like almost if(isset($_POST['delbutton'])) is always false Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted July 11, 2011 Share Posted July 11, 2011 if(isset($_POST['delbutton'])=='Delete') { $result = mysql_query("DELETE FROM 'locations' WHERE id='2'"); } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 11, 2011 Share Posted July 11, 2011 Do you actually have an id with the value 2 in the table? Add echo statements (echo 'you are here' to your code so that you can tell if the block of code where the mysql_query() statement is at is actually being executed. Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 11, 2011 Share Posted July 11, 2011 You could also print_r you Post variable to see if the command is coming through. echo '<pre>' . print_r($_POST,true) . '</pre>'; Quote Link to comment Share on other sites More sharing options...
unknown00 Posted July 11, 2011 Author Share Posted July 11, 2011 <?php if(isset($_POST['delbutton'])=='Delete') { echo '<pre>' . print_r($_POST,true) . '</pre>'; $delete = mysql_query("DELETE FROM locations WHERE id=2") or die(mysql_error()); } ?> still nothing, it seems like the if statement is always false. and yes there id=2 actually exists id is a type int(11) Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 I'm guessing you id is a primary key auto increment meaning your database will only ever have 2 as the id one time. Your database probably doesn't have a field with id 2 in it Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 That if statement will always evaluate false isset is a boolean return... id post an example but I'm on my phone Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 If (isset($_POST['delbutton'])) is enough by itself Quote Link to comment Share on other sites More sharing options...
unknown00 Posted July 11, 2011 Author Share Posted July 11, 2011 yes my id is an auto increment but i just changed my delete to $delete = mysql_query("DELETE FROM locations WHERE name='test'") or die(mysql_error()); to get rid of the id and it still doesn't work Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 11, 2011 Share Posted July 11, 2011 You could also print_r you Post variable to see if the command is coming through. echo '<pre>' . print_r($_POST,true) . '</pre>'; Did you do this? It's probably the best way to tell what's going on with the form data right now. Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 Please post all of your code as you are using it now (minus mysql account details of course). Quote Link to comment Share on other sites More sharing options...
unknown00 Posted July 11, 2011 Author Share Posted July 11, 2011 view source http://studydojo.com/maps/travel/ as for php <?php // adding a new location to the db if (isset($_POST['name'])) { $name = $_POST['name']; $description = $_POST['description']; $user_name = $_POST['user_name']; $user_location = $_POST['user_location']; $lat = $_POST['lat']; $long = $_POST['long']; $address = $_POST['address']; // generate id $id = 1; $result = mysql_query("select max(id) from locations"); if (mysql_num_rows($result) != 0) { $row = mysql_fetch_array($result); $id = $row['max(id)']+1; } // sql query to add location $result = mysql_query("insert into locations (`id`, `name`, `latitude`, `longitude`, `address`, `description`, `user_name`, `user_location`) values ('$id','$name', '$lat', '$long', '$address', '$description', '$user_name', '$user_location')"); } if(isset($_POST['delbutton'])=='Delete') { //echo "tset<br />"; //echo '<pre>' . print_r($_POST,true) . '</pre>'; //$result = die("A MySQL error has occurred." . mysql_error()); $delete = mysql_query("DELETE FROM locations WHERE locations.id=2") or die(mysql_error()); //echo "Account has been deleted."; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 11, 2011 Share Posted July 11, 2011 You could also print_r you Post variable to see if the command is coming through. echo '<pre>' . print_r($_POST,true) . '</pre>'; Did you do this? It's probably the best way to tell what's going on with the form data right now. I guess I should clarify. Put that outside of any conditional statements. We need to know what's in the $_POST array when the form is submitted, and if it's in a conditional that isn't being executed, it's useless. Quote Link to comment Share on other sites More sharing options...
phpjay Posted July 11, 2011 Share Posted July 11, 2011 should check also the table name is correct.. and be sure you also connection from your database Quote Link to comment Share on other sites More sharing options...
unknown00 Posted July 11, 2011 Author Share Posted July 11, 2011 should check also the table name is correct.. and be sure you also connection from your database yup all working. i posted my code above anyone take a look? Quote Link to comment Share on other sites More sharing options...
teynon Posted July 11, 2011 Share Posted July 11, 2011 THe code you posted has the delete button laying outside the <form> </form> You need to put it inside a form for it to submit. Quote Link to comment Share on other sites More sharing options...
unknown00 Posted July 11, 2011 Author Share Posted July 11, 2011 THe code you posted has the delete button laying outside the <form> </form> You need to put it inside a form for it to submit. BINGOOOOOOOOOOO Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.