[56K WARN] Update Record with select menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

[56K WARN] Update Record with select menu

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

More or less...
Image[/img]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply