我上传了图片。代码工作正常。我想在将图像保存到数据库时插入会话ID。如何在insert query.view文件中保存会话ID,以便在库中显示图像。我想将它们显示为用户。
这就是我需要保存用户ID以检索特定用户的特定图像的原因。现在它从数据库中获取所有图像。我想存储用户ID以跟踪特定图像。我在这里开始了我的会议。这是我的会话ID。
<?php
session_start(); //what can be done to save this id.I need save this staffid to save in the database.What is the process
if(!$this->session->userdata('staffid')){ redirect("login/checkLogin");
}
?>
<?php
我想将此id保存到数据库中以获取特定图像。我需要在我的数据库中将该id保存为类别ID,这样我就不必创建另一个表来使任何图库只使用可以用作图库名称的名称上传它们。
if (isset($_POST["sub1"]) || isset($_POST["sub2"])) {
// include resized library
require_once('./php-image-magician/php_image_magician.php'); //library for showing images in thumbnail.This library is helping for showing images as gallery.
$msg = "";
$valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
if (count($_FILES["user_files"]) > 0) {
$folderName = "uploads/";
$sql = "INSERT INTO tbl_images(image_name) VALUES (:img)"; //have insert my session id here.What is the procedure to insert session id in sql insert query.
$stmt = $DB->prepare($sql);
for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) { //Here is the loop for saving multiple images in database.
if ($_FILES["user_files"]["name"][$i] <> "") {
$image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));
// if valid image type then upload
if (in_array($image_mime, $valid_image_check)) {
$ext = explode("/", strtolower($image_mime));
$ext = strtolower(end($ext));
$filename = rand(10000, 990000) . '_' . time() . '.' . $ext;
$filepath = $folderName . $filename;
//this will save the folder name and file path in database.There is no problem saving them.Working successfully.
if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) {
$emsg .= "Failed to upload <strong>" . $_FILES["user_files"]["name"][$i] . "</strong>. <br>";
$counter++;
} else {
$smsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>";
$magicianObj = new imageLib($filepath);
$magicianObj->resizeImage(100, 100);
$magicianObj->saveImage($folderName . 'thumb/' . $filename, 100);
/* * ****** insert into database starts ******** */
try {
$stmt->bindValue(":img", $filename); //i have to insert my session id in here.
$stmt->execute();
$result = $stmt->rowCount();
if ($result > 0) {
// file uplaoded successfully.
} else {
// failed to insert into database.
}
} catch (Exception $ex) {
$emsg .= "<strong>" . $ex->getMessage() . "</strong>. <br>";
}
/* * ****** insert into database ends ******** */
}
} else {
$emsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> not a valid image. <br>";
//After checking file type if anything goes wrong it will show this message.File type is given.
}
}
}
$msg .= (strlen($smsg) > 0) ? successMessage($smsg) : "";
$msg .= (strlen($emsg) > 0) ? errorMessage($emsg) : "";
} else {
$msg = errorMessage("You must upload atleast one file");//validation for uploading files.
}
}
?>
答案 0 :(得分:1)
首先,您需要了解会话W3Schools Description
如果我理解正确的话:
这是对的吗?
认为“staffid”是独一无二的:
在图像表上创建一列:
ALTER TABLE tbl_images
ADD staffid
INT NOT NULL
然后将您的insert语句更改为
$ sql =“INSERT INTO tbl_images(image_name,staffid)VALUES(:img,:staffid)”;
和
$ stmt-&gt; bindValue(“:img”,$ filename); $ stmt-&gt; bindValue(“:staffid”,$ this-&gt; session-&gt; userdata('staffid'));
然后当select只需要传入当前用户id时,添加一个where子句并相应地准备查询。这将返回该用户保存的所有图像:
WHERE staffid =:staffid;