版权声明:版权归作者所有 https://blog.csdn.net/qq_36772866/article/details/82427589
前几天,跟同学交流了一下,打算做一个毕业求职意向调查表
所以就有了这篇文章,
首先我们要根据表单,提取字段
创建数据表
form.sql
-- 创建数据表
-- 根据表单数据创建
create table form (
//主键id自动递增
id int primary key not null auto_increment,
//性别
sex varchar(4) not null,
//学号
num varchar(20) not null,
//用户名
username varchar(20) not null,
// 手机号码
phone varchar(11) not null,
// 英语学习程度
english varchar(10) not null,
// 中文学习程度
chinese varchar(10) not null,
// 粤语学习程度
cantonese varchar(10) not null,
// 计算机等级
computer varchar(10) not null,
// 身高
height varchar(10) not null,
// 兴趣爱好
hobby varchar(100) not null,
// 目标
aim varchar(100) not null,
// 地点
place varchar(100) not null,
// 工作地点
work varchar(100) not null,
// 薪资
pay varchar(100) not null
) charset=utf8;// 默认utf8编码格式
表单提交
表单设计的时候要特别注意的就是
那些单选按钮的的设计
还有多选按钮的设计
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>就业意向</title>
</head>
<body>
<form action="form.php" method="post">
<table width="100%" align="center" cellspacing="0" cellpadding="0">
<tr height="60">
<th colspan="2">
就业意向调查 <b style="color:red;">(每项都是必填的)</b>
</th>
</tr>
<tr height="40">
<td>性别</td>
<td>
<input type="radio" name="sex" value="男"> 男
<input type="radio" name="sex" value="女"> 女
</td>
</tr>
<tr height="40">
<td>学号</td>
<td>
<input type="text" name="num" >
</td>
</tr>
<tr height="40">
<td>姓名</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr height="40">
<td>手机</td>
<td>
<input type="text" name="phone">
</td>
</tr>
<tr height="40">
<td>英语</td>
<td>
<input type="radio" name="English" value="二级">二级
<input type="radio" name="English" value="四级">四级
<input type="radio" name="English" value="六级">六级
<input type="radio" name="English" value="待定">待定
</td>
</tr>
<tr height="40">
<td>国语</td>
<td>
<input type="radio" name="Chinese" value="流利">流利
<input type="radio" name="Chinese" value="会说">会说
<input type="radio" name="Chinese" value="能听">能听
<input type="radio" name="Chinese" value="不懂">不懂
</td>
</tr>
<tr height="40">
<td>粤语</td>
<td>
<input type="radio" name="Cantonese" value="流利">流利
<input type="radio" name="Cantonese" value="会说">会说
<input type="radio" name="Cantonese" value="能听">能听
<input type="radio" name="Cantonese" value="不懂">不懂
</td>
</tr>
<tr height="40">
<td>计算机水平</td>
<td>
<input type="radio" name="computer" value="一级">一级
<input type="radio" name="computer" value="二级">二级
<input type="radio" name="computer" value="三级">三级
<input type="radio" name="computer" value="待定">待定
</td>
</tr>
<tr height="40">
<td>身高(cm)</td>
<td>
<input type="text" name="height" placeholder="请输入您的身高">
</td>
</tr>
<tr height="40">
<td>爱好特长</td>
<td>
<input type="text" name="hobby" placeholder="请输入您的爱好">
</td>
</tr>
<tr height="40">
<td>意向(可多选)</td>
<td>
<input type="checkbox" name="aim[]" value="企事业单位">企事业单位
<input type="checkbox" name="aim[]" value="考公务员">考公务员
<br>
<input type="checkbox" name="aim[]" value="三支一扶">三支一扶
<input type="checkbox" name="aim[]" value="山区计划西部计划">山区计划
<br/>
<input type="checkbox" name="aim[]" value="考研">考研
<input type="checkbox" name="aim[]" value="留学">留学
<input type="checkbox" name="aim[]" value="自主创业">自主创业
</td>
</tr>
<tr height="40">
<td>地域(具体镇区)</td>
<td>
<input type="text" name="place" placeholder="例:中山石岐区">
</td>
</tr>
<tr height="40">
<td>职务或岗位</td>
<td>
<input type="text" name="work" placeholder="例:技术类/行政/销售">
</td>
</tr>
<tr height="40">
<td>期望试用薪酬</td>
<td>
<input type="text" name="pay" placeholder="例:3000">
</td>
</tr>
<tr height="40">
<td></td>
<td>
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
比较丑,因为时间紧急啊,
就没加样式了
后台配置文件
config.php
<?php
$cfg = array(
'host'=>"localhost",
'username'=>'root',
'pwd'=>'root',
'dbname'=>'form',
'charset'=>'utf8'
);
return $cfg;
?>
form.php
<?php
header("content-type:text/html;charset=utf-8");
// 引入数据库配置文件
$cfg = include('./config.php');
// var_dump($_POST);
// 封装一个函数用来检测
// 数据是否为空
function e(){
// 如果下面这些有任意一个数据为空
// 返回true
if(
$_POST['sex'] == ''
|| $_POST['num'] == ''
|| $_POST['username'] == ''
|| $_POST['phone'] == ''
|| $_POST['English'] == ''
|| $_POST['Chinese'] == ''
|| $_POST['Cantonese'] == ''
|| $_POST['computer'] == ''
|| $_POST['height'] == ''
|| $_POST['hobby'] == ''
|| $_POST['aim'] == ''
|| $_POST['place'] == ''
|| $_POST['work'] == ''
|| $_POST['pay'] == ''){
return true;
}
}
if(e()){
// 判断是否有数据提交过来
// 如果为空给出提示
echo "<script>
alert('请将必填项填好在提交');
location = 'index.html';
</script>";
return ;
}
// echo $_POST['sex'];
// echo "<br/>";
// echo $_POST['number'];
// echo "<br/>";
// echo $_POST['username'];
// echo "<br/>";
// echo $_POST['phone'];
// echo "<br/>";
// echo $_POST['English'];
// echo "<br/>";
// echo $_POST['Chinese'];
// echo "<br/>";
// echo $_POST['Cantonese'];
// echo "<br/>";
// echo $_POST['computer'];
// echo "<br/>";
// echo $_POST['height'];
// echo "<br/>";
// echo $_POST['hobby'];
// echo "<br/>";
// var_dump( $_POST['aim'] );
// echo "<br/>";
// echo $_POST['place'];
// echo "<br/>";
// echo $_POST['work'];
// echo "<br/>";
// echo $_POST['pay'];
// echo "<br/>";
// 获取表单提交过来的数据
// 准备查输入数据库
$sex = $_POST['sex'];
$num = $_POST['num'];
$username = $_POST['username'];
$phone = $_POST['phone'];
$english = $_POST['English'];
$chinese = $_POST['Chinese'];
$cantonese = $_POST['Cantonese'];
$computer = $_POST['computer'];
$height = $_POST['height'];
$hobby = $_POST['hobby'];
$aim = $_POST['aim'];
$place = $_POST['place'];
$work = $_POST['work'];
$pay = $_POST['pay'];
// var_dump($aim);
// 要处理一下那个$aim的数据因为传过来的是数组
// 要处理成字符串
// echo count($aim);
// count()函数可以计算数组的长度
$aimStr='';
for($i = 0; $i < count($aim); $i++){
$aimStr .= $aim[$i]."/";
}
// 截取去掉最后一个字符的剩下字符串
$aimStr = substr($aimStr,0,-1);
// echo $aimStr;
// 截取字符串
// mb_strlen()获取字符串长度
// $len = mb_strlen($aimStr);
// 只执行1次
// $count = 1;
// $aimStr = str_replace("/","",$aimStr,$count);
// 切割字符串
// 变成数组
// $aimArr = explode("/",$aimStr);
// // var_dump($aimArr);
// $str = '';
// for($i = 0; $i < count($aimArr)-1; $i++){
// // 将最后一个字符去掉
// $str .= $aimArr[$i].'/';
// }
// echo $str;
// $sqlArr = [];
// $sqlArr['sex'] = $sex;
// $sqlArr['number'] = $number;
// $sqlArr['username'] = $username;
// $sqlArr['phone'] = $phone;
// $sqlArr['english'] = $english;
// $sqlArr['chinese'] = $chinese;
// $sqlArr['cantonese'] = $cantonese;
// $sqlArr['computer'] = $computer;
// $sqlArr['height'] = $height;
// $sqlArr['hobby'] = $hobby;
// $sqlArr['aim'] = $aimStr;
// $sqlArr['place'] = $place;
// $sqlArr['work'] = $work;
// $sqlArr['pay'] = $pay;
$sqlArr = array(
"sex"=>$sex,
"num"=>$num,
"username"=>$username,
"phone"=>$phone,
"english"=>$english,
"chinese"=>$chinese,
"cantonese"=>$cantonese,
"computer"=>$computer,
"height"=>$height,
"hobby"=>$hobby,
"aim"=>$aimStr,
"place"=>$place,
"work"=>$work,
"pay"=>$pay
);
// 拼接查询语句
$sql = "insert into "."form";
$sql.=" (".implode(',',array_keys($sqlArr)).") ";
$sql.=" values ('".implode("','",array_values($sqlArr))."')";
// echo $sql;
// 链接数据库
$conn = new Mysqli($cfg['host'],$cfg['username'],$cfg['pwd'],$cfg['dbname']);
$conn->set_charset('utf8');
if($conn->connect_error){
echo "数据库连接失败了"." 出错的原因是 ".$conn->connect_error;
}
// 插入数据
if ($conn->query($sql) === TRUE) {
echo "<script>
alert('数据提交成功,一个人只允许提交一次');
location = 'index.html'
</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// // 关闭连接
// $conn->close();
?>
我好像没有创建数据库,和数据表