gpt4 book ai didi

php - 无法通过PHP向mysql数据库中插入新记录

转载 作者:可可西里 更新时间:2023-11-01 06:31:28 24 4
gpt4 key购买 nike

当我按下提交按钮时出现错误。

object not found error.

页面会自动添加带有自动递增主键的空条目(无需按下提交按钮)。

我还是PHP的初学者,我搜索得很彻底,但我找不到代码中的错误。

  <html>   
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<form action="insert.php" method="post">
<p>

<label for="Name">Full Name:</label>

<input type="text" name="Name" id="Name">

</p>
<p>
<label for="Code">Code:</label>
<input type="text" name="Code" id="Code">
</p>
<p>
<label for="GPA">GPA:</label>
<input type="text" name="GPA" id="GPA">
</p>
<input type="submit" value="Submit">
</form>

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL

server with default setting (user 'root' with no password) */

$link = mysqli_connect("localhost", "username", "password", "students");
// Check connection

if ($link === false) {

die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$full_name = filter_input(INPUT_POST, 'full_name');
$code = filter_input(INPUT_POST, 'code');
$gpa = filter_input(INPUT_POST, 'gpa');
// attempt insert query execution
$sql = "INSERT INTO info VALUES ('$full_name', '$code', '$gpa')";
if (mysqli_query($link, $sql)) {
echo "Records added successfully. $full_name";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
</body>
</html>

最佳答案

试试这个:

$full_name = filter_input(INPUT_POST, 'Name');
$code = filter_input(INPUT_POST, 'Code');
$gpa = filter_input(INPUT_POST, 'GPA');

我之所以这样写是因为你的输入名称包含NameCodeGPA 所以你需要完全按照你的输入名称(区分大小写)。

关于php - 无法通过PHP向mysql数据库中插入新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39819095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com