No values in dropdown box

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Morphies
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Jun 01, 2018 7:58 am

Fri Jun 01, 2018 8:06 am

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.

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Fri Jun 01, 2018 8:13 am

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

Morphies
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Jun 01, 2018 7:58 am

Fri Jun 01, 2018 9:22 am

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.

element121
php-forum Fan User
php-forum Fan User
Posts: 628
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Mon Jun 04, 2018 7:29 am

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

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 55
Joined: Mon Oct 16, 2017 1:06 pm

Tue Jun 05, 2018 2:51 pm

mysqli_query requires two parameters. You only have one. Missing the connection param.
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

Post Reply