<?php
function add(){
//先校验是否非空
if(empty($_POST['title'])){
$GLOBALS['error_message']='请输入标题';
return;
}
if(empty($_POST['artist'])){
$GLOBALS['error_message']='请输入歌手';
return;
}
if(!(isset($_POST['gender'])&&$_POST['gender']!=='-1')){
$GLOBALS['error_message']='请输入性别';
return;
}
$title=$_POST['title'];
$artist=$_POST['artist'];
$gender=$_POST['gender'];
if (empty($_FILES['images'])) {
$GLOBALS['error_message'] = '请正常使用表单';
return;
}
$images = $_FILES['images'];
// 准备一个容器装所有的海报路径
// 遍历这个文件域中的每一个文件(判断是否成功、判断类型、判断大小、移动到网站目录中)
for ($i = 0; $i < count($images['name']); $i++) {
// $images['error'] => [0, 0, 0]
if ($images['error'][$i] !== UPLOAD_ERR_OK) {
$GLOBALS['error_message'] = '上传海报文件失败1';
return;
}
//$ext=pathinfo($_FILES['images']['name'],PATNINFO_EXTENSION);
$dest = 'uploads/' . uniqid() . iconv('UTF-8', 'GBK', $images['name'][$i]);
// move_uploaded_file 在 Windows 中文系统上要求传入的参数如果有中文必须是 GBK 编码
// 切记在接收文件时注意文件名中文的问题,通过iconv函数转换中文编码为 GBK 编码
if (!move_uploaded_file($images['tmp_name'][$i], $dest)) {
$GLOBALS['error_message'] = '上传海报文件失败2';
return;
}
}
$con=mysqli_connect('localhost','root','123456','test');
if(!$con){
exit('<h1>数据库链接失败</h1>');
}
//var_dump("insert into demo values (null, '{$title}', '{$artist}',{$gender},'{$images}');");
//开始查询
// insert into demo values (null,'sds','as',0,'D:\www\uploads\5ccf02679ccf01534773864265.jpg');
//$query=mysqli_query($con,"insert into users values ('{$title}', '{$artist}',{$gender},'{$images}');");
$query = mysqli_query($con, "insert into users values (null, '{$title}','{$artist}', {$gender},'{$images}');");
if (!$query) {
$GLOBALS['error_message'] = '查询过程失败';
return;
}
}
//先校验是否是接收到post文件
if($_SERVER['REQUEST_METHOD']=='POST'){
add();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加新音乐</title>
<link rel="stylesheet" href="bootstrap.css">
</head>
<body>
<div class="container py-5">
<h1 class="dispaly-4">添加新音乐</h1>
<?php if(isset($error_message)):?>
<div class="alret alert-danger" role="alert">
<?php echo $error_message; ?>
</div>
<?php endif?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">标题</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="artist">歌手</label>
<input type="text" class="form-control" id="artist" name="artist">
</div>
<div class="form-group">
<label for="gender">性别</label>
<select class="form-control" id="gender" name="gender">
<option value="-1">选择性别</option>
<option value="1">男</option>
<option value="0">女</option>
</select>
</div>
<div class="form-group">
<label for="images">海报</label>
<!-- multiple 可以让一个文件域多选 -->
<input type="file" class="form-control" id="images" name="images[]" accept="image/*" >
</div>
<button class="btn btn-primary btn-block">保存</button>
</form>
</div>
</body>
</html>
增加hh的易途会员 易途旅游,致力于做特价门票和吃喝玩乐,低价的门票,让你省钱省事。