简体   繁体   English

如何使用PHP从图像标签将图像存储在数据库中?

[英]how to store images in database from image tag using PHP?

I have an image tag like this:我有一个像这样的图像标签:

  <img src="" id="img1" alt="img1" width="100" height="100" style="display: none;">

where the image source is set dynamically by JavaScript.其中图像源由 JavaScript 动态设置。 I want to store this image in database using PHP if the source is set.如果设置了源,我想使用 PHP 将此图像存储在数据库中。 This should happen on click of a button.这应该在单击按钮时发生。 This is my PHP code where I'm just checking if database insert works and it does.这是我的 PHP 代码,我只是在其中检查数据库插入是否有效并且确实有效。

if(isset($_POST['save'])){
    $name="checkcheck";
    global $wpdb; 
    $table = $wpdb->prefix ."authorlist"; 
    $wpdb->insert( 
        $table, 
        array( 
            'authorname' => $name
            )
        );            
    }

Now how do I modify this code to store that image?现在如何修改此代码以存储该图像?

EDIT 1: This is what I did after what you guys suggested编辑 1:这是我按照你们的建议所做的

    if(isset($_POST['save'])){
        $image = $_POST['img_src_value'];

        global $wpdb; 

        $table = $wpdb->prefix ."authorlist"; 
        $wpdb->insert( 
            $table, 
            array( 
                'authorname' => $image,
                )
            );            
    }

and this is my image tag:这是我的图片标签:

  <img src="" id="img1" alt="img1" width="100" height="100" style="display: none;">
  <form method="POST" name="imgForm">
  <input name="img_src_value" type="hidden" class="img_src_value" value="">
  </form>

although you must know that this image tag is also within a form itself.尽管您必须知道此图像标签也在表单中。 So do I put another form tag?那么我要放另一个表单标签吗?

Take a form tag and hidden field.取一个表单标签和隐藏字段。 With javascript also add img src value to hidden field.使用 javascript 还可以将 img src 值添加到隐藏字段。

<img src="" id="img1" alt="img1" width="100" height="100" style="display: none;">
<input name="img_src_value" type="hidden" class="img_src_value" value="">

Then to save然后保存

if(isset($_POST['save'])){
   $imgSrc = $_POST['img_src_value']; /// You can get that value here           
 }

While you set image source dynamically at same time set same value in hidden field and while submitting form you will get it in Request value.当您同时动态设置图像源时,在隐藏字段中设置相同的值,并在提交表单时,您将在请求值中获得它。

You will receive it as您将收到它作为

$name="checkcheck";

$image = $_POST['imagesource'];

global $wpdb; 

$table = $wpdb->prefix ."authorlist"; 
$wpdb->insert( 
    $table, 
    array( 
        'authorname' => $name,
        'image' => $image
        )
    );            

暂无
暂无

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

相关问题 将网站页面源中的数据存储到数据库中<script> tag (php) - Store to database data from website page source in the <script> tag (php) 如何将网络摄像头捕获的图像存储在指定的文件夹中,并使用php将捕获的图像路径存储在mysql数据库中? - how to store webcam captured image in specified folder and captured image path in mysql database using php..? 如何使用html输入标签从用户获取图像并将其存储在树莓派上的文件夹中 - How to get an image from a user and store it in a folder on a raspberry pi using the html input tag 如何使用php将网络摄像头捕获的图像路径存储在mysql数据库中? - How to store webcam captured image path in mysql database using php.? 如何使用Ajax将图像存储到文件夹并存储到数据库的路径? - How to store image to folder and store the path to database using Ajax? 如果图像标签是从数据库动态创建的,如何使用javascript或php确定何时加载图像? - how can I use javascript or php to determine when images are loaded if image tags are dynamically created from database? 如何仅使用一个图像标签显示多个图像? - How to show multiple images using only one image tag? 如何浏览图像形式的HTML并使用javascript将图像存储在数据库中? - How to browse image form HTML and store that image in database using javascript? 使用PHP MySQL使用数据库中的一个重置选项标签值 - Reset option tag value with one from database using PHP MySQL 使用PHP动态创建的表单在数据库中存储不同的值? - Store different value in the database from the forms created dynamically using PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM