gpt4 book ai didi

php - 检索最后插入的 iD,然后上传到 MySQL。 #PDO #PHP

转载 作者:行者123 更新时间:2023-11-29 00:29:20 25 4
gpt4 key购买 nike

我有一个表单可以同时将 Textarea 信息Image 上传到 MySQL。

Textarea iDAUTO_INCREMENTImage iD 必须是textarea 的 副本。我已经尝试在 $_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>

数据库中的更多信息,我怀疑它是否有用。

  1. posts 表:文本区域信息所在的位置。

    *post_iD |留言 | uid_fk |知识产权 |创建 |*

  2. user_uploads 表:图像位置所在的位置。

    *image_iD |图片路径 | 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 总是返回 0:

您插入值的表必须有 PRIMARY KEY AUTO_INCREMENT 字段,最好是 int 类型。

关于php - 检索最后插入的 iD,然后上传到 MySQL。 #PDO #PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com