每当我试图将带有图像的数据插入到数据库中时,都不会提交或保存图像。它只给出了类似于这个BLOB-0B,但数据(文本)保存得很成功。
external.php
class Dbother {
private $host = 'localhost';
private $user = 'root';
private $password = '';
private $dbname = 'pcnl';
private $conn = '';
function connect() {
$this->conn = mysql_connect($this->host, $this->user, $this->password) or die (mysql_error());
mysql_select_db($this->dbname, $this->conn) or die(mysql_error());
}
function addEvent($title, $place, $date, $people, $content, $eventImg, $EventExt) {
$sql = "insert into tbl_event values('$title', '$place', '$date', '$people', '$content', null, null, '$eventImg', '$EventExt', null)";
mysql_query($sql) or die (mysql_error());
mysql_close();
}
}event.php
<?php
require_once 'dbother.php';
$object = new Dbother();
$object->connect();
if(isset($_POST['title'])){
$title = $_POST['title'];
$place = $_POST['place'];
$date = $_POST['date'];
$people = $_POST['people'];
$content = $_POST['content'];
$eventImg=file_get_contents($_FILES['pic']['tmp_name']);
$eventImg=mysql_escape_string($eventImg);
@list(, , $imtype, ) = getimagesize($_FILES['pic']['tmp_name']);
if ($imtype == 3){
$EventExt="png";
}elseif ($imtype == 2){
$EventExt="jpeg";
}elseif ($imtype == 1){
$EventExt="gif";
}
$object->addEvent($title, $place, $date, $people, $content, $eventImg, $EventExt);
header('Location: events.php');
exit;
}
?>
<div>
<h4>New Event</h4>
<form name="eventform" id="eventform" method="post" action="events.php">
<p> Title: <input type="text" name="title" id="title" required pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,80}" title="Please enter valid title of you event."/></p>
<p> Where: <input type="text" name="place" id="place" required pattern="[A-Za-z0-9 ,.)(*!?|<>:]{5,100}"/></p>
<p>When: <input type="date" name="date" id="date" width="40px;" required /></p>
<p>People Involved: <input type="text" name="people" id="people" required/></p>
<p>Content:</p>
<textarea rows="4" cols="50" required name="content"></textarea>
<p><input type="file" name="pic" id="file" style="float:left">
<a href ="events.php"><input name="btnexit" type="button" id="btnexit" value="Cancel" style="width:80px; height:30px; margin:5px; border:2px solid #757575; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; float:right"></a>
<input name="btnfinish" type="submit" class="btnfinish" value="Done" style="width:80px; height:30px; margin:5px; border:2px solid #757575; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; float:right"></p>
</form>抱歉,密码排得太长了,希望你能帮我。
发布于 2013-09-04 15:17:38
首先,我建议使用折叠INSERT INTO语法,其中绝对确定每个列的值:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);第二:表单标记应该具有以下属性enctype="multipart/form-data",以允许文件上载,因此表单应该如下所示:
<form name="eventform" id="eventform" method="post" action="events.php" enctype="multipart/form-data">正如其他评论者所说,您必须迁移mysql_函数。您可以查看mysql标记并查看这个问题的答案,以了解为什么:PHP中的函数?
https://stackoverflow.com/questions/18617052
复制相似问题