无法通过PHP将新记录插入到mysql数据库中
当我按提交按钮时出现错误。无法通过PHP将新记录插入到mysql数据库中
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');
为什么我写的,这是因为你的输入名称包含Name
,Code
和GPA
所以你需要写这正是因为你的输入名称(大小写敏感的原因)。
它仍不会添加记录。 –
Jees的答案是否正确地为你工作? –
仅适用于空条目,但记录仍不会通过表单添加。 –
用isset()
来做。当提交按钮只点击代码运行时。
在php中你应该使用表单输入名字段。
<?php
if(isset($_POST['submit'])){
$link = mysqli_connect("localhost", "username", "password", "students");
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');
//to prevent sql injection attack
$full_name = mysqli_real_escape_string($link, $full_name);
$code = mysqli_real_escape_string($link, $code);
$gpa = mysqli_real_escape_string($link, $gpa);
// attempt insert query execution
$sql = "INSERT INTO info (Name,Code,GPA) 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);
}
?>
<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="full_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" name="submit" value="submit">
</form>
</body>
</html>
谢谢,解决了空条目问题。 –
请提一下'info'的表结构。 –
info是“students”db中的一个表,它有3列;名称,代码,GPA和2行数据。 –
问题是输入名称。您用name="Name"
命名全名输入,但您在php部分声明$full_name = filter_input(INPUT_POST, 'full_name');
。您必须将full_name
更改为Name
。以及代码和GPA输入。
这正是我在我的答案中写的... –
@EdvinTenovimas啊,你是对的。对不起,我不注意..我只是投你的答案:) –
好吧,谢谢! :)如果你没有注意到,没关系,每个人都会遇到这种情况。 –
您需要检查是否已处理表单。我更喜欢在一个单独的php文件中处理表单而不是实际的表单。 – Linkan
您已开放给SQL注入。 – chris85
我应该如何处理SQL注入? –