MySQLテーブルの結果がHTMLテーブルとして表示されるウェブサイト(内部リソースとして使用する)を作成するように求められました。各レコードにはアップロードボタンがあり、特定の画像やPDFをアップロードできます。次に、これらのレコードとアップロードされたファイルのリストを送信できます。PHP:テーブルの適切な場所にアップロードされたファイル名を表示する方法
私は(非常に)初歩的なアップロードを行うことができました。私の問題は、アップロードされたファイル名を表示されるレコードにのみ表示する方法を知らないことです。どうすればこれを達成できますか?
私はばかなことを試して、<td>
に{$file}
と表示しましたが、もちろんすべてのレコードにファイル名が表示されます。 t20pctID
がファイル名の一部と等しくない場合、同じファイルを表示しているFirefoxのクラッシュが終わることはありません。
これは私が使用しているコードです。これはいくつかの外部リソースで見つかったコードの適合です。前もって感謝します。 (オンラインショップでカートと同様のパターン以下)
Claims.php
<?php
include '../config/database.php';
include 'layout_head.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
$t20pctID = isset($_GET['song_id']) ? $_GET['song_id'] : "";
$track_title = isset($_GET['track_title']) ? $_GET['track_title'] : "";
$file_name = isset($_GET['file']) ? $_GET['file'] : "";
$user_id = 1;
//display a message
if ($action =='removed') {
echo "<div class='alertInfo'>
<strong>{$track_title}</strong> was removed!
</div>
";
} elseif ($action =='file_uploaded') {
echo "<div class='alertInfo'>
New file <strong>successfully</strong> uploaded!
</div>
";
} elseif ($action =='sent') {
echo "<div class='alertInfo'>
<strong>Sent OK!</strong>
</div>
";
} elseif ($action =='sent_failed') {
echo "<div class='alertInfo'>
<strong>Sent failed :/</strong>
</div>
";
}
$query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, t.record_label, t.publication_year, c.file FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%Sony%' ORDER BY t.main_artist, t.track_title ASC";
$stmt=$con->prepare($query);
$stmt->execute();
$num = $stmt->rowCount();
if ($num > 0) {
echo "<table class='records'>
<tr>
<th>Main Artist</th>
<th>Track Title</th>
<th>Original Album</th>
<th>Record Label</th>
<th>Publication Year</th>
<th>Documentation (only .jpg, .gif, .png, .pdf allowed)</th>
<th style='width:100px;'></th>
</tr>
";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$form_name = "file_upload_".$t20pctID;
echo "<tr>
<td>
<div class='t20pctID' style='display:none;'>{$t20pctID}</div>
<span>{$main_artist}</span>
</td>
<td>
<div class='track_title'>{$track_title}</div>
</td>
<td>
<span>{$original_album}</span>
</td>
<td>{$record_label}</td>
<td>{$publication_year}</td>
<td>
<form name='$form_name' action='upload.php?id=$t20pctID' method='post' enctype='multipart/form-data'>
<input type='file' name='upload_file'>
<input type='submit' value='Upload' class=''>
</form>";
if ($file_name) {
$user_id = 1;
$query_update = "UPDATE cart SET file='$file_name', uploaded_id='$uploaded_id' WHERE t20pctID=? AND user_id=?";
$stmt = $con->prepare($query_update);
// bind values
$stmt->bindParam(1, $t20pctID);
$stmt->bindParam(2, $user_id);
//update
$stmt->execute();
}
echo "{$file}"; //
echo "</td>
<td>
<a href='remove_from_cart.php?song_id={$t20pctID}&track_title={$track_title}' class='btnCTARemove'>
<span class='glyphicon glyphicon-remove'></span>Remove
</a>
</td>
</tr>
";
}
echo "<tr>
<td colspan='6'></td>
<td id='checkout'>
<a href='checkout.php' class='btnCTASend'>
<span class='glyphicon glyphicon-shopping-cart'></span>Send
</a>
</td>
</tr>
";
echo "</table>";
} else {
if ($action !='sent' && $action !='removed') {
echo "<div class='alertInfo'>
<strong>No songs found</strong> in your cart!
</div>
";
}
}
include 'layout_foot.php';
?>
Upload.php
<?php
$t20pctID = $_GET['id'];
date_default_timezone_set('CET');
$date = date('Y-m-d-H:i:s');
if (isset($_FILES['upload_file'])) {
$file = $_FILES['upload_file'];
//file properties
$file_name = $file['name'];
$file_tmp = $file['tmp_name'];
$file_size = $file['size'];
$file_error = $file['error'];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array('jpg', 'pdf');
if (in_array($file_ext, $allowed)) {
if ($file_size <= 2097152) {
$file_name_new = $t20pctID. '_' . $date . '.' . $file_ext;
$file_destination = 'tmp_uploads/' .$file_name_new;
if (move_uploaded_file($file_tmp, $file_destination)) {
header('Location: claims.php?action=file_uploaded&song_id=' . $t20pctID .'&file='. $file_name_new);
} else {
echo "Error moving the file(s).";
}
} else {
echo "Error: your file is too big. The maximum size permitted is 2 MB.";
}
} else {
echo "Error: your file is not permitted (". $file_name."). Only .jpg and .pdf permitted.";
}
}
?>