index.php
<?php
//connect database
require_once 'config.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<h2>Write message</h2>
<form action="add.php" method="GET">
<p><input type="text" name="name" placeholder="Your name"></p>
<p><input type="text" name="email" placeholder="Your email"></p>
<textarea name="text" cols="30" row="10" placeholder="Write something..."></textarea>
<p><input type="submit" value="submit"> <input type="reset" value="reset"></p>
</form>
<hr>
<h2>Message List</h2>
<ul>
<?php
//The latest message is on top
$sql = " SELECT * FROM 'mmb' ORDER BY 'mmb' . 'id' DESC";
//excute sql
$result = $conn->query($sql);
if($result->num_rows > 0){
//output database
while($row = $result->fetch_assoc()){
?>
<li>
<p><?php echo $row["id"];?> Floor </p>
<p>Text:<?php echo $row["text"];?></p>
<p>Name:<?php echo $row["name"];?></p>
<p>Email:<?php echo $row["email"];?></p>
<p>Time:<?php echo $row["time"];?></p>
<p>
<a href = "edit.php?id=<?php echo $row['id'];?>">Edit</a>
<a href = "del.php?id=<?php echo $row['id'];?>">Delet</a>
</p>
</li>
<?php
}
}else{
echo "There is no message yet";
}
?>
</ul>
</body>
</html>
add.php
<?php
//connet database
require_once 'config.php';
$name = $_GET["name"];
$email = $_GET["email"];
$text = $_GET["text"];
$time= date("Y-m-d H:i:s", time()); //get current time
//insert
$sql = "INSERT INTO 'mmb' ('id', 'name', 'email', 'text', 'time') VALUES (NULL, '$name', '$email', '$text', '$time')";
//query
$conn->query($sql);
//back to index
header("Location: index.php");
?>
我创建了database.php 和config.php 连接数据库,然后在文本框填写也可以送到php my admin. 但是下面留言列表里面就是显示没有留言。小白一个,完全不懂为什么。
index.php
<?php
//connect database
require_once 'config.php';
$title = "网站标题";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<h2>Write message</h2>
<form action="add.php" method="POST">
<p><input type="text" name="name" placeholder="Your name"></p>
<p><input type="text" name="email" placeholder="Your email"></p>
<textarea name="text" cols="30" row="10" placeholder="Write something..."></textarea>
<p><input type="submit" value="submit"> <input type="reset" value="reset"></p>
</form>
<hr>
<h2>Message List</h2>
<ul>
<?php
//The latest message is on top
$sql = " SELECT * FROM mmb";
//excute sql
$result = $connID->query($sql);
if($result->num_rows > 0){
//output database
while($row = $result->fetch_assoc()){
?>
<li>
<p><?php echo $row["id"];?> Floor </p>
<p>Text:<?php echo $row["text"];?></p>
<p>Name:<?php echo $row["name"];?></p>
<p>Email:<?php echo $row["email"];?></p>
<p>Time:<?php echo $row["time"];?></p>
<p>
<a href = "edit.php?id=<?php echo $row['id'];?>">Edit</a>
<a href = "del.php?id=<?php echo $row['id'];?>">Delet</a>
</p>
</li>
<?php
}
}else{
echo "There is no message yet";
}
?>
</ul>
</body>
</html>
config.php
<?php
$config=array(
'db_host' =>'localhost',
'db_user' =>'数据库用户名',
'db_psw' =>'数据库密码',
'db_name' =>'数据库名',
'db_charset'=>'utf8'
);
$connID=new mysqli($config['db_host'],$config['db_user'],$config['db_psw'],$config['db_name']);
if(!$connID){
echo("数据库连接失败");
}
?>
add.php
<?php
//connet database
require_once 'config.php';
$name = $_POST["name"];
$email = $_POST["email"];
$text = $_POST["text"];
$time= time(); //get current time
//insert
$sql = "INSERT INTO mmb (name, email, text, time) VALUES ('".$name."', '".$email."', '".$text."', '".$time."')";
//query
$query = $connID->query($sql);
//back to index
header("Location: index.php");
?>
想问问$config=array 放在主页还是add.php
$config=array(
$config=array(
是连接数据库配置文件,我忘了打上去,放在config.php里