php mysql交互
1.接受数据
if($_POST){
$uname = trim($_POST[‘uname‘]);
$pasd = md5($_POST[‘pasd‘]);
2.查询数据库
$conn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘qiye‘);
$sql = "select * from admin where uname = ‘$uname‘ and pasd = ‘$pasd‘ ";
$res = $conn->query($sql);
if($conn->error){
die($conn->error);
}
$info = $res->fetch_assoc();
3. 判断数据
if($res->num_rows > 0){
setcookie(‘admin‘,$info[‘id‘]);
header(‘Location:index.php‘);
}else{
$msg= ‘账号或者密码错误‘;
}
$conn->close();
}
4.获取mysql报错信息$coon->error;
二 、文件上传
if($_FILES){
1.获取文件后缀
$temp=explode(".",$_FILES["img"]["name"]);
$extension=end($temp);
2.判断文件类型和大小
if($_FILES["img"]["name"]=="image/png" || $_FILES["img"]["name"]=="image/gif" || $_FILES["img"]["name"]=="image/jpeg" || $_FILES["img"]["name"]=="image/jpg" && $_FILES["img"]["size"]<=204800){
3.拼接文件名
$pasd = md5($_POST[‘pasd‘]);
2.查询数据库
$conn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘qiye‘);
$sql = "select * from admin where uname = ‘$uname‘ and pasd = ‘$pasd‘ ";
$res = $conn->query($sql);
if($conn->error){
die($conn->error);
}
$info = $res->fetch_assoc();
3. 判断数据
if($res->num_rows > 0){
setcookie(‘admin‘,$info[‘id‘]);
header(‘Location:index.php‘);
}else{
$msg= ‘账号或者密码错误‘;
}
$conn->close();
}
4.获取mysql报错信息$coon->error;
二 、文件上传
if($_FILES){
1.获取文件后缀
$temp=explode(".",$_FILES["img"]["name"]);
$extension=end($temp);
2.判断文件类型和大小
if($_FILES["img"]["name"]=="image/png" || $_FILES["img"]["name"]=="image/gif" || $_FILES["img"]["name"]=="image/jpeg" || $_FILES["img"]["name"]=="image/jpg" && $_FILES["img"]["size"]<=204800){
3.拼接文件名
$uname = ‘../uploads‘.time().mt_rand(100,999).‘.‘.$extension;
4.存储到目录
4.存储到目录
move_uploaded_file($_FILES["img"]["tmp_name"],$uname);
5. 存入数据库
5. 存入数据库
$conn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘denglu‘);
$sql = "insert into product (img) values(‘$uname‘)";
$res = $conn->query($sql);
var_dump($uname);
$conn->close();
6.调取数据库中的图片
$connn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘denglu‘);
$sqll = " select * from product where id =x";
$ress = $connn->query($sqll);
$r = $ress->fetch_assoc();
$connn->close();
?>
}
}
相关推荐
weiguoxin 2020-06-12
xceman 2020-09-22
bcbeer 2020-08-27
风萧萧梦潇 2020-08-17
夕加加 2020-07-20
LuckyDeng 2020-07-08
huangchunxia 2020-07-08
lbPro0 2020-07-05
葉無聞 2020-07-05
万里之书 2020-07-04
冰蝶 2020-07-04
yuzhongdelei0 2020-07-04
meiseeny 2020-06-28
RuoShangM 2020-06-28
bianxq 2020-06-28
80327065 2020-06-28
Codeeror 2020-06-28
somyjun 2020-06-25