Jump to content

PHP Page, delete from dropdown list?


buzzkip

Recommended Posts

Hi all,

 

Please could someone guide me as to how I would create a simple php page, that has a dropdown list populated by a database table - and then a submit button.

 

When I select an option in the dropdown list and then click submit, it deletes the row from the database table.

 

 

Would be very much appreciative of some help  :confused:

Link to comment
https://forums.phpfreaks.com/topic/212361-php-page-delete-from-dropdown-list/
Share on other sites

Hi all,

 

Please could someone guide me as to how I would create a simple php page, that has a dropdown list populated by a database table - and then a submit button.

 

When I select an option in the dropdown list and then click submit, it deletes the row from the database table.

 

 

Would be very much appreciative of some help  :confused:

 

Post your code here for people to look at so they can help you.

table: inplace

columns: field, value, stock

 

The drop down displays the value columns perfectly, but when I hit the submit button... nothing happens.

 

<?php
//create_topic.php
include 'connect.php';
include 'header.php';

echo '<h2>Delete an item in the database</h2>';
if($_SESSION['signed_in'] == false)
{
echo 'Sorry, you have to be signed in.';
}
else
{
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
	$sql = "SELECT
				field,
				value,
				stock
			FROM
				inplace";

	$result = mysql_query($sql);

	if(!$result)
	{
		echo 'Error while selecting from database. Please try again later.';
	}
	else
	{
		if(mysql_num_rows($result) == 0)
		{
			if($_SESSION['user_level'] == 1)
			{
				echo 'You have not created any items yet.';
			}
			else
			{
				echo 'Before you can post an item, you must wait for an admin to create some locations.';
			}
		}
		else
		{
			echo '<form method="post" action="">
				Item:
			<select name="field">';
				while($row = mysql_fetch_assoc($result))
				{
					echo '<option value="' . $row['field'] . '">' . $row['value'] . '</option>';
				}
			echo '</select><input type="submit" value="Delete item" /></form>';
		}
	}
}
else
{
	$query  = "BEGIN WORK;";
	$result = mysql_query($query);

	if(!$result)
	{
		echo 'An error occured while deleting your item. Please try again later.';
	}
	else
	{
			$sql = "DELETE FROM inplace WHERE value = ' " . $_POST['value'] . " ' ";

		$result = mysql_query($sql);
		if(!$result)
		{
			echo 'An error occured while deleting your data. Please try again later.<br /><br />' . mysql_error();
			$sql = "ROLLBACK;";
			$result = mysql_query($sql);
		}
		else
		{
			$topicid = mysql_insert_id();

			$sql = "DELETE FROM inplace WHERE value = ' " . $_POST['value'] . " ' ";



			$result = mysql_query($sql);

			if(!$result)
			{
				echo 'An error occured while deleting your item. Please try again later.<br /><br />' . mysql_error();
				$sql = "ROLLBACK;";
				$result = mysql_query($sql);
			}
			else
			{
				$sql = "COMMIT;";
				$result = mysql_query($sql);

				echo 'You have succesfully deleted your new item.';
			}
		}
	}
}
}

include 'footer.php';
?>

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.