无法通过PHP将新记录插入到mysql数据库中
发布时间:2020-12-13 13:44:13  所属栏目:PHP教程  来源:网络整理 
            导读:当我按下提交按钮时,我收到错误. object not found error. 页面会自动添加带有自动递增主键的空条目(无需按下提交按钮). 我仍然是PHP的初学者,我彻底搜索,但我无法找到代码中的错误. html head titleAdd New Record in MySQL Database/title/head body form
                
                
                
            | 
 当我按下提交按钮时,我收到错误. 
  
  
  
 页面会自动添加带有自动递增主键的空条目(无需按下提交按钮). 我仍然是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所以你需要把它写成输入名称(区分大小写). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
