我正在尝试创建一个页面,有人可以上载文档或图像,并将其存储在MySQL数据库中,但是由于某种原因,文档/图像的内容实际上并未插入到我创建的表中。奇怪的是,表中的其余列都已填充,只有一个LONGBLOB
列没有填充。
这是表单的HTML代码:
<form method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" />
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<label>Select Event:</label>
<select name="eventID">
<?php
foreach($data as $e)
{
echo "<option value='" . $e["eventID"] . "'>" . $e["name"] . "</option>";
}
?>
</select>
<label>Your ID:</label>
<input name="contributorID" type="number" min="0" />
<label>Your Abstract:</label>
<textarea name="abstract" rows="10" cols="50"></textarea>
<label>Attachment:</label>
<input type="file" name="attachment" />
</form>
我相信我的表格是正确的,并确保存在具有最大值的隐藏字段并且编码正确。
这是处理表单POST的PHP代码:
$abstract = $this->model("Abstracts");
if (!empty($_FILES["attachment"]) && ($_FILES["attachment"]["error"] == 0))
{
$fileName = $_FILES["attachment"]['name'];
$tmpName = $_FILES["attachment"]['tmp_name'];
$fileSize = $_FILES["attachment"]['size'];
$fileType = $_FILES["attachment"]['type'];
//$fp = fopen($tmpName, 'r');
$content = file_get_contents($tmpName);
$content = addslashes($content);
//fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$abstract->insertAbstract($_POST["contributorID"], $_POST["eventID"], $_POST["abstract"], $content, $fileName, $fileType, $fileSize);
}
insertAbstract
类的Abstracts
方法:public function insertAbstract($contributorID, $eventID, $abstract, $attachment = null, $name = null, $type = null, $size = null)
{
$conn = new Credentials;
$this->connection = $conn->conn;
if (!empty($attachment))
{
$query = $this->connection->prepare("CALL sp_PopulateAbstractAttachments(?,?,?,?,?)");
$query->bind_param("ibssi", $abstractID, $attachment, $name, $type, $size);
$query->execute();
$query->close();
}
mysqli_close($this->connection);
}
存储过程背后的SQL:
CREATE PROC...
(abID INT, att LONGBLOB, n VARCHAR(500), t VARCHAR(30), s INT)
BEGIN
DELETE
FROM abstractattachments
WHERE abstractID = abID;
INSERT INTO abstractattachments
(abstractID, attachment, name, type, size)
VALUES (abID, att, n, t, s);
END
该行插入正确的
abID
,n
,t
,s
,但attn
似乎没有。在phpMyAdmin中,该值在表中只是空的。如果我错了,请纠正我,但是它不应该显示值吗?为了再次检查我是否正确,我创建了一个脚本来下载文件,但是当我打开文件时,它会显示为空文件。这是脚本:
public function downloadAbstract()
{
//instantiates connection object from credentials.php
$conn = new Credentials;
$this->connection = $conn->conn;
//manually change attachmentID parameter for script
$query = "SELECT name, type, size, attachment FROM AbstractAttachments WHERE attachmentID = 22";
$this->result = mysqli_query($this->connection, $query);
list($name, $type, $size, $attachment) = mysqli_fetch_array($this->result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $attachment;
mysqli_close($this->connection);
exit;
}
我在这里迷路了,所以任何指导都将受到欢迎。我已尽力将代码压缩。
最佳答案
我不擅长使用sql程序,请在mysql数据库中创建表后尝试以下代码,它将起作用
。如果确实能解决您的问题,请标记为正确答案。
sql
create table customer_account (photo_id int(55) primary key auto_increment,photo longblob,name varchar(88),account_no integer(86),address varchar(72),email varchar(45));
html
<html>
<body>
<font color=green size=4>upload Passport Photograph of aCustomer<BR>
Using PHP Uploads Technology</font></b><br>
<form method="post" action="upload.php" enctype="multipart/form-data">
<table class="main">
<tr>
<td class="header" colspan="2">General Account Opening Form Along with Passport Photograph of the Customer<BR>
Using PHP Uploads Technology
</td>
</tr>
<tr>
<td>Photo</td>
<td><input type="file" name="photo_upload" class="long"></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" class="long"></td>
</tr>
<tr>
<td>Account Number</td>
<td><input type="text" name="account_no" class="long"></td>
</tr>
<tr>
<td>Address</td>
<td><input name="address" type="text"></td>
<tr>
<td>Email</td>
<td><input name="email" type="TEXT"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Register!" /></td>
</tr>
</table>
</form>
</body>
</html>
upload.php
<?
$mysqli = new mysqli("localhost", "nickolysis_username", "nick_password", "photo_database");
$photo_upload='';// variable photo is set to photo_upload
$name=mysql_real_escape_string($_POST['name']);
$account_no=mysql_real_escape_string($_POST['account_no']);
$address=mysql_real_escape_string($_POST['address']);
$email=mysql_real_escape_string($_POST['email']);
$stmt = $mysqli->stmt_init();
// read image with variable read_customerphoto and then insert into database
move_uploaded_file($_FILES['photo_upload']['tmp_name'],"customerphoto.img");
$read_customerphoto = fopen("customerphoto.img","rb");
$photo = addslashes(fread($read_customerphoto,filesize("customerphoto.img")));
if($stmt->prepare("insert into customer_account(photo,name,account_no,address,email) VALUES (?, ?, ?, ?, ?)")) {
$photo_id= mysql_insert_id();
$stmt->bind_param('ssiss',$photo,$name,$account_no,$address,$email);
$stmt->execute();
// Close statement object
$stmt->close();
print "<font size=4 color=green>upload is successful</font> ";
}
else{
print "error";
}
/* close connection */
$mysqli->close();
?>