我正在尝试使用我的MySQLi插入数据,但我总是收到错误“没有记录”。我测试了它检索数据的查询,我之前有过,但我认为问题出在prepare语句中!
我感谢任何帮助!
<?php
require 'connect.php';
require '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)) {
echo $first_name;
echo $last_name;
echo $bio;
$insert = $db-> prepare("INSERT INTO people(first_name, last_name, bio, created)
VALUES(?, ?, ?, NOW() ) ");
$insert->bind_param('sss', $first_name, $lastname, $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();
}
}
//echo '<pre>', print_r($records);
?>
<!DOCTYPE html>
<html>
<head>
<title>People</title>
</head>
<body>
<h3>People</h3>
<?php
if (!count($records)) {
echo 'No records!';
}else {
?>
<table border = "1">
<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>
<label for="first_name">First name </label>
<input type="text" name="first_name" id="first_name" autocomplete="off">
</div>
<div>
<label for="last_name">Last name </label>
<input type="text" name="last_name" id="last_name" autocomplete="off">
</div>
<div>
<label for="bio">Bio </label>
<textarea name="bio" id="bio"></textarea>
</div>
<input type="submit" value="Insert">
</form>
</body>
</html>