如何从一个 db 表中检索 ImageId 并将其存储在另一个 db 表中

编程入门 行业动态 更新时间:2024-10-28 09:17:48
本文介绍了如何从一个 db 表中检索 ImageId 并将其存储在另一个 db 表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 INSERT 函数,它将图像文件名插入到Image"表的ImageFile"字段中,由于自动编号,每行都有自己的 ImageId.一个例子如下:

I have a INSERT function where it inserts the image filename in the 'ImageFile' field in the "Image" table, each row has it's own ImageId thanks to auto number. An example of this is below:

ImageId ImageFile 23 orange.jpg 24 flowers.png 25 castle.png 26 orange.jpg

我还想做的是将 ImageId 插入到另一个带有 QuestionId 和 SessionId 的表中,以便该表 (Image_Question) 可以使用 ImageId 将 Image 表与 Image Question 表链接起来.现在我正在尝试使用 mysql_insert_id 从图像表中检索 ImageId 并将其存储在 Image_Question 表中的 ImageId 中.

What I want to do is also insert the ImageId into another table with the QuestionId and SessionId so that this table (Image_Question) can use the ImageId to link the Image table with the Image Question table. Now I am trying to use mysql_insert_id to retrieve the ImageId from the Image Table and store it in the ImageId in the Image_Question table.

但我似乎无法弄清楚我需要做什么,目前将值插入图像表工作正常,但它没有在 Image_Question 表中插入任何值.

But I can't seem to figure out what I need to do, at the moment the the INSERTING values into the Image Table is working fine but it does not insert any values inside the Image_Question table.

所以我的问题是对于插入到 Image 表中的每一行,如何从 Image 表中检索 ImageId,并使用 mysql_insert_id() 将其插入到 Image_Question 表中?下面的例子:

So my question is for each row inserted into the Image table, how do I retrieve the ImageId from the Image Table, and insert it into the Image_Question table using mysql_insert_id()? Example below:

ImageId SessionId QuestionId 23 AAA 1 24 AAA 2 25 AAA 3 26 AAA 4

我已经对 SessionId 和 QuestionId 的 INSERT 值进行了编码,但只需要帮助检索和插入 ImageId.以下是当前代码:

I have coded the INSERT values for SessionId and QuestionId but just need help retrieving and inserting the ImageId. Below is the current code:

<?php session_start(); //connect to db $result = 0; $i = 0; $insertimage = array(); $lastimageid = mysql_insert_id(); move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]); $result = 1; $imagesql = "INSERT INTO Image (ImageFile) VALUES ('ImageFiles/".mysql_real_escape_string($_FILES['fileImage']['name'])."')"; for($i = 0; $i < $c; $i++ ){ $insertimage[] = "'". mysql_real_escape_string( $lastimageid [$i] ) ."','". mysql_real_escape_string($_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['numQuestion'][$i] ) ."'"; } $imageinsertsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId) VALUES (" . implode('), (', $insertimage) . ")"; mysql_query($imageinsertsql); mysql_query($imagesql); } mysql_close(); ?>

我有一个旧的 php 版本 5.2.13,因为那是大学服务器的版本.

I have an old php version 5.2.13 because that is the version of the university's server.

推荐答案

你需要重新排列你的php代码才能运行

You need to reorder your php code to run

mysql_query($imagesql); $lastimageid = mysql_insert_id(); mysql_query($imageinsertsql);

目前您检索 lastimageid,插入 Image_Question,然后插入图像.

Currently you retreive lastimageid, insert Image_Question then insert Image.

更多推荐

如何从一个 db 表中检索 ImageId 并将其存储在另一个 db 表中

本文发布于:2023-10-26 07:32:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将其   db   ImageId

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!