Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 信息不提交到数据库中_Php_Html_Mysql - Fatal编程技术网

Php 信息不提交到数据库中

Php 信息不提交到数据库中,php,html,mysql,Php,Html,Mysql,我们有一个学校作业,我试着构建应用程序,但是一些我想插入数据库的文本没有提交 我尝试了不同的方法,但是页面也没有显示错误 这是我插入页面的代码 <head> </head> <body> <form action="index.php" method="post"> ID: <input type="text" name="id"><br/> Server: <input type="text" nam

我们有一个学校作业,我试着构建应用程序,但是一些我想插入数据库的文本没有提交

我尝试了不同的方法,但是页面也没有显示错误

这是我插入页面的代码

<head>
</head>
<body>
<form action="index.php" method="post">
    ID: <input type="text" name="id"><br/>
    Server: <input type="text" name="Server"><br/>
    Student: <input type="text" name="Student"><br/>
    Docent: <input type="text" name="Docent"><br/>
    Project: <input type="text" name="Project"><br/>
    Startdatum: <input type="text" name="Startdatum"><br/>
    Einddatum: <input type="text" name="Einddatum"><br/>
    <input type="submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])) {

    $con = mysqli_connect("localhost", "root", "usbw", "serverruimte");
    if(!$con) {
        die(mysqli_connect_error());
    }

    $sql = "INSERT INTO serverruimte (id,Server,Student,Docent,Project,startdatum,einddatum) VALUES ('$_POST[id]','$_POST[Server]','$_POST[Student]','$_POST[Docent]','$_POST[Project]','$_POST[startdatum]','$_POST[einddatum]')";

    $result = mysqli_query($con, $sql);

    if($result) {
        echo "Opslaan voltooid!";
    } else {
        echo mysqli_error($con);
    }

    mysqli_close($con);
}
?>
</body>
</html>

ID:
服务器:
学生:
讲解员:
项目:
起始日期:
Einddatum:
更改此行:

$sql = "INSERT INTO serverruimte (id,Server,Student,Docent,Project,startdatum,einddatum) VALUES ('$_POST[id]','$_POST[Server]','$_POST[Student]','$_POST[Docent]','$_POST[Project]','$_POST[startdatum]','$_POST[einddatum]')";
致:

此更改背后的原因是您的查询错误,原因如下:

  • 您使用的是字符串,而不是连接来自$\u POST的实际值
  • $\u POST中的某些索引拼写错误。例如:

    $\u POST[einddatum]应该是$\u POST['einddatum']


也可以认为,此代码易受

< Stime>警告:的影响,并且应该使用参数化<强>准备好的语句< /强>,而不是手动构建查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行

使用MySQLi时,应该启用自动错误报告,而不是手动检查错误。手动检查错误是一种糟糕的做法,非常容易出错,应该不惜一切代价避免。让MySQLi抛出异常而不捕获它们。看

打开MySQLi连接时,必须指定正确的字符集。建议使用
utf8mb4

if (isset($_POST['submit'])) {
    // Enable automatic error reporting
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    // Create new instance of MySQLi class
    $con = new mysqli("localhost", "root", "usbw", "serverruimte");
    // Set correct charset. Important!
    $con->set_charset('utf8mb4');

    $stmt = $con->prepare('INSERT INTO serverruimte (id,Server,Student,Docent,Project,startdatum,einddatum) VALUES (?,?,?,?,?,?,?)');
    $stmt->bind_param('sssssss', $_POST['id'], $_POST['Server'], $_POST['Student'], $_POST['Docent'], $_POST['Project'], $_POST['startdatum'], $_POST['einddatum']);
    $stmt->execute();

    echo "Opslaan voltooid!";

    mysqli_close($con);
}

除了当前答案之外,您还可以使用
{}
值(“{$\u POST['id']}',“{$\u POST['Server']}”
if (isset($_POST['submit'])) {
    // Enable automatic error reporting
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    // Create new instance of MySQLi class
    $con = new mysqli("localhost", "root", "usbw", "serverruimte");
    // Set correct charset. Important!
    $con->set_charset('utf8mb4');

    $stmt = $con->prepare('INSERT INTO serverruimte (id,Server,Student,Docent,Project,startdatum,einddatum) VALUES (?,?,?,?,?,?,?)');
    $stmt->bind_param('sssssss', $_POST['id'], $_POST['Server'], $_POST['Student'], $_POST['Docent'], $_POST['Project'], $_POST['startdatum'], $_POST['einddatum']);
    $stmt->execute();

    echo "Opslaan voltooid!";

    mysqli_close($con);
}