12 Replies - 3308 Views - Last Post: 10 July 2011 - 09:28 PM Rate Topic: -----

#1 unknown00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-July 11

Html button delete row from mysql doesn't work

Posted 10 July 2011 - 06:51 PM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Html button delete row from mysql doesn't work

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 06:55 PM

Have you got an active database connection when the mysql_query runs?
Was This Post Helpful? 0
  • +
  • -

#3 unknown00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-July 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 06:59 PM

View Poste_i_pi, on 10 July 2011 - 06:55 PM, said:

Have you got an active database connection when the mysql_query runs?

yes i believe so. i have include "connect.php"; statement and connect.php has
$connId = mysql_connect($server,$username,$password) or die("Cannot connect to server");
$selectDb = mysql_select_db($database,$connId) or die("Cannot connect to database");


again, add works. i don't know why delete doesn't
Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 07:21 PM

You're passing ID as a string instead of an int, that could be the problem.
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 07:25 PM

View Postunknown00, on 10 July 2011 - 09:51 PM, said:

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?

I'm not even sure what you mean by this statement but if you don't pass anything then isset($_POST['delbutton']) will always return false, thus skipping over your query.

I'm assuming id is an integer so it should not be in quotes. Only strings are to be in quotes.

Your query also makes your database vulnerable to SQL injections. Either use mysql_real_escape_string or Prepared Statements from MySQLi or PDO.
Was This Post Helpful? 1
  • +
  • -

#6 unknown00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-July 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 07:31 PM

View Poste_i_pi, on 10 July 2011 - 07:21 PM, said:

You're passing ID as a string instead of an int, that could be the problem.

got rid of the '' around int, doesn't work

View Postcodeprada, on 10 July 2011 - 07:25 PM, said:

View Postunknown00, on 10 July 2011 - 09:51 PM, said:

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?

I'm not even sure what you mean by this statement but if you don't pass anything then isset($_POST['delbutton']) will always return false, thus skipping over your query.

I'm assuming id is an integer so it should not be in quotes. Only strings are to be in quotes.

Your query also makes your database vulnerable to SQL injections. Either use mysql_real_escape_string or Prepared Statements from MySQLi or PDO.

what i meant by not passing anything is that id=2 is just something i put for testing purposes. if it works it's going to be id=XXXXXXXXX. isn't isset($_POST['delbutton']) true when button is clicked?
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 08:02 PM

Yes it will be. Let's see your connect.php script.
Was This Post Helpful? 0
  • +
  • -

#8 unknown00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-July 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 08:04 PM

<?php
$server = "localhost";
$username = "user";
$password = "pass";
$database = "database";
$connId = mysql_connect($server,$username,$password) or die("Cannot connect to server");
$selectDb = mysql_select_db($database,$connId) or die("Cannot connect to database");
?>

Was This Post Helpful? 0
  • +
  • -

#9 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 08:51 PM

You're missing a closing tag on your delete button:
<input id="delbutton" name="delbutton" type="submit" value="Delete">


...should be...
<input id="delbutton" name="delbutton" type="submit" value="Delete"/>


See if that helps
Was This Post Helpful? 0
  • +
  • -

#10 unknown00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-July 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 09:00 PM

didn't work. here is my site and code
http://studydojo.com/maps/travel/
php code
	<?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 name='nyc'") or die(mysql_error());
		//echo "Account has been deleted.";
	}
	
	?>

Was This Post Helpful? 0
  • +
  • -

#11 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 09:06 PM

Your query on Line #26 may be the problem. Is your id auto increment? If so then you should either just specify NULL and let the database do the auto incrementing or don't specify any value at all.
INSERT INTO `table` VALUES(NULL, 'name', 'whatever');
INSERT INTO `table` (`name`, `whatever_column`) VALUES('name', 'whatever')

Was This Post Helpful? 0
  • +
  • -

#12 unknown00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-July 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 09:09 PM

View Postcodeprada, on 10 July 2011 - 09:06 PM, said:

Your query on Line #26 may be the problem. Is your id auto increment? If so then you should either just specify NULL and let the database do the auto incrementing or don't specify any value at all.
INSERT INTO `table` VALUES(NULL, 'name', 'whatever');
INSERT INTO `table` (`name`, `whatever_column`) VALUES('name', 'whatever')

i know i changed it to match a string instead of id but still doesn't work
Was This Post Helpful? 0
  • +
  • -

#13 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Html button delete row from mysql doesn't work

Posted 10 July 2011 - 09:28 PM

To avoid all of this set your id to an INT type with AUTO_INCREMENT so that you don't have to worry about adding one to your id. I can assure you the query will work just fine if you do it right.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1