User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,567 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,595 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 916 | Replies: 5 | Solved
Reply
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Deleting an item from a database by selecting it from a drop-down menu

  #1  
Oct 23rd, 2007
Hi

I would like to know if it's possible to delete a row from a database by selecting it from a drop-down list and hitting the submit("Delete") button.

If it is could you help me out with a bit of "code and description" or a link please.

Much appreciated
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Location: Budapest
Posts: 252
Reputation: fatihpiristine has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 14
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: Deleting an item from a database by selecting it from a drop-down menu

  #2  
Oct 23rd, 2007
post the selected item's value and request it...
Do a favour, leave me alone
Reply With Quote  
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Re: Deleting an item from a database by selecting it from a drop-down menu

  #3  
Oct 24th, 2007
This is what I have so far but it doesn't work.

This code is at the very top of my page and is supposed to delete the row from the database that has been selected from the drop down list. The thing is, even if it did work it would delete all rows in the database with the same heading and I can't figure out how to delete via primary key.
<?php
// Delete event data
if ($_REQUEST['delete']){

	// open connection to MySQL server
	$connection = mysql_connect('localhost:80', 'username', 'password')
	or die ('Unable to connect!');
		
	//select database
	mysql_select_db('test') or die ('Unable to select database!');
	
	//define variables
	$eventSelect = $_POST['eventSelect'];
	
	//create and execute query
	$query = "DELETE FROM event WHERE heading = '$eventSelect'";
	$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
		
	// close connection
	mysql_close($connection);
}
?>

This is the code I have in the body of my page that fetches all the event headings and displays them in a drop down list
<td>
<?php 
// open connection to MySQL server
$connection = mysql_connect('localhost:80', 'username', 'password')
or die ('Unable to connect!');
					
//select database
mysql_select_db('test') or die ('Unable to select database!');
					
//create and execute query
$query = 'SELECT heading FROM event ORDER BY heading';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
					
//create selection list
echo "<select name='events' value='-'>\n";
echo "<option value='Select event'>Select an event to be edited\n";
while($row = mysql_fetch_row($result))
  {
    $eventSelect = $row[0];
    echo "<option value='$eventSelect'>$eventSelect</option>\n";
  }
echo "</select>"
?>					
<input name="edit" type="submit" class="submitForm" value="Edit">
<input name="delete" type="submit" class="submitForm" value="Delete">
</td>

Confused and somewhat frustrated
Last edited by Venom Rush : Oct 24th, 2007 at 4:42 am.
Reply With Quote  
Join Date: Aug 2006
Posts: 32
Reputation: StatiX is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
StatiX's Avatar
StatiX StatiX is offline Offline
Light Poster

Re: Deleting an item from a database by selecting it from a drop-down menu

  #4  
Oct 24th, 2007
to delete only one row from database, set your primary key as the value for the select like the following:

  1.  
  2. //create selection list
  3. echo "<select name='events'>\n";
  4. echo "<option value='Select event'>Select an event to be edited\n";
  5. while($row = mysql_fetch_row($result))
  6. {
  7. $eventSelect = $row[0];
  8. $eventid = $row[1]; //set this to the column of th primary key
  9. echo "<option value='$eventid'>$eventSelect</option>\n";
  10. }
  11. echo "</select>"

Then in your sql that deletes from database, add LIMIT 1 to the end and that will only affect one row.

Also change the following:

  1.  
  2. //define variables
  3. $eventSelect = $_POST['eventSelect'];
  4.  
  5. //create and execute query
  6. $query = "DELETE FROM event WHERE heading = '$eventSelect'";
  7.  

To:

  1.  
  2. //define variables
  3. $eventSelect = $_POST['events'];
  4.  
  5. //create and execute query
  6. $query = "DELETE FROM event WHERE id = '$eventSelect' LIMIT 1";
  7.  

This allows only one row to be deleted and uses the events id (the value of the selection in the dropdown box) to be deleted. You'll need to change the id= to match the primary key in your database and change the first block of code at the comment to match the primary id.
Always do what you wish you could..
Reply With Quote  
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Re: Deleting an item from a database by selecting it from a drop-down menu

  #5  
Oct 24th, 2007
Hi Statix

I tried your example out and it hasn't worked
Nothing is deleted.
Last edited by Venom Rush : Oct 24th, 2007 at 9:49 am.
Reply With Quote  
Join Date: Oct 2007
Posts: 189
Reputation: Venom Rush is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Junior Poster

Re: Deleting an item from a database by selecting it from a drop-down menu

  #6  
Oct 24th, 2007
My bad. Statix, your example did work....i just had name of my select field wrong. Thanks a bunch
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 5:55 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC