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

Member Avatar for fatihpiristine

post the selected item's value and request it...

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

to delete only one row from database, set your primary key as the value for the select like the following:

//create selection list
echo "<select name='events'>\n";
echo "<option value='Select event'>Select an event to be edited\n";
while($row = mysql_fetch_row($result))
  {
    $eventSelect = $row[0];
$eventid = $row[1]; //set this to the column of th primary key
    echo "<option value='$eventid'>$eventSelect</option>\n";
  }
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:

//define variables
	$eventSelect = $_POST['eventSelect'];
	
	//create and execute query
	$query = "DELETE FROM event WHERE heading = '$eventSelect'";

To:

//define variables
	$eventSelect = $_POST['events'];
	
	//create and execute query
	$query = "DELETE FROM event WHERE id = '$eventSelect' LIMIT 1";

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.

Hi Statix

I tried your example out and it hasn't worked :(
Nothing is deleted.

My bad. Statix, your example did work....i just had name of my select field wrong. Thanks a bunch ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.