今天写一个将数据插入到数据库中的实例,下面我先说一个我的感想:昨天晚上做了一个梦,梦到自己困难重重,:我都那么那么努力了,为什么还是困难重重:,后来,想了很多很多,其实,我们活着就是一种最大的幸福,我们身边爱我们的人,是再多的金钱都买不到的,珍惜每一天,也许,明天你就可以成功,就看你的坚持,加油,追梦的人.
好了,我们回归正题,直接上代码,注释比较全:html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>这是php插入数据的练习</title>
</head>
<body background="timg.jpg">
<h4 align="center"> 请输入您要插入的数据</h4>
<form action="inserting.php" method="post" enctype="multipart/form-data" align="center">
姓名:<input type="text" name="name" size="10"><br/>
年龄:<input type="text" name="age" size="10"><br/>
班级:<input type="text" name="class" size="10"><br/>
id:<input type="text" name="id" size="10"><br/>
性别:<input type="text" name="sex" size="10"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
php:<?php
header("content-type:text/html;charset=utf-8");
$name=$_POST['name'];
$age=$_POST['age'];
$id=$_POST['id'];
$sex=$_POST['sex'];
$class=$_POST['class'];
if(!$name && !$age and !$id and !$sex and !$class){
echo("Error:There is no data found");
exit;
}
if(!$name || !$age or !$id or !$sex or !$class){
echo("Error:There is lack os some data");
exit;
}
//然后我们判断上传的数据是否符合规范
if(!get_magic_quotes_gpc()){
$name=addslashes($name);
$age=addslashes($age);
$id=addslashes($id);
$sex=addslashes($sex);
$class=addslashes($class);
}
//下面我们对数据库进行操作
@ $db=mysqli_connect("localhost","root","dckandcqq123456","dck");
if(mysqli_connect_errno()){
echo("Error:Couldnot connect the database");
exit;
}
$q="insert into student values($id,'$name',$age,'$class','$sex')";
$result=mysqli_query($db,$q);
if(!$result){
echo("fail to insert data");
}else{
echo("sucess in insert data");
}
@ mysqli_free_result($result);
mysqli_close($db);
?>
运行结果:
前:
后:
欢迎大家多多留言交流