php - 表单提交后清空数据库记录

标签 php mysql forms

我正在尝试将表单数据保存到我的数据库中,但我只得到空记录。 我尝试了很多解决方案,但我真的不知道错误在哪里。我快疯了!

这是我的表格:

<head>

<form action="uploadall.php" method="post">
Name: <input type="text" name="name"><br>
Autore: <input type="text" name="author"><br>
Descrizione: <textarea id="editordescription" name="description" cols="45" rows="15">
        </textarea>
        <script>
            CKEDITOR.replace( 'editordescription' );
        </script>
<br>Misure: <input type="text" name="misure"><br>
Data: <input type="text" name="date"><br>
    <input type="hidden" name="status" value="Disattivo" size="20">

<input type="submit">
</form>

这是我保存记录的 PHP 脚本:

     <?php

     // check if the form has been submitted. If it has, start to process the form and save it to the database
     if (isset($_POST['submit']))
     { 
     // get form data, making sure it is valid
     $name = mysqli_real_escape_string(htmlspecialchars($_POST['name']));
 $author = mysqli_real_escape_string(htmlspecialchars($_POST['author']));
  $description = mysqli_real_escape_string(htmlspecialchars($_POST['description']));
 $misure = mysqli_real_escape_string(htmlspecialchars($_POST['misure']));
 $date = mysqli_real_escape_string(htmlspecialchars($_POST['date']));
  $status = mysqli_real_escape_string(htmlspecialchars($_POST['status']));

     }


    $servername = "xxxxxxx";
    $username = "xxxxxxx";
    $password = "xxxxxxx";
    $dbname = "xxxxxxxxx";

    try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO exposition (name, author, description, misure, date, status)
    VALUES ('$name', '$author', '$description', '$misure', '$date', '$status')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;


    ?>

这是我目前在数据库中得到的内容:

enter image description here

最佳答案

首先,您在使用 mysqli_* 的某个时刻混合了 mysql api。在某些时候你使用 mysql_*他们不混合。和mysql_*函数已被贬值,更高版本的 php 不再支持它们。最好使用 mysqli 或 pdo。这个mysql_real_escape_string()mysqlo_real_escape_string()不够安全,不足以防止您遭受 sql 注入(inject)。解决方案很简单,最好开始使用 mysqli 准备语句或 pdo 准备语句。

另一个错误:<input type="text" name="name"> <input type="text" name="name">这两个输入字段具有相同的名称属性,php 只会读取其中之一。你会在这里得到一个 undefined index $misure = $_POST['misure'];您需要在开发过程中激活错误报告,以便可以看到错误和通知:

将其添加到每个 php 页面的顶部:ini_set('display_errors', 1); error_reporting(E_ALL);

还有date date 是 mysql 的保留字,因此您最好使用其他名称作为列名或添加反斜杠 date

Oh and your code never execute here :

if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
 $author = mysql_real_escape_string(htmlspecialchars($_POST['author']));
  $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
 $misure = mysql_real_escape_string(htmlspecialchars($_POST['misure']));
 $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
  $status = mysql_real_escape_string(htmlspecialchars($_POST['status']));

 }

这是为什么呢?因为你没有POSTsubmit属性名称。 <input type="submit">看?您提交的内容没有名称属性。所以。这意味着

这一切:

VALUES ('$name', '$author', '$description', '$misure', '$date', '$status')";这些都是 undefined variable 。我很惊讶为什么你的服务器不告诉你这一点,通过启用错误报告,你将得到所有这些。

这就是你需要做的来解决这个问题:

你的 html 端。

<form action="uploadall.php" method="post">
Name: <input type="text" name="name"><br>
Autore: <input type="text" name="author"><br>
Descrizione: <textarea id="editordescription" name="description" cols="45" rows="15">
        </textarea>
        <script>
            CKEDITOR.replace( 'editordescription' );
        </script>
<br>Misure: <input type="text" name="misure"><br>
Data: <input type="text" name="date"><br>
    <input type="hidden" name="status" value="Disattivo" size="20">

<input type="submit" name="submit">
</form>

uploadall.php

<?php

// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit'])) {

    $servername = "xxxxxxx";
    $username   = "xxxxxxx";
    $password   = "xxxxxxx";
    $dbname     = "xxxxxxxxx";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }


    //check your inputs are set and validate,filter and sanitize
    $name        = $_POST['name'];
    $author      = $_POST['author'];
    $description = $_POST['description'];
    $misure      = $_POST['misure'];
    $date        = $_POST['date'];
    $status      = $_POST['status'];



    //prepare and bind
    $sql = $conn->prepare("INSERT INTO exposition (name, author, description, misure, date, status)
VALUES (?,?,?,?,?,?)");
    $sql->bind_param("ssssss", $name, $author, $description, $misure, $date);

    if ($sql->execute()) {

        echo "New record created successfully";

    } else {

        //you have an error
    }

    $conn->close();

}

?>

祝你好运。

更新:

I corrected errors you told me and I am using PDO now but it still doesn't work

我从您上面的评论中读到了这一点,但您没有告诉我们错误是什么,但我相信它们就是我上面强调的错误。

通过 PDO,您将实现您的目标:

<?php

    //connection
    $servername = 'XXXXXXXXXXXXX';
    $dbname     = 'XXXXXXXXXXXXX';
    $username   = 'XXXXXXXXXXXXXX';
    $password   = 'XXXXXXXXX';
    $charset    = 'utf8';

    $dsn = "mysql:host=$servername;dbname=$dbname;charset=$charset";
    $opt = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false,
            ];


    $dbh = new PDO($dsn, $username, $password, $opt);

// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit'])) {




    //check your inputs are set and validate,filter and sanitize
    $name        = $_POST['name'];
    $author      = $_POST['author'];
    $description = $_POST['description'];
    $misure      = $_POST['misure'];
    $date        = $_POST['date'];
    $status      = $_POST['status'];

    //prepare and bind
    $stmt = $dbh->prepare("INSERT INTO exposition (name, author, description, misure, date, status)VALUES (?,?,?,?,?,?)");
    if ($stmt->execute(array($name,$author,$description,$misure,$date,$status))) {

        echo "New Record inserted success";
    }

}

?> 

关于php - 表单提交后清空数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42972503/

相关文章:

mysql - 如何在插入该表后立即通过触发器更新属性的值?

Mysql子查询外部Where

javascript - 在 Angular 中访问没有表单的输入的 $pristine 属性?

javascript - 带有输入字段的重复 DIV

php - 我无法在 Mysql 中的另一个查询中运行查询。表消息。显示收件箱

mysql - 将未知数量的数据显示为列名

python - Django - GenericForeignKey 的动态表单

html - request.FILES 在文件上传时始终为空

php - JavaScript 不工作

使用 JSONParser 的 java.lang.NullpointerException