我正在尝试将数据插入到我的数据库中,但输入不会存储在 mysqli 中。
如果我将它插入数据库,它会显示输出正常,但是当我使用我的表单时,没有任何内容存储。一双新眼睛可以看到我做错了吗?
这是我的代码:
<?php
error_reporting(0);
require 'db/connect.php';
require 'functions/security.php';
$records = array();
if(!empty($_POST)) {
if(isset($_POST['first_name'], $_POST['last_name'], $_POST['bio'])) {
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$bio = trim($_POST['bio']);
if(!empty($first_name) && !empty($last_name) && !empty($bio)) {
$insert = $db->prepare("INSERT INTO people (first_name, last_name, bio, created) VALUES (?, ?, ?, NOW())");
$insert->bind_param('sss', $first_name, $last_name, $bio);
if($insert->execute()) {
header('Location: index.php');
die();
}
}
}
}
if($results = $db->query("SELECT * FROM people")) {
if($results->num_rows) {
while($row = $results->fetch_object()) {
$records[] = $row;
}
$results->free();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>People</title>
</head>
<body>
<h3>People</h3>
<?php
if(!count($records)) {
echo 'No records';
} else {
?>
<table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Bio</th>
<th>Created</th>
</tr>
</thead>
<tbody>
<?php
foreach($records as $r) {
?>
<tr>
<td><?php echo escape($r->first_name); ?></td>
<td><?php echo escape($r->last_name); ?></td>
<td><?php echo escape($r->bio); ?></td>
<td><?php echo escape($r->created); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
<hr>
<form action="" method="post">
<div class="field">
<label for ="first_name">First name</label>
<input type="text" name="first_name" id="first_name" autocomplete="off">
</div>
<div class="field">
<label for ="last_name">Last name</label>
<input type="text" name="Last_name" id="Last_name" autocomplete="off">
</div>
<div class="field">
<label for ="bio">Bio</label>
<textarea name="bio" id="bio"></textarea>
</div>
<input type="submit" value="insert">
</form>
</body>
</html>
答案 0 :(得分:2)
将name="Last_name"
更改为name="last_name"
,因为您的帖子$_POST['last_name']
已将其设为小写