質問内容
phpでモデルクラスを作成して、書き換えていたのですが、画像が表示されなくなり困っています。
画像以外はうまく表示されました。
モデルに書き換えたことで、画像ファイルに画像が入らなくなったのが原因みたいです。
考えられる理由は何でしょうか?ご回答お願いいたします。
変更前
(index.php)
<?php require_once('../config.php'); require_once('lib/smarty/Smarty.class.php'); $pdo=new PDO('mysql:host=127.0.0.1;dbname=one_luck;charset=utf8', 'root', DB_PASSWORD); $sql = "SELECT * FROM lucks"; error_log("sql=" . $sql); $stm = $pdo->prepare($sql); $stm->execute(); $record_list = $stm->fetchAll();
(register.php)
if (!empty($_POST['tmp_file_path'])) { $tmp_file_path = $_POST['tmp_file_path']; $arr = explode('.', $tmp_file_path); $ext = end($arr); $fname = $id . '.' . $ext; rename($tmp_file_path, '../luckfile/images/' . $fname); $sql = "UPDATE lucks set attach_filename = '" . $fname . "' WHERE id = ".$id; error_log("sql=" . $sql); $stm = $pdo->prepare($sql); $stm->execute(); }
###変更後
(index.php)
<?php require_once('../config.php'); require_once("model/lucks.php"); require_once('lib/smarty/Smarty.class.php'); $pdo=new PDO('mysql:host=127.0.0.1;dbname=one_luck;charset=utf8', 'root', DB_PASSWORD); $luck = new Luck(); $record_list = $luck->getLuckByAll($id,$content,$attach_filename,$deleted_at);
(register.php)
if (!empty($_POST['tmp_file_path'])) { $tmp_file_path = $_POST['tmp_file_path']; $arr = explode('.', $tmp_file_path); $ext = end($arr); $fname = $id . '.' . $ext; rename($tmp_file_path, '../luckfile/images/' . $fname); $luck = new Luck(); $luck_image = $luck->updateImage($fname); }
質問追記
model/lucks.php
<?php require_once("../config.php"); class Luck { private $db; function __construct() { $user = root; $password = root; $dbname = 'luck'; $host = 'localhost'; $dsn = "mysql:host={$host};dbname={$dbname};charset=utf8"; $this->db = new PDO($dsn, $user, $password); } public function addContent($name){ $sql = "INSERT INTO lucks (content) VALUES ('" . $name . "')"; error_log("sql=" . $sql); $stm = $this->db->prepare($sql); $stm->execute(); return $this->db->lastInsertId(); } public function updateImage($fname){ $sql = "UPDATE lucks set attach_filename = '" . $fname . "' WHERE id = ".$id; error_log("sql=" . $sql); $stm = $this->db->prepare($sql); $stm->execute(); return $this->db->lastInsertId(); } public function getLuckByAll($id,$content,$attach_filename,$deleted_at){ $sql = "SELECT * FROM lucks"; error_log("sql=" . $sql); $stm = $this->db->prepare($sql); $stm->execute(); $record_list = $stm->fetchAll(); return $record_list; } }
画像を表示しているページ
(index.tpl)
<!DOCTYPE html> <html> <head> <title>a</title> <meta charset="utf-8"> </head> <body> <table border='1'> <tr> <td>内容</td> <tr> {foreach from=$indexdata item=current} <td>{$current.id}</td> <td>{$current.content}</td> {if $current.attach_filename} <td><img src='/luckfile/images/{$current.attach_filename}'/></td> {else} <td>no image</td> {/if} <td><form method="POST" action="delete.php"> <input type="hidden" name="id" value='{$current.id}'> <input type="submit" value="削除"></form></td> <td> <a href="/luckfile/edit_menu.php?id={$current.id}">編集</a> </td> <td> <a href="/luckfile/show.php?id={$current.id}">詳細</a> </td> </tr> {/foreach} </table> </body> </html>
回答1件
あなたの回答
tips
プレビュー