无法将数据从网页添加到数据库

时间:2017-03-07 07:42:51

标签: php html sql

这是我的表单.html文件连接到adddatabase.php页面。有第一个名字和第二个名字

       <form action="addtodatabase.php" method="post">
       <div class="container">
       <form class="form-inline">
       <fieldset>
       <legend>Security Department User Registration</legend>
       <div class="form-group">
       <label for="Firstname">First Name</label>
       <input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
       </div>

       <div class="form-group">
       <label for="Secondname">Second Name</label>
       <input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
        </div>
       </form>

       <button type="submit" class="btn btn-default">Submit</button>
       </form>

addtodatabase.php页面如下。

     if (isset($_POST)) { 
     $firstname  = isset($_POST['firstname']) ? $_POST['firstname'] : '';
     $secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';
     echo 'Your first name is ' .$firstname. '<br>'; 
     echo 'Your second name is ' .$secondname. '<br>'; 
     }
     mysqli_query($connect,"INSERT INTO form_details(firstname,secondname)
            VALUES('$firstname','$secondname')");

1 个答案:

答案 0 :(得分:0)

您的表单中有错误的表单。这样做:

<form method="POST" action="addtodatabase.php" >
      <!-- Your inputs -->
</form>

另外,将提交按钮放在表单中:

<form method="POST" action="addtodatabase.php" >
      <!-- Your inputs -->
      <input type="submit" value="Submit" />
</form>

如果提交按钮在外面,请将按钮的form属性设置为id的{​​{1}}:

form