简体   繁体   English

如何使用mysql将带有文本数据的图像插入到php中的数据库中。?

[英]How to insert images with text data to database in php using mysql.?

I have a long-form with some images upload areas.我有一个带有一些图像上传区域的长格式。 I need to insert images with text data to MySQL database The form and query code as follows.我需要将带有文本数据的图片插入到MySQL数据库中 表格和查询代码如下。 If PHP codes are not correct, tell me how to do it correctly.?如果 PHP 代码不正确,请告诉我如何正确操作。?

<div class="container">
    <div class="row">
    <div class="col-md-4">
        <div class="form_main">
                <h4 class="heading"><strong>Add</strong> Vehicle <span></span></h4>
                <div class="form">
                <form action="insert.php" method="POST" id="contactFrm" enctype="multipart/form-data" name="contactFrm">
                    <select class="form-control form-control-lg" name="brand">
                      <option value="TATA" >TATA</option>
                      <option value="TOYOTA">TOYOTA</option>
                      <option value="DAIHATSu">DAIHATSU</option>
                    </select>
                    <input type="text" required="" placeholder="Model" name="model" class="txt">           
                     <div class="form-group">
                        <label for="exampleFormControlFile1">Front</label>
                        <input type="file" name="vehi_front" class="form-control-file" id="exampleFormControlFile1">
                     </div>
                     <div class="form-group">
                        <label for="exampleFormControlFile1">Left</label>
                        <input type="file" name="vehi_left" class="form-control-file" id="exampleFormControlFile1">
                     </div>
                     <div class="form-group">
                        <label for="exampleFormControlFile1">Right</label>
                        <input type="file" name="vehi_right" class="form-control-file" id="exampleFormControlFile1">
                     </div>
                     <div class="form-group">
                        <label for="exampleFormControlFile1">Rear</label>
                        <input type="file" name="vehi_back" class="form-control-file" id="exampleFormControlFile1">
                     </div>
                     <textarea placeholder="Other" name="other" type="text" class="txt_3"></textarea>
                     <input type="submit" value="submit" name="submit" class="txt2">
                </form>
            </div>
            </div>
            </div>
</div>
</div>

This is the insert query code.这是插入查询代码。

<?php
include("../inc/conn.php");

$brand=$_POST['brand'];
$model=$_POST['model'];
$vehi_front=$_POST['vehi_front'];
$vehi_left=$_POST['vehi_left'];
$vehi_right=$_POST['vehi_right'];
$vehi_back=$_POST['vehi_back'];
$other=$_POST['other'];
    {
    $sql = "INSERT INTO vehicle(brand,model,vehi_front,vehi_left,vehi_right,vehi_back,other) VALUES ('{$brand}','{$model}','{$vehi_front}','{$vehi_left}','{$vehi_right}','{$vehi_back}','{$other}')";
    if(mysqli_query($con,$sql))
    {
    header("location:add_vehicle.php?msg=Successfully Saved !");
    }   
}
?>

You can do this using $_FILES variable您可以使用$_FILES变量执行此操作

see reference here: https://www.php.net/manual/en/reserved.variables.files.php请参阅此处的参考: https : //www.php.net/manual/en/reserved.variables.files.php

you must create image uploader function in php $_POST['image_input_name'] return you null you must use $_FILE您必须在 php $_POST['image_input_name'] 中创建图像上传功能返回 null 您必须使用 $_FILE

  function uploadPic($file_input_name, $path)
    {
        if (isset($_FILES[$file_input_name])) {
            $file = $_FILES[$file_input_name];
            // for bin2hex and random_bytes you must use php > 7
            $new_name = (string)bin2hex(random_bytes(32));
            $extension = ".jpg";
            $final_name = $new_name . $extension;
            move_uploaded_file($file['tmp_name'], $path . $final_name);
            return $path . $final_name;

        }
    }

first line check for image 2 get the file and put in into $file 3 change the name for security 4 change the extension for more security // user upload a shel.php.第一行检查图像 2 获取文件并放入 $file 3 更改名称以提高安全性 4 更改扩展名以提高安全性 // 用户上传 shel.php。 in function we change the name and extension // for exp shel.php converted to hs7f4w8r5c1f4s5d9t6g3s149748654asdasd.jpg 5 add name and extension in var 6 we move uploaded pic to path 7 we return the address and name of pic and in your new code here在函数中,我们更改名称和扩展名 // 将 exp shel.php 转换为 hs7f4w8r5c1f4s5d9t6g3s149748654asdasd.jpg 5 在 var 6 中添加名称和扩展名,我们将上传的 pic 移至路径 7 我们返回 pic 的地址和名称,并在此处在您的新代码中

<?php
include("../inc/conn.php");

$brand = $_POST['brand'];
        $model = $_POST['model'];
        // in if we check for image exist
        if ($_FILES['vehi_front']['tmp_name'] != "") {
            $vehi_front = $this->uploadPic("vehi_front", "/path/to/save/image");
        }
        if ($_FILES['vehi_left']['tmp_name'] != "") {
            $vehi_left = $this->uploadPic("vehi_left", "/path/to/save/image");
        }
        if ($_FILES['vehi_right']['tmp_name'] != "") {
            $vehi_right = $this->uploadPic("vehi_right", "/path/to/save/image");
        }
        if ($_FILES['vehi_back']['tmp_name'] != "") {
            $vehi_right = $this->uploadPic("vehi_back", "/path/to/save/image");
        }


        $other = $_POST['other'];
        {
            $sql = "INSERT INTO vehicle(brand,model,vehi_front,vehi_left,vehi_right,vehi_back,other) VALUES ('{$brand}','{$model}','{$vehi_front}','{$vehi_left}','{$vehi_right}','{$vehi_back}','{$other}')";
            if (mysqli_query($con, $sql)) {
                header("location:add_vehicle.php?msg=Successfully Saved !");
            }
        }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM