【发布时间】:2021-05-11 19:29:32
【问题描述】:
这是当前显示的错误。下面第一个代码我保存为 details.php,第二个代码保存为 details.html
警告:第 12 行 C:\xampp\htdocs\children_math\detail2.php 中未定义的数组键“first_name”
警告:第 13 行 C:\xampp\htdocs\children_math\detail2.php 中未定义的数组键“last_name”
警告:第 14 行 C:\xampp\htdocs\children_math\detail2.php 中未定义的数组键“email”
警告:第 15 行 C:\xampp\htdocs\children_math\detail2.php 中未定义的数组键“age”
错误:无法执行 INSERT INTO 详细信息(名字、姓氏、电子邮件、年龄)值(''、''、''、'')。无法添加或更新子行:外键约束失败 (record.details, CONSTRAINT details_ibfk_1 FOREIGN KEY (id) REFERENCES users (id))
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "record");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);
$last_name = mysqli_real_escape_string($link, $_REQUEST['last_name']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$age = mysqli_real_escape_string($link, $_REQUEST['age']);
// Attempt insert query execution
$sql = "INSERT INTO details (first_name, last_name, email, age) VALUES ('$first_name', '$last_name', '$email', '$age')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="detail2.php" method="post">
<p>
<label for="firstName">First Name:</label>
<input type="text" name="first_name" id="firstName">
</p>
<p>
<label for="lastName">Last Name:</label>
<input type="text" name="last_name" id="lastName">
</p>
<p>
<label for="emailAddress">Email Address:</label>
<input type="text" name="email" id="emailAddress">
</p>
<p>
<label for="ages">Email Address:</label>
<input type="text" name="age" id="ages">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>```
【问题讨论】:
-
尝试isset函数来修复这个错误。
-
for $email = $_POST['email']; ?
-
您确定 POST 正在成功获取值吗?表单必须具有method="POST"的属性
-
@TanYiJing 先生,我不太确定,因为我刚刚学习了几个月的 php 和 html
-
@TanYiJing 是不是编码错误?
标签: php html visual-studio-code