大家好,我正在学习 PHP,下面的代码将信息发送到我的 MySQL 数据库,但它与我刚刚给他插入的数据不匹配,例如它为下面的代码插入 1|blank|blank。我无法解决这个问题:
插入.php文件:
<?php
$con=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$age = mysqli_real_escape_string($con, $_POST['age']);
$sql="INSERT INTO Person (PID,Name, Age)
VALUES ('1','$firstname', '$age')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added to database";
mysqli_close($con);
?>
这让我不断地说:
Notice: Undefined index: firstname in E:\XAMPP\htdocs\Bioinformatics\insert.php on line 11
Notice: Undefined index: age in E:\XAMPP\htdocs\Bioinformatics\insert.php on line 12
html 表单代码:
<!DOCTYPE html>
<html>
<body>
<form name="input" action="insert.PHP" method="get">
First name: <input type="text" name="FirstName" value="Ronaldo"><br>
Age: <input type="text" name="Age" value="28"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
感谢大家的帮助!
最佳答案
您似乎忘记发布所需的数据。 $_POST['firstname'] 和 $_POST['age'] 未在您的 html 中定义。 您缺少以下输入或拼写错误:
<input type="text" name="firstname" />
<input type="text" name="age" />
关于php 没有向 mysql 发送正确的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26099316/