gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:43:13 26 4
gpt4 key购买 nike

这是我的表单 .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')");

最佳答案

您的表单中包含表单,这是错误的。这样做:

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

此外,将您的提交按钮放在表单中:

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

或者如果提交按钮在外面,将按钮的form属性设置为formid:

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

<input type="submit" value="Submit" form="myForm" />

关于php - 无法将数据从网页添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42642820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com