我在使用Web表单将数据输入到SQL中的表时遇到问题。现在,下拉菜单从SQL表中提取数据,允许用户选择一个选项,然后提交表单以将信息添加到“passedexams”中。表。到目前为止,我得到了一个未知的索引"第14行和第15行的错误(标有" *")。 html网站和php脚本之间的变量都没有完成。
这是HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add your passed Exam!</title>
</head>
<body>
<form action="cert.php" method="post">
<p>
<label for="name">Name:</label>
<?php
$conn = new mysqli('localhost', '*****', '******', '*******')
or die ('Cannot connect to db');
$result = $conn->query("SELECT UID, UName from users");
echo
echo "<select name='Name'>";
while ($row = $result->fetch_assoc()) {
unset($name);
$name = $row['UName'];
$UID = $row['UID'];
echo "<option value=\"$UID\">$name</option>\n";
}
echo "</select>";
echo "<br>";
echo "<br>\n\n";
echo "<label for='vendor'>Certification:</label>\n\n";
$result = $conn->query("SELECT CertName, CertID from certs");
echo "<select id='CertName' name='CertName'>";
while ($row = $result->fetch_assoc()) {
unset($name);
$certname = $row['CertName'];
$certid = $row['CertID'];
echo "<option value=\"$certid\">$certname</option>\n";
}
echo "</select>";
?>
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
这是cert.php:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "********", "********", "********");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
//$Uname = mysqli_real_escape_string($link, $_POST['Name']);
//$certname = mysqli_real_escape_string($link, $_POST['CertName']);
"*"$UID = $_POST['UID'];
"*"$certid = $_POST['certid'];
echo $certname;
// attempt insert query execution
$sql = "INSERT INTO passedexams (CertID, UserID)
VALUES ('$certid', '$UID')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
// Redirect to home table
//header( "refresh:5; url=wherever.php" );
} else{
echo "ERROR: Unable to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
答案 0 :(得分:0)
您必须使用选择框的名称属性来获取帖子值。更改以下行
$UID = $_POST['UID'];
$certid = $_POST['certid'];
要
$UID = $_POST['Name'];
$certid = $_POST['CertName'];