前台html
math
china
music
pe
php根据前台提交的数据查找mysql的数据
数据库中已经有一个名为book的数据表,字段分别有type,author 和time
$type=$_POST['type'];
//链接数据库
$db=new mysqli('localhost','root','','books');
//检测是否链接成功
if(mysqli_connect_erron()){
echo 'error connect to mysql server';
exit;
}
//查询语句
$query="select * from book where type='".$type."'";
//执行查询
$result=$db->query($query);
//返回结果的行数
$num_result=$result->num_rows;
//循环输出结果的内容
for($i=0;$i
$rows= $result->fetch_assoc();
echo $rows['type'].'
';
echo $rows['author'].'
';
echo $rows['time'];
}
// 释放结果集
$result->free();
//关闭数据库
$db->close();
?>
php根据前台输入的数据向数据库添加数据
$type=trim($_POST['type']);
$author=trim($_POST['author']);
$time=trim($_POST['time']);
if(!$type || !$author || !$time){
echo 'plz enter the full ';
exit;
}
if(!get_magic_quotes_gpc()){
$type=addslashes($type);
$author=addslashes($author);
$time=addslashes($time);
}
$db=new mysqli('localhost','root','','books');
if(mysqli_connect_erron()){
echo 'cant connect to mysql server';
exit;
}
$query="insert into book values ('".$type."','".$author."','".$time."')";
$result=$db->query($query);
if($result){
echo $db->affected_rows.'books insert into databases';
}else{
echo 'an error occurred';
}
$db->close();
?>