Php 表单无法将数据插入我的数据库

Php 表单无法将数据插入我的数据库,php,mysqli,Php,Mysqli,我正试图向我的数据库发送一个简单的标题,但出于某种原因,它不断给我以下错误: > Error: INSERT INTO `formular` (`idFormular`, `idUser`, `Nume`) VALUES (NULL, 'qwe', 'Test1'); Column 'idformular' cannot be null 我曾多次尝试更改insert函数,但均无效。 这是我正在使用的代码: <?php session_start(); include 'conn

我正试图向我的数据库发送一个简单的标题,但出于某种原因,它不断给我以下错误:

> Error: INSERT INTO `formular` (`idFormular`, `idUser`, `Nume`) VALUES (NULL, 'qwe', 'Test1');
Column 'idformular' cannot be null 
我曾多次尝试更改insert函数,但均无效。 这是我正在使用的代码:

<?php

session_start();
include 'connections.php';
$name = $_POST['nume'];
$id = $_SESSION['username'];

$name = stripcslashes($name);
$name = mysqli_real_escape_string($con, $name);

$sql = "INSERT INTO `formular` (`idFormular`, `idUser`, `Nume`) VALUES (NULL, '$id', '$name');";

if ($con->query($sql) === true) {
    $sql1 = "SELECT idFormular FROM `formular` WHERE idUser=$id AND Nume='$name';";
    $result = mysqli_query($con, $sql1);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    $idForm = $row["idFormular"];
    $_SESSION["idForm"] = $idForm;
    header("Location:Forms.php", true, 301);
    exit();
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}

$con->close();

错误是您自己造成的

这是您编写的查询:

INSERT INTO `formular` (`idFormular`, `idUser`, `Nume`) VALUES (NULL, 'qwe', 'Test1');
报告的错误消息为:

Column 'idformular' cannot be null
因此,非常清楚:字段
idFormular
是表中的一个键,您正在传递一个
NULL

您需要做的是将
idFormular
声明为
AUTO_INCREMENT
,并按照中的示例重新实现您的代码

下面是一个关于如何重写代码相关部分的示例:

mysqli\u查询($con),插入'formular'('idUser','Nume')值('qwe','Test1');
$idForm=mysqli\u insert\u id($con);

好的,我确实纠正了这个妄想,但现在它不喜欢mysql\u查询函数了?致命错误:未捕获错误:调用D:\xamp\htdocs\pw\CreateNewForm.php中未定义的函数mysql_query():10堆栈跟踪:#0{main}在第行的D:\xamp\htdocs\pw\CreateNewForm.php中抛出10@YelloTwist我修复了这个答案中的所有拼写错误。警告:您对参数化语句非常开放,应该使用参数化语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。