[英]Retrieving last inserted iD then uploading to MySQL. #PDO #PHP
我有一个将Textarea信息和图像同时上传到MySQL的表格。
Textarea iD
是AUTO_INCREMENT
而Image iD
必须是textarea's
副本。 我尝试在$_POST
内获取lastInsertId()
; 返回Fatal error: Call to undefined function lastInsertId()
。
这是我当前的isset代码,它将信息上传到Function和/或查询:
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 | ip | 创建| *
user_uploads table
:图像位置所在的位置。
* image_iD | image_path | uid_fk | image_id_fk *
注意: image_id_fk
应该等于post_iD
(这很明显)。
lastInsertId()
怎么样? 假设要使用?
编辑1:上传完成后,image_id_fk的插入值等于0,而不是post_iD值。 有什么想法吗?
您可以在成功执行INSERT
语句后立即使用它。 但是,您必须始终检查执行是否成功(或是否存在受影响的行),因为以前执行的语句中可能有last insert ID
。
$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()));
$this->db->lastInsertId(); //Upon success, is available
例如:您具有创建新帖子的功能。 成功使用此函数可以返回最后一个插入ID值,因此您可以向用户显示该值。 喜欢:
if (isset($_GET['id'])){
$id = $manager->createPost($_GET['id']) ;
if (is_numeric($id)){
echo "Your article has been created. ID: {$id}" ; //Or link to it.
}
}
更新:
这是我对您的问题的建议: lastInsertId always retuns 0
:
您要在其中插入值的表必须具有PRIMARY KEY AUTO_INCREMENT
字段,最好是int
类型。
您以错误的方式访问pdo lastinsertid 。 它需要数据库连接对象。
$post_iD = lastInsertId();
应该
$post_iD = $db->lastInsertId();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.