Jump to content

Html button delete row from mysql doesn't work


unknown00

Recommended Posts

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?

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.

	<?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)

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.

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.";
}

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.