【发布时间】: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();
}
?>
当我点击提交按钮时,它会显示带有消息 success 的 php/register.php 页面,这与 connect.php 数据库连接成功时的代码。 我不知道问题出在哪里,因为它没有将名字存储到数据库中并且没有给出错误。
【问题讨论】:
-
for="first-name"&id="firstname"不匹配。 -
你有一个关闭的
</center>标签,没有打开一个(不是php问题,而是一个问题)