Select from Combobox to update MySql

Codes here !

Moderators: macek, egami, gesf

Post Reply
ccordner
New php-forum User
New php-forum User
Posts: 2
Joined: Thu Mar 23, 2017 1:00 pm

Select from Combobox to update MySql

Post by ccordner » Mon Apr 10, 2017 12:00 pm

Hi

I've pieced this together from various tutorials, so apologies if it doesn't quite make sense, but here goes:

In my database, I have two tables - tblServiceStatus and tblCurrentStatus.

tblServiceStatus has several fields, including ID (Primary Key), a colour code, summary and description.

tblCurrentStatus has one column and one row, which contains an ID from tblServiceStatus. So 1 might indicate green, 2 yellow, 3 red etc.

What I have so far is the following:

At the top of the page, to create a connection and populate $options with all the rows from tblServiceStatus

Code: Select all

<?php 

	// Create connection
	$conn = new mysqli($servername, $username, $password, $dbname);

	$query = "SELECT Defcon, Summary, Template, LastVersion, Forecolour, Backcolour FROM tblServiceStatus";
	
	$result = mysqli_query($conn, $query);
	$defcons = "";
	
	while($row = mysqli_fetch_array($result))
	{
		$options = $options."<option>$row[1]</option>";
	}
	
?>
And the actual form on the page:

Code: Select all

		<form action="#" method="post">
	
			<select name="defcon">

					<?php echo $options; ?>
			
			</select>
			
			<br />
			<br />
			
			<input type="submit" name="submit" value="Update Status" />
		
		</form>
		
		<?php
			if(isset($_POST['submit'])){
			$selected_val = $_POST['defcon'];  // Storing Selected Value In Variable
			echo "Status is now :" .$selected_val;  // Displaying Selected Value
			}
		?>
What I want is to use the selection from the combobox to update tblCurrentStatus, so it's pointing to the right row in tblServiceStatus. As part of this I would like to specify the width of the combobox and concatenate two fields to be displayed (ideally I don't want to show the ID, but if I have to fair enough).

I would also like to know if it's possible to display a field from the database in an editable textarea on the form. In other words, when they choose green it would pull the appropriate 'description' from the table, allow them to edit it and save it back.

Thanks
Chris

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Re: Select from Combobox to update MySql

Post by NigelRen » Mon Apr 10, 2017 11:15 pm

Sounds like you want to use the value attribute of the option element. So your code should generate something like (copied from https://www.w3schools.com/tags/att_option_value.asp)

Code: Select all

<select name="cars">
    <option value="volvo">Volvo XC90</option>
    <option value="saab">Saab 95</option>
    <option value="mercedes">Mercedes SLK</option>
    <option value="audi">Audi TT</option>
  </select>
So when the form is submitted, it's the value that gets passed as the selected item for "cars" (in this case).
As for the width of the combo box, if you simply put the values you want displayed, then the size will usually fit. This content can be made up of whatever you wish, simply concatenate the bits you want when setting the content.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest