[英]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.