【发布时间】:2013-06-28 06:49:02
【问题描述】:
我有一个表单,可以同时将 Textarea Information 和 Image 上传到 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 值。出于这个原因有什么想法吗?
【问题讨论】: