Page 1 of 1

[56K WARN] Update Record with select menu

Posted: Thu Nov 02, 2006 4:05 pm
by psurrena
I am buildiing an admin area to manage a list of articles. Each article has a status which can either be current (0) or archived (1). I have both of these options in a slect menu. I want to be able to update the record when choosing the status in the select menu. The code I wrote seems ok but I don't know if I have to specify the id in the menu options (nor do I understand how).

Update code:

Code: Select all

<?php
	ob_start();
	
	include '../includes/connect.php';

	if(isset($_POST['update'])) {
		$status = $_POST['status'];
	
		$query = "UPDATE article SET status='$status' WHERE a_id='$a_id'";

		mysql_query($query) or die (mysql_error());
		mysql_close();

		header ('location:index.php');
	}	
?>
display / loop code:

Code: Select all

<?php
	include '../includes/connect.php';
			
	$query = "select * FROM article ORDER BY a_id";
	$result = mysql_query($query);
	mysql_query($query) or die (mysql_error());				
	
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$a_id	 = $row['a_id'];
		$title	 = $row['title'];
		$status  = $row['status'];
		
		echo '<li><p><a href="view.php?a_id=' . $a_id . '">' . $title . '</a><br />';
		echo '<a href="#a_id=' . $a_id . '">Edit</a> - ';
		echo '<a href="delete.php?a_id=' . $a_id . '">Delete</a><br />';
		echo '<form method="post">';
			echo 'Status: ';
			switch ($status) {
				case 0:
					echo '<select><option value="0" selected="selected">Current</option><option value="1">Archive</option></select>';
				break;
				
				case 1:
					echo '<select><option value="0">Current</option><option value="1" selected="selected">Archive</option></select>';
				break;
			}
		echo '<input type="submit" name="update" value="Update">';
		echo '</form>';
	}
?>
Thanks,
Pete

Posted: Thu Nov 02, 2006 5:30 pm
by RobertGonzalez
Lets talk english and see if we can translate into code...

You have a select list, that when you select an item, it updates the database? Is there more to it than that?

Posted: Thu Nov 02, 2006 5:49 pm
by psurrena
More or less...
Image[/img]

Posted: Thu Nov 02, 2006 6:42 pm
by RobertGonzalez
You may want to consider one form, or using an array of fields to post with. Also, there is no need to use output buffering in your update code, unless you are doing it to hush header already sent warnings (which is still not a reason to use it).

You might want to even consider a checkbox, since in essence you are offering only a Yes/No type situation to them as it related to Archive/Current.