如何检索imageid并将其插入另一个表 [英] How to retrive imageid and insert it into another table

查看:53
本文介绍了如何检索imageid并将其插入另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 表链接起来.但是我如何编码它以便当我在上表中插入图像详细信息时,它将检索它的 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. But how do I code it so that when I insert image details in the table above, then it will retrieve it's ImageId and also store it in the ImageId in the Image_Question table. 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

$i = 0;

$insertimage = array();

for($i = 0;  $i < $c; $i++ ){


    $insertimage[] = "'". mysql_real_escape_string($_SESSION['id'] ) . 
                    ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". 
                    mysql_real_escape_string( $_POST['numQuestion'][$i] ) ."'";

}


 $imagesql = "INSERT INTO Question (ImageId, SessionId, QuestionId) 
    VALUES (" . implode('), (', $insertimage) . ")";

echo($imagesql);

mysql_close();


?>

我有一个旧的 php 版本 5.2.13,因为这是大学服务器的版本,所以我必须使用 mysql() 代码,即使我知道使用 PDO 或 Mysqli 更好.

I have an old php version 5.2.13 because that is the version of the university's server so I have to use mysql() code even though I know it is better to use PDO or Mysqli.

推荐答案

我认为您正在寻找的是 mysql_insert_id .它将返回最后插入行的 id,因此您可以将其分配给变量并使用该变量将 id 插入到另一个表中进行下一个查询

I think what you are looking for is mysql_insert_id . It will return the id of last inserted row, so you can assign it to variable and use that varriable to insert the id into another table with next query

这篇关于如何检索imageid并将其插入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆