我正在尝试在PHP中获取“选择选项,下拉菜单”以从MySQL表中获取数据。 以下代码正确显示了我的表“comunas”的完整列表(一个ID号,一个描述VARCHAR),也是第一次将数据正确插入数据库。 但是在第一次提交后(按“发送”按钮),网页上的警告失败并且没有显示我第二次下拉菜单id =“comuna”:
警告:mysqli_query():无法在第37行的F:\ proyectosFelipe \ proyectosPaginas \ mantenedor \ clienteAdd.php中获取mysqli 无法连接到MySQL:
我在clienteAdd.php和clientForm.php中要求一次require_once mysqli_connect.php 我认为我的问题与第二次加载网页有关,无法连接到数据库,但我无法找出原因。 可以请你指导我解决我的问题吗?
提前谢谢你,
这是我理解的代码部分不正常,因为如果我把它拿走,它不会产生我的问题:
<?php
$select = "";
require_once('mysqli_connect.php');
//include('mysqli_connect.php');
$sql=mysqli_query($dbc,"SELECT id
,comuna
FROM comunas") OR die('Could not connect to MySQL: ' . mysqli_connect_error());
if(mysqli_num_rows($sql))
{
//$select= "<select name='comuna' id='comuna' tabindex='6' value='$comuna'>";
$select= "<select name='comuna' id='comuna' tabindex='6'>";
$select.='<option value="">Comuna...</option>';
while($rs=mysqli_fetch_array($sql))
{
$select.='<option value="'.$rs['id'].'">'. $rs['comuna'] .'</option>';
}
}
mysqli_close($dbc);
$select.='</select>';
echo utf8_encode($select);
?>
我的完整代码如下:
clienteAdd.php
<html>
<head>
<title>Add Client</title>
</head>
<body>
<a href="clienteShow.php">Show Client</a><br>
<a href="clienteSearch.php">Search Client</a>
<?php //include('mysqli_connect.php');
include('clientForm.php');
?>
<div id="divAgenda">
<form id="contact" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="post" name = "registration">
<div class="success"><?= $success ?></div>
<fieldset>
<span class="error"><?= $rut_error ?></span><br>
<input placeholder="Rut..." id="rut" type="text" name="rut" value="<?= $rut ?>" tabindex="1" size="8" maxlength="8"> - <input type="text" name="dv" value="<?= $dv ?>" size="1" tabindex="2" maxlength="1"> Ejemplo: 12345678-1<br>
</fieldset>
<fieldset>
<span class="error"><?= $first_name_error ?></span><br>
<input placeholder="Primer Nombre..." type="text" id="first_name" name="first_name" value="<?= $first_name ?>" maxlength="50" tabindex="3"><br>
</fieldset>
<fieldset>
<span class="error"><?= $last_name_error ?></span><br>
<input placeholder="Segundo Nombre..." type="text" id="last_name" name="last_name" value="<?= $last_name ?>" maxlength="50" tabindex="4"><br>
</fieldset>
<fieldset>
<span class="error"><?= $email_error ?></span><br>
<input placeholder="Correo Electrónico..." id="email" type="text" name="email" value="<?= $email ?>" maxlength="100" tabindex="5"><br>
</fieldset>
<fieldset>
<span class="error"><?= $comuna_error ?></span><br>
<?php
$select = "";
require_once('mysqli_connect.php');
//include('mysqli_connect.php');
$sql=mysqli_query($dbc,"SELECT id
,comuna
FROM comunas") OR die('Could not connect to MySQL: ' . mysqli_connect_error());
if(mysqli_num_rows($sql))
{
//$select= "<select name='comuna' id='comuna' tabindex='6' value='$comuna'>";
$select= "<select name='comuna' id='comuna' tabindex='6'>";
$select.='<option value="">Comuna...</option>';
while($rs=mysqli_fetch_array($sql))
{
$select.='<option value="'.$rs['id'].'">'. $rs['comuna'] .'</option>';
}
}
mysqli_close($dbc);
$select.='</select>';
echo utf8_encode($select);
?>
<br>
</fieldset>
<fieldset>
<span class="error"><?= $address_error ?></span><br>
<input placeholder="Dirección..." id="address" type="text" name="address" value="<?= $address ?>" maxlength="200" tabindex="5"><br>
</fieldset>
<fieldset>
<span class="respuesta"><?= $codigosValores ?></span><br>
</fieldset>
<fieldset>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Send</button>
</fieldset>
</form>
<?php
if ($rut_error != '')
echo "<script>(document.getElementById('rut').focus())();</script>";
else
if ($first_name_error != '')
{
echo "<script>(document.getElementById('first_name').focus())();</script>";
}
else
if ($last_name_error != '')
{
echo "<script>(document.getElementById('last_name').focus())();</script>";
}
else
if ($email_error != '')
{
echo "<script>(document.getElementById('email').focus())();</script>";
}
else
if ($address_error != '')
{
echo "<script>(document.getElementById('address').focus())();</script>";
}
?>
</body>
</html>
clientForm.php
<?php
// define variables and set to empty values
$rut_error = $first_name_error = $last_name_error = $email_error = $address_error = $comuna_error = "";
$rut = $dv = $first_name = $last_name = $email = $address = $comuna = "";
$error = 0;
$success = "";
$codigosValores = "";
if(isset($_POST['submit']))
{
//RUT Validation
$rut = test_input($_POST["rut"]);
$dv = ($_POST["dv"]);
if ( empty($_POST["rut"]))
{
$rut_error = "RUT is required";
$error = 1;
}
else if ( $dv=='' )
{
$rut_error = "Verification digit is required";
$error = 1;
}
else if (!is_numeric($rut))
{
$rut_error = "Entered RUT is not numeric";
$error = 1;
}
else if (!((strlen($rut) == 7) or (strlen($rut) == 8)))
{
$rut_error = "Number of digits of RUT not valid";
$error = 1;
}
else
{
$x = 2; $s = 0; $dv2 = 0;
for($i = (strlen($rut) - 1); $i >= 0; $i--)
{
if($x > 7)
$x = 2;
$s += ($rut[$i] * $x);
$x++;
}
$dv2=11-($s % 11);
if($dv2 == 10)
$dv2 = 'K';
if($dv2 == 11)
$dv2 = '0';
if($dv2 == $dv)
{
}
else
{
$rut_error = "invalid RUT";
$error = 1;
}
}
//First name Validation
if (empty($_POST["first_name"]))
{
$first_name_error = "First name is required";
$error = 1;
}
else
{
$first_name = test_input($_POST["first_name"]);
}
//Last name Validation
if (empty($_POST["last_name"]))
{
$last_name_error = "Second name is required";
$error = 1;
}
else
{
$last_name = test_input($_POST["last_name"]);
}
//Email Validation
if (empty($_POST["email"]))
{
$email_error = "Email is required";
$error = 1;
}
else
{
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if ((!filter_var($email, FILTER_VALIDATE_EMAIL)) and $rut_error == '' and $first_name_error == '' and $last_name_error == '')
{
$email_error = "Invalid email";
$error = 1;
}
}
//Adress Validation
if (empty($_POST["address"]))
{
$address_error = "Address is required";
$error = 1;
}
else
{
$address = test_input($_POST["address"]);
}
//Comuna Validation
if (empty($_POST["comuna"]))
{
$comuna_error = "Comuna is required";
$error = 1;
}
else
{
$comuna = test_input($_POST["comuna"]);
}
$codigosValores = "<br>". "rut={" . $rut ."-" . $dv . "}";
$codigosValores = $codigosValores . " ". "first_name={" . $first_name . "}";
$codigosValores = $codigosValores . " ". "last_name={" . $last_name . "}";
$codigosValores = $codigosValores . " ". "email={" . $email . "}";
$codigosValores = $codigosValores . " ". "address={" . $address . "}";
$codigosValores = $codigosValores . " ". "comuna={" . $comuna . "}";
if ($error == 0)
{
//include('mysqli_connect.php');
require_once('mysqli_connect.php');
$query = "INSERT INTO students (rut, dv, first_name, last_name, email, address, comuna) VALUES (?,?,?,?,?,?,?)";
$stmt = mysqli_prepare($dbc, $query);
mysqli_stmt_bind_param($stmt, "sssssss", $rut, $dv, $first_name, $last_name, $email, $address, $comuna);
mysqli_stmt_execute($stmt);
$affected_rows = mysqli_stmt_affected_rows($stmt);
if($affected_rows == 1)
{
$rut = $dv = $first_name = $last_name = $email = $address = $comuna = '';
$success = "Customer successfuly inserted.";
echo "<br>"."Client Entered";
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
else
{
echo 'Error Occurred<br />';
echo mysqli_error();
mysqli_stmt_close($stmt);
mysqli_close($dbc);
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}