在MySQL数据库中存储图像文件的路径

时间:2014-02-11 17:36:43

标签: php mysql sql codeigniter

我正在使用Codeigniter构建应用程序,并尝试在MySQL数据库中手动存储图像文件的路径。图像存储在名为crests的文件夹中,该文件夹位于应用程序的根级别。

我使用以下代码将图像插入到数据库中,但它会引发“错误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没问题,但图像没有显示 - 相反,我得到一个损坏的图像链接。

4 个答案:

答案 0 :(得分:2)

您需要转义单引号或使用双引号:

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。

答案 1 :(得分:0)

此行的insert语句中使用的引号存在问题:

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期望一个逗号。

转义预期值内的引号。

答案 2 :(得分:0)

你的问题在这里:

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

将php或HTML代码存储在数据库中并不是最佳实践。数据库用于数据,而不是代码。将图像路径存储在DB中会好得多。您应该像这样编写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="...">');

答案 3 :(得分:0)

这就是我通常存储图片路径名的方式,例如:images / uploads / [picname]。[picext]。

所以它会是:

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

我觉得处理保存路径名的方法比较容易,你可以通过html处理img标签。

相关问题