【问题标题】:My simple html php form not inserting into database我的简单 html php 表单没有插入数据库
【发布时间】:2018-11-13 19:16:15
【问题描述】:

由于某种原因,此表单没有插入到我的数据库中。

HTML

<form action="../php/register.php" method="post">
    <div id ="personal-form">
        <h4><b>Personal Details:</b></h4>
        <hr>

        <div class="form-group">
            <label class="sr-only" for="first-name">First name</label>
            First Name

            <input type="text" name="firstname" placeholder="" 
                   class="form-control" id="firstname">

            <button type="submit" class="btn btn-next" id="submit">
                Submit
            </button>
        </center>
        </div>
    </div>
</form>

php/register.php

<?php
    include('connect.php');

    if(isset($_POST["submit"])) {
        $firstname = $_POST["firstname"];
        $stmt = $conn->prepare("INSERT INTO storeowners (firstname) VALUES 
(:firstname)");

        $stmt->bindParam(':firstname', $firstname);
        $stmt->execute();

        header("location: next.php");
    }
?>

这是 connect.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=blaza", $username, 
$password);
        //set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "success";
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
?>

当我点击提交按钮时,它会显示带有消息 successphp/register.php 页面,这与 connect.php 数据库连接成功时的代码。 我不知道问题出在哪里,因为它没有将名字存储到数据库中并且没有给出错误。

【问题讨论】:

  • for="first-name" & id="firstname" 不匹配。
  • 你有一个关闭的&lt;/center&gt;标签,没有打开一个(不是php问题,而是一个问题)

标签: php mysql pdo


【解决方案1】:
if(isset($_POST["submit"]))

你没有带有name=submit 的表单控件,所以这个条件永远不会成立。

你无条件连接到数据库,但你从不使用连接做任何事情。

【讨论】:

    【解决方案2】:

    name="submit" 添加到您的按钮。

    <form action="../php/register.php" method="post">
          <div id ="personal-form">
            <h4><b>Personal Details:</b></h4>
            <hr>
            <div class="form-group">
               <label class="sr-only" for="first-name">First name</label>
                  First Name
               <input type="text" name="firstname" placeholder="" class="form-control" id="firstname">
               <button name="submit" type="submit" class="btn btn-next" id="submit">Submit</button></center>
           </div>
         </div>
     </form>
    

    【讨论】:

      【解决方案3】:

      添加这个而不是 $_POST["submit"]

      if(isset($_POST["firstname"]))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-04
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多