簡體   English   中英

在MySQL數據庫中存儲圖像文件的路徑

[英]Storing a path for an image file in MySQL database

我正在使用Codeigniter構建應用程序,並嘗試在MySQL數據庫中手動存儲圖像文件的路徑。 圖像存儲在名為crest的文件夾中,該文件夾位於應用程序的根目錄下。

我正在使用以下代碼將圖像插入數據庫,但它拋出了“錯誤1064”

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

/*
File:        crests.sql
Description: Used for creating the objects and loading data into the teams schema
Created:     February 11, 2014
Version:     1.0
Advice:      Save this file onto your hardrive and use the following command(no  semicolon) to run it from the 

command prompt
           SOURCE C:\xampp\htdocs\myApplication\sql_scripts\teams.sql  
*/

SELECT '<starting bars script>' AS '';

SELECT '<my_teams_ database>' AS '';

INSERT INTO teams (team_name, team_logo) VALUES('Antrim', '<img src="<?php echo    site_url('../logo/antrim.png'); ?>" 

alt="...">');


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

我在MySQL命令行客戶端中遇到錯誤

ERROR 1064 <4200> you have an error in your SQL syntax; check the manual that corresponds to your MySQL server for the right syntax to use near '../logo/antrim.png'); ?>" alt="...">'); at line 1

我不確定這是否是語法錯誤,或者是否與使用反斜杠轉義引號有關。 我也不確定這是否是在Codeigniter中存儲圖像路徑的正確方法。 我已經在這里和其他論壇上搜索過,但是找不到答案。 任何幫助,將不勝感激。

我正在使用MVC來存儲,操作和顯示此數據。

團隊模式

<?php  
class Team_model extends CI_Model {  

public function __construct()
{
  parent::__construct();
}

function team() 

    {  
        //Query the team table for every record and row  

        $team_results = array();
        $this->db->select('team_name, team_logo');
        $this->db->from('team');

        $query = $this->db->get();

        if($query->num_rows() > 0) 
        {
         $team_results = $query->result();
        }

        return $team_results;   

    }  

}
/*End of file team_model.php*/
/*Location .application/models/team_model.php*/ 
?> 

在視圖中

<?php

     if (is_array($team_results))
     {
       if( !empty($team_results) ) 
       {

         foreach($team_results as $row) 
         {

          echo '<div class="col-sm-6 col-md-6">';
          echo '<div class="thumbnail">';
          echo '<tr>';
          echo '<h4>';
          echo '<td>'.$row->team_name.'</td>'."</br>";
          echo '<td>'.$row->team_logo'</td>'."</br></br>";
          echo '</h4>';

          echo '</tr>';
          echo '</div>';
          echo '</div>';
         }
       }

      else echo "Not Array";
    }

    ?>


  </div>
</div>

可以毫無問題地返回team_name,但是圖像不會顯示-相反,我得到了一個損壞的圖像鏈接。

您需要轉義單引號或使用雙引號:

INSERT INTO team (team_name, team_logo) VALUES('Antrim',
      '<img src="<?php echo site_url(\'../crests/antrim.png\'); ?>"  alt="...">');

要么:

INSERT INTO team (team_name, team_logo) VALUES('Antrim',
      '<img src="<?php echo site_url("../crests/antrim.png"); ?>"  alt="...">');

順便說一下,如果可以避免的話,我不會存儲這樣的圖像路徑(我不知道您將如何使用它...); 您將需要評估稍后從數據庫獲得的字符串以處理php。

在此行的插入語句中使用引號存在問題:

INSERT INTO team (team_name, team_logo) VALUES('Antrim', '<img src="<?php echo    site_url('../crests/antrim.png'); ?>"

第一個值是Antrim ,下一個是<img src="<?php echo site_url( ,下一個單引號將終止該值。在此MySQL后面需要逗號。

轉義預期值內的引號。

您的問題在這里:

INSERT INTO team (team_name, team_logo) VALUES('Antrim', '<img src="<?php echo       site_url('../crests/antrim.png'); ?>" 

將php或HTML代碼存儲在數據庫中並不是最佳實踐。 數據庫用於數據 ,而不是代碼 最好只將映像路徑存儲在數據庫中。 您應該這樣編寫INSERT:

INSERT INTO team (team_name, team_logo_url, team_logo_altText) VALUES('Antrim', '../crests/antrim.png', '...') 

並處理代碼中的任何PHP或HTML。

真的不建議這樣做, 但是如果您只是想使其工作,可以將查詢更改為:

INSERT INTO team (team_name, team_logo) VALUES('Antrim', '<img src="<?php echo site_url(\'../crests/antrim.png\'); ?>"  alt="...">');

這通常是我存儲圖片路徑名稱的方式,例如:images / uploads / [picname]。[picext]。

因此它將是:

insert values ('Antrim', 'images/uploads/example.jpg') 

我發現這是一種更輕松的保存路徑名的方法,您可以通過html處理img標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM