堆栈溢出调用堆栈#timememoryfunctionlocation 10.0000143728
Hi this is my second time posting on this site. I looked @ the other article pertaining to this message and am unable to figure this out.
So i'm trying to dynamically populate the content of a drop down box with data store in the DB.
I'm getting the following error message displayed in the drop down for each record in the DB (3times)
Any help would be appreciated. Thanks
<!DOCTYPE html>
<?php
$con=mysqli_connect("localhost","root","","project1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// this block would be at the top of your file above the html
$query = "select * from countries";
$country_results = mysqli_query($con,$query);
$number_of_returns = mysqli_num_rows($country_results);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<h1>Insert Site into DB</h1>
<h2 class="button"><a href=/index.html>Home</a></h2>
<h2 class="button"><a href=/insert.html>add site</a></h2>
<h2 class="button"><a href=/delete.html>delete site</a></h2>
<h2 class="button"><a href=/search.html>search site</a></h2>
<form class="insert" action="insert.php" method="post">
<p>Site Info</p>
Site code:<input type="text" name="site_code"><br>
Site name: <input type="text" name="site_name"><br>
Address: <input type="text" name="site_address"><br>
City:<br>
Postal code: <input type="text" name="site_postalcode"><br>
Province: <select name="province"></select><br>
Country: <select name=”country”>
<?php while (($row = mysqli_fetch_row($country_results)) != null){ ?>
<option value=”<?php echo $row[‘country’];?></option>
<?php } ?>
</select><br>
<p>Site Contact Info</p>
Site contact name: <input type="text" name="site_contact_name"><br>
Phone number 1: <input type="number" name="site_contact_number1"><br>
Phone number 2: <input type="number" name="site_contact_number2"><br>
Email address: <input type="email" name="site_contact_email"><br>
<input type="submit">
</form>
</body>
</html>
If you want to access the result as an associative array e.g. $row['country']
you have to use mysqli_fetch_assoc
. By using mysqli_fetch_row
you can access you results as $row[0]
. Also, the are some odd quotes in your code which have to be replaced as well as some other errors in your html e.g. missing value attribute closing quotes in options.This will work:
<select name="country">
<?php while($row = mysqli_fetch_assoc($country_results)){ ?>
<option value="<?php echo $row['title'];?>"><?php echo $row['title'];?></option>
<?php } ?>
</select>