Page 1 of 1

No values in dropdown box

Posted: Fri Jun 01, 2018 8:06 am
by Morphies
Hi All,

I'm new to the forum and to php, MySQL and HTML. *Waves and smiles*

I've given myself a little project to build a small form to capture some information efficiently and quickly.

Essentially, I now have a test environment established where I can have a play.

I have created a config.php with connection details to my db.

The following code spits out the details of a table i'm trying to use in a dropdown for a form.

Code: Select all

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "warrenst";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM equipment";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". $row["Source"]. " - Name: ". $row["SourceID"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?> 

</body>
</html>
</html>
Which is great, I can see the results from the table. I;m trying to use the source field in the dropdown but utilise the SourceID when the form is posted.

I have the following code however the dropdown contains no values atall..

Code: Select all

 	<label for="Source">Source:</label>
		<select id="Source" name="source">
<?php
include 'config.php';
$dbname = 'warrenst';
$con=mysqli_connect("$host","$username","$password") or die("Server Error");
mysqli_select_db($dbname, $con) or die ("error selecting database");
$soquery="SELECT * FROM equipment";
$soresult=mysqli_query($soquery) or die ("Query to obtain data failed");
while ($sorow=mysqli_fetch_array($soresult)) {
$soTitle=$sorow["Source"];
$soReference=$sorow["SourceID"];
    echo "<option value=\"$soReference\">
        $soTitle
    </option>";
}
?>
</select>
Any pointers as to where I'm going wrong?

Thanks immensely in advance.

Morph.

Re: No values in dropdown box

Posted: Fri Jun 01, 2018 8:13 am
by element121
Hi Morph,

Welcome!
There are different ways of concatenating strings and variables together, personally I would do like this:

Code: Select all

<?php
echo '<option value="'. $soReference .'">'. $soTitle .'</option>';
Hope that helps,
Jon

Re: No values in dropdown box

Posted: Fri Jun 01, 2018 9:22 am
by Morphies
HI Jon,

Thanks for the speedy reply and warm welcome!

I've updated the code to reflect your suggestion and I still have the same issue.

The dropdown is visible, but there are no values there.

incidentally, the remainder of the page is not displaying either. previously I;ve had the email box and submit button present which makes me thing something isn't closing out.

here is the full code:

Code: Select all

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Production entry form</title>
</head>
<body>
<form action="insert.php" method="post">
	<p>
    	<label for="OperatorID">OperatorID*:</label>
        <input type="Number" name="OperatorID" id="OperatorID" maxlength="4" required="Yes">
    </p>
    <p>
    	<label for="Source">Source:</label>
		<select id="Source" name="source">
<?php
include 'config.php';
$dbname = 'warrenst';
$con=mysqli_connect("$host","$username","$password") or die("Server Error");
mysqli_select_db($dbname, $con) or die ("error selecting database");
$soquery="SELECT * FROM equipment";
$soresult=mysqli_query($soquery) or die ("Query to obtain data failed");
while ($sorow=mysqli_fetch_array($soresult)) {
$soTitle=$sorow["Source"];
$soReference=$sorow["SourceID"];
    echo '<option value="'. $soReference .'">'. $soTitle .'</option>';
}
?>
</select>
    </p>
    <p>
    	<label for="emailAddress">Email Address:</label>
        <input type="text" name="email" id="emailAddress">
    </p>
    <input type="submit" value="Add Records">
</form>
</body>
</html>
Cheers,

Chris.

Re: No values in dropdown box

Posted: Mon Jun 04, 2018 7:29 am
by element121
Hi Chris,

What code do you see when you view source in the browser?
(In Chrome you can view source with Ctrl+U or right click "View page source")

I'm guessing from previous experience you will see an error message there?

Regards,
Jon

Re: No values in dropdown box

Posted: Tue Jun 05, 2018 2:51 pm
by benanamen
mysqli_query requires two parameters. You only have one. Missing the connection param.