【发布时间】:2014-11-05 18:30:27
【问题描述】:
我使用以下代码将数据插入到名为 sample 的数据库中,该表也称为 sample.. 它们是四个字段 first_name、last_name、bio、created。 当我
但是当我使用这段代码时,有一个 html 表单来输入数据..它不起作用。我没有收到任何错误消息...数据没有插入到数据库中..
[代码]
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
//require 'functions/security.php';
$db = new mysqli('localhost', 'root', '', 'sample');
//check connection
if($db->connect_errno) {
die('sorry, we are having some problems');
}else {
echo 'connected';
}
$records = array();
if(!empty($_POST)) {
if(isset($_POST['first_name'], $_POST['lats_name'],$_POST['bio'])){
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$bio = mtrim($_POST['bio']);
if(!empty($first_name) && !empty($last_name) && !empty($bio) ){
$insert = $db->prepare
("INSERT INTO sample(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 sample")) {
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
</head>
<body>
<h3>Sample</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 $r->first_name; ?></td>
<td><?php echo $r->last_name; ?></td>
<td><?php echo $r->bio ?></td>
<td><?php echo $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">
</div>
<div class="field">
<label for="last_name">last_name" </label>
<input type ="text" name="last_name" id="last_name">
</div>
<div class="field">
<label for="bio">bio" </label>
<textarea name="bio" id="bio"></textarea>
<input type="submit" value="insert">
</div>
</form>
</hr>
</body>
</html>
[/code]
【问题讨论】:
-
您没有检查错误。这就是你看不到任何东西的原因。
-
因为
name="last_name"+$_POST['lats_name']= 没有爱。 -
还有
mtrim($_POST['bio'])没有mtrimPHP函数。
标签: php html mysql mysqli xampp