如何使用php和mysql将图像插入数据库和目标文件夹?

时间:2018-06-21 06:24:36

标签: php mysql

我创建了一个测试表单,只是试图将图像插入mysql的“学生” 表和“图像” 文件夹中。但是图像不能插入数据库和图像文件夹。下面的代码只是一个测试,我想提交,但不是。

**Mysql Database:**
    CREATE TABLE  STUDENT
           (    
    STD_SL_NO INT(10) NOT NULL AUTO_INCREMENT, 
    STD_NAME VARCHAR(140) NOT NULL,
    STD_FNAME VARCHAR(140) NOT NULL,
    STD_MNAME VARCHAR(140) NOT NULL,
    STD_BDATE DATE NOT NULL,
    STD_GENDER VARCHAR(10) NOT NULL,
    STD_RELIGIOUS VARCHAR(20) NOT NULL,
    STD_CURR_ADDRESS TEXT NOT NULL,
    STD_MOBILE  CHAR(12),
    STD_ROLL_NO INT(10) NOT NULL,
    STD_PHOTO  BLOB, 
    USER_NAME VARCHAR(50), 
    ENTRY_DATE TIMESTAMP (6),
    PRIMARY KEY(STD_SL_NO, STD_ROLL_NO, STD_REG_NO)
    )ENGINE=InnoDB;
**Form HTML & PHP:**
<!DOCTYPE html>
<html>
<head>
<title> Student Information Entry Form </title>
</head>
<body>
<form name="form1" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
     <td><h4> Student Name</h4></td>
     <td> <h5> <input type="text" name="t1" size="30"/></h5></td>
</tr>
<tr>
        <td><h4> Student's Father Name</h4></td> 
        <td> <h5> <input type="text" name="t2" size="30"/></h5></td>
</tr>
<tr>
       <td><h4> Student's Mother Name</h4></td> 
       <td> <h5> <input type="text" name="t3" size="30"/></h5></td>
</tr>
<tr>
        <td><h4> Student's Birth Date</h4></td> 
       <td> <h5> <input type="date" name="t4" /></h5></td>
</tr>
<tr>
       <td><h4> Student's Gender </h4></td>
        <td>
              <?php
     $con=mysqli_connect("localhost", "root", "ew6wLoLOro", "result");
     $sql=mysqli_query($con, "select gender_sl_no, gender_name from gender");

                while($row=mysqli_fetch_array($sql))
                {
                    echo '<table>
      <input type="radio" name="t5" value='.$row['gender_name'].' 
         checked="checked"/>
                    '.$row['gender_name'].' 
                        </table>';
                }
                ?>
       </td>
 </tr>
 <tr>
        <td><h4> Please Select Your Religion</h4></td>
        <td> <h5>
                <div>
                    <select name="t6">
                        <option>Selection</option>
                        <?php

                $sql=mysqli_query($con, "select  religion_name from religion");
                   while($row=mysqli_fetch_array($sql))
                        {
                            ?>
                         <option> <?php echo $row["religion_name"]; ?></option>
                            <?php
                        }
                        ?>
                   </select> </div> </h5>
        </td>
    </tr>

<tr>
       <td><h4>House No/Village,P.O/<br>Detail address except above </h4></td>
       <td> <h5> <input type="text" name="t7" size="40" /></h5></td>
</tr>   
<tr>
            <td><h4>Entry Student Mobile No.</h4></td>
             <td> <h5> <input type="text" name="t15" maxlength="11" 
               onpaste="return false;" onkeypress="StdMobile(event)">
                <script>
                    function StdMobile(evt)
                    {
                        var anwar = String.fromCharCode(evt.which);
                        if(!(/[0-9]/.test(anwar)))
                        {
                            evt.preventDefault();
                        }
                    }
                </script>
                </h5></td>
</tr>
<tr>
        <td><h4> Student Roll No. </h4></td>
        <td> <h5> <input type="text" name="t23" maxlength="6" 
              onkeypress="StdRoll(event)" >
        <script>
        function StdRoll(evt)
        {
            var elaf=String.fromCharCode(evt.which);
            if(!(/[0-9]/.test(elaf)))  
            {
                evt.preventDefault();
            }
        }
        </script>

        </h5></td>
</tr>
<tr>
        <td><h3> Student Photo</h3></td>
        <td> <h5> <input type="file" name="file"/>
        </h5></td>
</tr>
<tr>
        <td> </td>
            <td><input type ="submit" name="submit"/> </td>
        </tr>
</table>
</form>
<?php

$con=mysqli_connect("localhost", "root", "ew6wLoLOro", "result");
if(!$con)
{
    die('Could not connect:'.mysqli_connect_error());
}

if(isset($_POST['submit']))
{
    $t1 = (isset($_POST['t1']) ? $_POST['t1']:null);
    $t2 = (isset($_POST['t2']) ? $_POST['t2']:null);
    $t3 = (isset($_POST['t3']) ? $_POST['t3']:null);
    $t4 = (isset($_POST['t4']) ? $_POST['t4']:null);
    $t5 = (isset($_POST['t5']) ? $_POST['t5']:null);
    $t6 = (isset($_POST['t6']) ? $_POST['t6']:null);
    $t7 = (isset($_POST['t7']) ? $_POST['t7']:null);
    $t15 = (isset($_POST['t15']) ? $_POST['t15']:null);
    $t23 = (isset($_POST['t23']) ? $_POST['t23']:null);
    $file = (isset($_FILES['file']));
    $file_name = $file['name'];
    $file_type = $file ['type'];
    $file_size = $file ['size'];
    $file_path = $file ['tmp_name'];
    if($file_name!="" && ($file_type="image/jpeg"||$file_type="image/png"||$file_type="image/gif")&& $file_size<=614400)
    {
    if(move_uploaded_file ($file_path,'images/'.$file_name))
    {

$ query =“插入到学生值中(默认,'$ t1','$ t2','$ t3','$ t4','$ t5','$ t6','$ t7',' $ t15','$ t23','$ file_name'(默认,默认))“;

    $result = mysqli_query($con, $query);
    if($result==true)
   {
    echo "File Uploaded";
   }

    }       
    }

}


?>

2 个答案:

答案 0 :(得分:0)

尝试使用此代码

表格

RETURN NULL;

PHP代码”

<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
  <input name="image" accept="image/jpeg" type="file">
  <input value="Submit" type="submit">
</form>

?>

答案 1 :(得分:-1)

使用此api。只需修改

/ 上传徽标图像的功能 /

    public function upload_logo($filename,$temp_name,$filesize,$dir){

            global $postClass;
        $upload_size = imagesize*1424*1424;
        $file_name = $filename;

            if (!file_exists($dir)) 
                    {
                            mkdir($dir, 0777, true);
                    }
              $finfo = finfo_open(FILEINFO_MIME_TYPE);
              $mime = finfo_file($finfo, $temp_name);
            if($mime == 'image/jpeg' || $mime == 'image/png' || $mime == 'image/jpg' || $mime == 'image/JPG' || $mime == 'application/pdf')
        {
            if($filesize < $upload_size)
            {
                $temp = $temp_name;
                $extention = explode('.',$file_name);
                $name = rand(1,100).'_'.time().'.'.$extention[1];

                if($mime == 'application/pdf'){
                  if(move_uploaded_file($temp,"$dir/$name")){
                       if(strpos($dir, '/') !== false){
                            $dir1 = explode('/', $dir);
                            $dir = $dir1[1];
                        }
                        else{
                            $dir =$dir;
                        }
                        return  $dir.'/'.$name;
                    }
                    else{
                        return 1;
                    }
                }
                else{
                    $image = $postClass->compress($temp, "$dir/$name", 70);
                                 //if(move_uploaded_file($temp,"$dir/$name"))
                    if($image)
                    {
                        if(strpos($dir, '/') !== false){
                            $dir1 = explode('/', $dir);
                            $dir = $dir1[1];
                        }
                        else{
                            $dir =$dir;
                        }
                        return  $dir.'/'.$name;

                    }else{
                        return 1;
                    }
                }
            }else{
                return 2;
            } 
            }else{
                    return 3;
            }
    }