【问题标题】:Empty database records after a form submit表单提交后清空数据库记录
【发布时间】:2017-03-23 09:50:50
【问题描述】:

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

这是我的表格:

<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;


    ?>

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

【问题讨论】:

  • 没有出现错误?请停止使用mysql_* 函数,它们已经被贬低了。而是使用 [PDO](php.net/manual/en/book.pdo.php} 或 MySQLi
  • mysql_real_escape_string 你的连接是mysqli
  • 你在创建连接之前正在使用mysql函数
  • 我纠正了你告诉我的错误,我现在正在使用 PDO,但它仍然不起作用

标签: php mysql forms


【解决方案1】:

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

另一个错误:&lt;input type="text" name="name"&gt;&lt;input type="text" name="name"&gt; 这两个输入字段具有相同的名称属性 php 只会读取一个。你会在这里得到一个未定义的索引$misure = $_POST['misure'];你需要在开发过程中激活错误报告,这样你才能看到你的错误和通知:

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

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

哦,你的代码永远不会在这里执行:

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']));

 }

这是为什么呢?因为您没有具有 submit 属性名称的 POST 值。 &lt;input type="submit"&gt; 看到了吗?您的提交没有名称属性。所以。这意味着

这一切:

VALUES ('$name', '$author', '$description', '$misure', '$date', '$status')"; 这些都是未定义的变量。我很惊讶为什么你的服务器不告诉你,启用错误报告后你会得到所有这些。

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

你的 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>

上传所有.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();

}

?>

祝你好运。

更新:

我纠正了你告诉我的错误,我现在正在使用 PDO,但它仍然 不工作

我从您上面的 cmets 中读到了这一点,但您没有告诉我们错误是什么,但我相信它们是我在上面突出显示的错误。

使用 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";
    }

}

?> 

【讨论】:

  • 第一次,它不起作用(在第一个 uploadall.php 中,您错过了 bind_param 中的 '$status'。我更正了它,它起作用了!谢谢!我还添加了 $description = strip_tags ($description) 因为它也保存了 html 标签 :) 非常感谢! :)
【解决方案2】:

变量名问题 E.g Name: &lt;input name="name"&gt; 和 : Misure: &lt;input name="name"&gt;.这肯定不一样。

同样,&lt;input type="submit"&gt; 应该是 &lt;input type="submit" name="submit"&gt;。 希望对您有所帮助。

【讨论】:

    【解决方案3】:

    您在 INSERT 查询中使用的变量超出了您从表单中获取数据的第一个 if 块的范围。如果变量在第一个 if 块之前初始化,它可能会起作用。如下所示..

     $name = ""; $author = "";$description = "";$misure = "";$date = "";$status=";
    
    
    if (isset($_POST['submit'])){ // as is}
    

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 1970-01-01
      • 2019-02-28
      • 2021-09-24
      • 2014-02-18
      • 2015-07-11
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多