简体   繁体   English

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

[英]How to retrieve ImageId from one db table and store it in another db table

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. 我有一个INSERT函数,其中将图像文件名插入“图像”表的“ ImageFile”字段中,由于自动编号,每一行都有它自己的ImageId。 An example of this is below: 下面是一个示例:

ImageId    ImageFile

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

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. 我还想将ImageId插入到带有QuestionId和SessionId的另一个表中,以便该表(Image_Question)可以使用ImageId将Image表与Image Question表链接。 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. 现在,我尝试使用mysql_insert_id从图像表中检索ImageId并将其存储在Image_Question表的ImageId中。

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. 但是我似乎无法弄清楚我需要做些什么,目前将INSERTING值插入Image Table的工作正常,但没有在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()? 所以我的问题是插入图像表的每一行,如何从图像表中检索ImageId,然后使用mysql_insert_id()将其插入到Image_Question表中? Example below: 下面的例子:

ImageId   SessionId  QuestionId

23        AAA        1
24        AAA        2
25        AAA        3
26        AAA        4

I have coded the INSERT values for SessionId and QuestionId but just need help retrieving and inserting the ImageId. 我已经为SessionId和QuestionId编码了INSERT值,但只需要帮助检索和插入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();

?>

I have an old php version 5.2.13 because that is the version of the university's server. 我有一个旧的php版本5.2.13,因为那是大学服务器的版本。

You need to run 你需要跑步

$lastimageid = mysql_insert_id();

After you insert it into the database. 将其插入数据库后。

You need to reorder your php code to run 您需要重新排序您的php代码才能运行

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

Currently you retreive lastimageid, insert Image_Question then insert Image. 当前,您检索lastimageid,插入Image_Question,然后插入Image。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检索imageid并将其插入另一个表? (Mysqli) - How to retrieve imageid and insert it into another table? (Mysqli) 如何获取imageid并将其插入到另一个表中 - How to retrive imageid and insert it into another table Yii2 将表从一个数据库复制到另一个数据库 - Yii2 Copy table from one db to another db 流明将一个db表的数据移动到另一个db表 - Lumen move data of one db table to another db table 如何在php中从一个数据库表显示数据到另一数据库表的视图页? - How to display data form one db table to another db table's view page in php? 从db表1中选择数据并将其插入到php中的另一个db表2中 - Select data from db table 1 and insert it into another db table 2 in php 如何更改 rangeSlider 默认起始最小值和最大值,以便我可以从数据库表中存储和检索 - How do I change rangeSlider default starting minimum and maximum values so I can store and retrieve from a DB Table 检索所有记录并从另一个数据库插入另一个表 - Retrieve all record and insert to another table form another db 如何在数据库表中存储复选框值 - How to store checkbox values in db table 从一个表创建一个下拉列表,然后将所选值输入到另一个表中(相同的MySQL DB) - Create a dropdown list from one table and entering selected value into another table (same MySQL DB)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM