gpt4 book ai didi

php - 为什么查询不插入记录

转载 作者:行者123 更新时间:2023-11-29 04:38:32 25 4
gpt4 key购买 nike

我试图在数据库中插入简单的两个值,但它们既没有插入也没有给出错误。我不知道发生了什么事。我也尝试在 if 语句之外进行 sql 查询,但它没有用。

请帮忙

编码文件

<?php 

include 'includes/db_connection.php';


if (isset($_POST['submit']))
{
$name = test_input($_POST["name"]);
$fathername = test_input($_POST["fathername"]);


$sql = "INSERT INTO student (name,fathername)
VALUES ('$name','$fathername')";
mysqli_query($conn, $sql);

if(mysqli_query($conn, $sql))
{
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

}


?>


<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.2.43/jquery.form-validator.min.js"></script>
</head>



<body>

<form action="valid_test.php" method="post" id="registration-form">
<div>
Name
<input name="name" data-validation="length alphanumeric"
data-validation-length="6-15"
data-validation-error-msg="User name has to be an alphanumeric value (6-15 chars)">
</div>

<div>
Father Name
<input name="fathername" data-validation="length alphanumeric"
data-validation-length="6-20"
data-validation-error-msg="User name has to be an alphanumeric value (6-20 chars)">
</div>
<div>
<input type="submit" value="Validate">
<input type="reset" value="Reset form">
</div>
</form>

最佳答案

当我们发布 HTML 表单时,元素会使用 name 属性发布。

您缺少提交按钮的 name 属性。

改变:

<input type="submit" value="Validate">

收件人:

<input type="submit" value="Validate" name="submit">

由于您的提交按钮没有 name 属性,条件

if (isset($_POST['submit'])) 没有得到满足,因此代码不工作。

此外,您正在重复 mysqli_query($conn, $sql);。删除它的第一个实例。

关于php - 为什么查询不插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266760/

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