How to store images in mysql database using php

前端 未结 4 1917
猫巷女王i
猫巷女王i 2020-12-02 19:41

How can i store and display the images in a MySQL database. Till now i have only written the code to get the images from the user and store them in a folder, the code that i

4条回答
  •  一整个雨季
    2020-12-02 20:01

    if(isset($_POST['form1']))
    {
        try
        {
    
    
            $user=$_POST['username'];
    
            $pass=$_POST['password'];
            $email=$_POST['email'];
            $roll=$_POST['roll'];
            $class=$_POST['class'];
    
    
    
            if(empty($user)) throw new Exception("Name can not empty");
            if(empty($pass)) throw new Exception("Password can not empty");
            if(empty($email)) throw new Exception("Email can not empty");
            if(empty($roll)) throw new Exception("Roll can not empty");
            if(empty($class)) throw new Exception("Class can not empty");
    
    
            $statement=$db->prepare("show table status like 'tbl_std_info'");
            $statement->execute();
            $result=$statement->fetchAll();
            foreach($result as $row)
            $new_id=$row[10];
    
    
            $up_file=$_FILES["image"]["name"];
    
            $file_basename=substr($up_file, 0 , strripos($up_file, "."));
            $file_ext=substr($up_file, strripos($up_file, ".")); 
            $f1="$new_id".$file_ext;
    
            if(($file_ext!=".png")&&($file_ext!=".jpg")&&($file_ext!=".jpeg")&&($file_ext!=".gif"))
            {
                throw new Exception("Only jpg, png, jpeg or gif Logo are allow to upload / Empty Logo Field");
            }
            move_uploaded_file($_FILES["image"]["tmp_name"],"../std_photo/".$f1);
    
    
            $statement=$db->prepare("insert into tbl_std_info (username,image,password,email,roll,class) value (?,?,?,?,?,?)");
    
            $statement->execute(array($user,$f1,$pass,$email,$roll,$class));
    
    
            $success="Registration Successfully Completed";
    
            echo $success;
        }
        catch(Exception $e)
        {
            $msg=$e->getMessage();
        }
    }
    

提交回复
热议问题