Select from Combobox to update MySql

Codes here !

Moderators: egami, macek, gesf

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

Mon Apr 10, 2017 12:00 pm


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


	// 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; ?>
			<br />
			<br />
			<input type="submit" name="submit" value="Update Status" />
			$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.

php-forum GURU
php-forum GURU
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

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

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