繁体   English   中英

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

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

我有一个像这样的图像标签:

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

其中图像源由 JavaScript 动态设置。 如果设置了源,我想使用 PHP 将此图像存储在数据库中。 这应该在单击按钮时发生。 这是我的 PHP 代码,我只是在其中检查数据库插入是否有效并且确实有效。

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

现在如何修改此代码以存储该图像?

编辑 1:这是我按照你们的建议所做的

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

        global $wpdb; 

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

这是我的图片标签:

  <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>

尽管您必须知道此图像标签也在表单中。 那么我要放另一个表单标签吗?

取一个表单标签和隐藏字段。 使用 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="">

然后保存

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

当您同时动态设置图像源时,在隐藏字段中设置相同的值,并在提交表单时,您将在请求值中获得它。

您将收到它作为

$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.

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