<?PHP
header("Content-Type: text/html; charset=utf-8");
include('conn.php');//链接数据库
//1、设置变量来接收表单提交过来的值
$article_name = $_POST['art_name'];
$checkbox = $_POST['checkbox'];
$radio_box = $_POST['radio_box'];
$content = $_POST['content'];
$input_name = $_POST['input_name'];
$input_number = $_POST['input_number'];
$input_title = $_POST['input_title'];
$input_describe = $_POST['input_describe'];
//2、将数组转为字符串
$check_box=implode(',',$checkbox);
//获取上传文件信息
$upfile=$_FILES["input_ewm"];
$upfile2=$_FILES["input_image"];
//定义允许的类型
$typelist=array("image/jpeg","image/jpg","image/png","image/gif");
$path="../upload/";//定义一个上传后的目录
//过滤上传文件的错误号
if($upfile["error"] && $upfile2["error"]>0){
switch($upfile['error'] && $upfile2["error"]){//获取错误信息
case 1:
$info="上传得文件超过了 php.ini中upload_max_filesize 选项中的最大值.";
break;
case 2:
$info="上传文件大小超过了html中MAX_FILE_SIZE 选项中的最大值.";
break;
case 3:
$info="文件只有部分被上传";
break;
case 4:
$info="没有文件被上传.";
break;
case 5:
$info="找不到临时文件夹.";
break;
case 6:
$info="文件写入失败!";break;
}die("上传文件错误,原因:".$info);
}
//上传文件大小的过滤
if($upfile['size'] && $upfile2['size']>2000000){
die("上传文件大小超出限制");
}
//4.类型过滤
if(!in_array($upfile["type"],$typelist)){
die("上传文件类型非法!".$upfile["type"]);
}
if(!in_array($upfile2["type"],$typelist)){
die("上传文件类型非法!".$upfile2["type"]);
}
//上传后的文件名定义(随机获取一个文件名)
$fileinfo=pathinfo($upfile["name"]);//解析上传文件名字
do{
$newfile=date("YmdHis").rand(1000,9999).".".$fileinfo["extension"];
}while(file_exists($path.$newfile));
$fileinfo2=pathinfo($upfile2["name"]);//解析上传文件名字
do{
$newfile2=date("YmdHis").rand(1000,9999).".".$fileinfo2["extension"];
}while(file_exists($path.$newfile2));
//执行文件上传
//判断是否是一个上传的文件
if(is_uploaded_file($upfile["tmp_name"])){
//执行文件上传(移动上传文件)
if(move_uploaded_file($upfile["tmp_name"],$path.$newfile)){
if(move_uploaded_file($upfile2["tmp_name"],$path.$newfile2)){
//将图片的名称和路径存入数据库
$sql = "UPDATE article SET article_name='$article_name', check_box='".$check_box."', radio_box='$radio_box', content='$content', input_name='$input_name', input_number='$input_number', input_ewm='".trim($path, '.').$newfile."', input_title='$input_title', input_describe='$input_describe', input_image='".trim($path, '.').$newfile2."' where id";
$res = mysqli_query($conn,$sql);
if($res){
echo"文件已存储到数据库";
/*header("refresh:0;url=../active.html");*/
} else {
echo"请求失败,请重试";
}
} else {
die("上传文件失败!");
}
}else{
die("不是一个上传文件!");
}
}
?
问题1:
写了个对数据库里的内容进行更新的php,我提交上去后,把我数据库里全部的id后面对应都数据都改成了一样的了,我前端有直接显示id和article_name,如下图所示:
左边是article_name,右边是id,更新是右边的编辑按钮里面跳转过去的表单页面提交的数据,这个表单页面我有通过取对应id数据库里面的数据显示在表单中,所以我没有修改时echo这个PHP里从前端post过来的数据,都对得上对应id里的数据,但是提交update存进数据库后,会把我所有的id里面的数据都改成我提交的这个表单数据,而不是对应的id修改。
问题2:
编辑里的表单页面,如果没有上传新的图片,会报case 1错误,提示:
上传得文件超过了 php.ini中upload_max_filesize 选项中的最大值
我之前已经成功上传了图片,大小是没有超过设定的最大值,而且只是几十KB的图片,不上传新的图片,提交更新请求就不成功。
请问这两个问题该如何解决?
问题1 没过滤id
sql
where id
没看到加id参数