I want to display the Mysql table Filed values in selectbox. I tried the following code to display. But it normally display the specified field values in echo function and not i
There a few tips to offer that will condense the code block for this task -- too many to just comment under the accepted answer.
Tested Code:
if (!$con = new mysqli("localhost", "root", "root", "Time_sheet")) {
echo "Database Connection Error: " , $con->connect_error; // don't show actual error in "production" stage
} elseif (!$result = $con->query("SELECT Emp_id FROM Employee ORDER BY Emp_id")) {
echo "Syntax Error: " , $con->error; // don't show actual error in "production" stage
} else {
echo "";
}
if
line is both declaring $con
and checking it for a falsey return value in one step.elseif
line is both declaring $result
and checking it for a falsey return value in one step.else
block will process the zero or more rows of data that were generated as a result of the successful SELECT query.foreach()
and enjoy direct access to the row data via associative keys. PHP Manual Reference Another StackOverflow Postvalue
attribute inside of the
tag IF the value is different from text between the opening and closing tags (
). Form submissions and javascript implementations will all work the same if you omit the redundant value
attribute.If you DO wish to submit a different value in the select field rather than the text, here is what the syntax can look like:
echo "";
If you want to write a selected
attribute on one of the options based on a pre-existing variable (e.g. $selected_id
), it can look like this:
echo "";
If you wanted to combine the two previous processes, it can look like this:
echo "";