我制作了一个可以上传图像文件的页面,但我希望能够添加一些带有图片的详细信息并同时上传,我不知道如何从带有图片的文本框中上传详细信息到我的 table 上。
这是我的index.php
代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fa-IR">
<head>
<meta name="author" content="www.hamyarprojects.com" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload file</title>
<link href="css/Style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="mainframe" >
<div id="lp" class = "panel panel-primary">
<div class = "panel-heading">
<h3 class = "panel-title">Upload file test</h3>
</div>
<div class = "panel-body">
<form action="Upload.php" method="post" enctype="multipart/form-data">
<p id="btn_box">
<input type="file" name="upload_image" />
</p>
<p id="btn_box">
<input type="submit" value="Upload Photo" name="submit">
<input type="reset" value="Clear" name="reset">
</p>
</form>
</div>
</div>
</div>
</body>
</html>
这是upload.php
代码
<link href="css/Style.css" rel="stylesheet" type="text/css">
<div class="main">
<?php
if (!isset($_FILES['upload_image'])) {
echo '<p>Please select a image</p>';
} else {
try {
upload();
echo '<p>Upload successfully</p>';
} catch(Exception $e) {
echo '<h4>'.$e->getMessage().'</h4>';
}
}
function upload() {
if (is_uploaded_file($_FILES['upload_image']['tmp_name'])
&& getimagesize($_FILES['upload_image']['tmp_name']) != false
) {
$size = getimagesize($_FILES['upload_image']['tmp_name']);
$type = $size['mime'];
$imgfp = fopen($_FILES['upload_image']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['upload_image']['name'];
$maxsize = 99999999;
if ($_FILES['upload_image']['size'] < $maxsize ) {
$dbh = new PDO("mysql:host=localhost;dbname=bmc", 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("INSERT INTO imageblob (image_type, "
. "image, image_size, image_name) VALUES (? ,?, ?, ?)");
$stmt->bindParam(1, $type);
$stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
$stmt->bindParam(3, $size);
$stmt->bindParam(4, $name);
$stmt->execute();
} else {
throw new Exception("size invalid");
}
} else {
throw new Exception("invalid format !");
}
}
</div>
最佳答案
你能不能只添加一个文本框
<input type="text" name="imageDetails" />
然后在代码中捕获该文本框
$imageDetails = filter_input(INPUT_POST, "imageDetails", FILTER_SANITIZE_SPECIAL_CHARS);
然后向表 imageBlob
添加一列,名为 imageDetails
然后将您的查询更改为
$stmt = $dbh->prepare("INSERT INTO imageblob (image_type ,image, image_size, image_name, imageDetails) VALUES (? ,?, ?, ?, ?)");
然后添加绑定(bind)参数
$stmt->bindParam(5, $imageDetails);
希望这有帮助
关于php - 使用 php 在 mysql 中插入带有标题和详细信息的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796923/