无法通过PHP将新记录插入到mysql数据库中

时间:2016-10-02 16:26:37

标签: php mysql

当我按下提交按钮时,我收到错误消息。

  

找不到对象错误。

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

我仍然是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>  

3 个答案:

答案 0 :(得分:13)

试试这个:

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

我之所以这样写是因为您的输入名称包含NameCodeGPA,因此您需要将其完全写为输入名称(区分大小写)。

答案 1 :(得分:6)

使用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>  

答案 2 :(得分:2)

问题是输入名称。您使用name="Name"命名了全名输入,但是在php部分中声明了$full_name = filter_input(INPUT_POST, 'full_name');。您必须将full_name更改为Name。以及代码和GPA输入。