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