我正在尝试select
使用数据库的元素填充a ,但是选择始终为空。我独立尝试了PHP部分,并且可以正常工作。
我总是得到同样的错误:Undefined variable: countries
在<select>
行。但是,它的定义是,首先是因为它被设置为空字符串,其次是因为正如我所说,数据的连接和检索正在正常进行。
那么可能是什么问题呢?
<?php
#Connection with the database
try{
$db = new PDO('mysql:dbname=php_test;host=localhost','root','mysql');
}
catch (PDOException $e){
echo $e->getMessage();
exit();
}
#Get the list of countries
$query = "SELECT country FROM countries";
$stmt = $db->prepare($query);
$stmt->execute();
$countries = "";
while ($row = $stmt->fetch()) {
$countries .= '<option value= "'.$row['country'].'">'.$row['country'].'</option>';
}
?>
然后在表单中:
<label for="country">Country:</label>
<select name="country"> <?php echo htmlspecialchars($countries); ?>
</select>
您已$countries
定义,但仅在提交表单时才具有。此代码关闭检查POST
值的条件。这将允许$countries
在每次加载时进行填充。
<?php
$name = $email = $password = $password2 = $address1 = $address2 = $address3 = $town = $county = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = sanitize($_POST["name"]);
if ((strlen($name) < 2) || (strlen($name) > 50)) {
echo "<span style=\"color: #FF0000;\"> Name must have between 2 and 50 characters </span> <br>";
$name = "";
}
$email = sanitize($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<span style=\"color: #FF0000;\"> Check the format of the email </span> <br>";
$email = "";
}
$password = sanitize($_POST["pass1"]);
if ((strlen($password) < 6) || (strlen($password) > 20)) {
echo "<span style=\"color: #FF0000;\"> Password must have between 6 and 20 characters </span> <br>";
}
else {
$password2 = sanitize($_POST["pass2"]);
if ($password != $password2) {
echo "<span style=\"color: #FF0000;\"> Passwords don't match </span> <br>";
}
}
$address1 = sanitize($_POST["addr1"]);
if ((strlen($address1) < 2) || (strlen($address1) > 80)) {
echo "<span style=\"color: #FF0000;\"> The address must have between 2 and 80 characters </span> <br>";
$address1 = "";
}
if (isset($_POST["addr2"])) {
$address2 = sanitize($_POST["addr2"]);
if ((strlen($address2) < 2) || (strlen($address2) > 80)) {
echo "<span style=\"color: #FF0000;\"> The address 2 must have between 2 and 80 characters </span> <br>";
$address2 = "";
}
}
if (isset($_POST["addr3"])) {
$address2 = sanitize($_POST["addr3"]);
if ((strlen($address3) < 2) || (strlen($address3) > 80)) {
echo "<span style=\"color: #FF0000;\"> The address 3 must have between 2 and 80 characters </span> <br>";
$address3 = "";
}
}
$town = sanitize($_POST["town"]);
if ((strlen($town) < 2) || (strlen($town) > 30)) {
echo "<span style=\"color: #FF0000;\"> The town must have between 2 and 30 characters </span> <br>";
$town = "";
}
$county = sanitize($_POST["county"]);
if ((strlen($county) < 2) || (strlen($county) > 30)) {
echo "<span style=\"color: #FF0000;\"> The county must have between 2 and 30 characters </span> <br>";
$county = "";
}
#Connection with the database
try{
$db = new PDO('mysql:dbname=php_test;host=localhost','root','mysql');
}
catch (PDOException $e){
echo $e->getMessage();
exit();
}
}
#Get the list of countries
$query = "SELECT country FROM countries";
$stmt = $db->prepare($query);
$stmt->execute();
$countries = "";
while ($row = $stmt->fetch()) {
$countries .= '<option value= "'.$row['country'].'">'.$row['country'].'</option>';
}
function sanitize($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句