我有一个表单可以同时将文本区域信息和图像上传到 MySQL。
Textarea iD
是,AUTO_INCREMENT
而Image iD
必须是textarea's
副本。我已经尝试过$_POST
获得lastInsertId()
;返回Fatal error: Call to undefined function lastInsertId()
。
这是我当前将信息上传到函数和/或查询的 isset 代码:
if(isset($_POST['update']) && isset($_FILES['photo1'])){
$update = $_POST['update'];
$data = $Wall->Insert_Update( $uiD, $update);
$name = $_FILES['photo1']['name'];
$tmp_name = $_FILES['photo1']['tmp_name'];
$target = "uploads/". $_FILES['photo1']['name'];
if (move_uploaded_file($tmp_name,$target)) {
$sth = $db->prepare("SELECT post_iD FROM posts WHERE uid_fk = :uiD");
$sth->execute(array(':uiD' => $uiD));
$post_iD = lastInsertId();
$sth = $db->prepare('INSERT INTO user_uploads (image_path, uid_fk, image_id_fk) VALUES (:image_path, :uiD, :image_id_fk)');
$sth->execute(array(':image_path' => $target, ':uiD' => $uiD, ':image_id_fk' => $post_iD));
}
}
这是正在上传文本区域的 Insert_Update:
PUBLIC FUNCTION Insert_Update( $uiD, $update){
$sth = $this->db->prepare("SELECT post_iD,message FROM posts WHERE uid_fk = :uiD ORDER by post_iD DESC LIMIT 1");
$sth->execute(array(':uiD' => $uiD));
$result = $sth->fetch();
if ($update!=$result['message']){
$sth = $this->db->prepare("INSERT INTO posts ( message, uid_fk, ip, created) VALUES ( :update, :id, :ip, :time)");
$sth->execute(array(':update' => $update, ':id' => $uiD, ':ip' => $_SERVER['REMOTE_ADDR'], ':time' => time()));
$sth = $this->db->prepare("
SELECT M.post_iD, M.uid_fk, M.message, M.created, U.username
FROM Posts M, users U
WHERE M.uid_fk=U.uiD
AND M.uid_fk = ?
ORDER by M.post_iD DESC LIMIT 1");
$sth->execute(array($uiD));
$result = $sth->fetchAll();
return $result;
} else {
return false;
}
}
形式:
<form method="POST" action="" enctype="multipart/form-data">
<textarea name="update" id="update" class="_iType"></textarea>
<input type="file" name="photo1">
<input type="submit" value="post" class="update_button">
</form>
数据库中的更多信息,我怀疑它是否有用。
posts table
: textarea 信息所在的位置。*post_iD | 留言 | uid_fk | 知识产权 | 已创建 |*
user_uploads table
:图像位置的位置。*image_iD | 图像路径 | uid_fk | image_id_fk*
注意:image_id_fk
应该相等post_iD
(这是显而易见的)。
怎么样lastInsertId()
; 假设被使用?
编辑 1:上传完成后 image_id_fk 的插入值等于 0,而不是 post_iD 值。出于这个原因有什么想法吗?