Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:53:40+00:00 2026-06-08T06:53:40+00:00

I have a mysqli code below where it uploads a file and insert data

  • 0

I have a mysqli code below where it uploads a file and insert data into the “Image” Table:

<?php

session_start();


$username="xxx";
$password="xxx";
$database="mobile_app";

  $mysqli = new mysqli("localhost", $username, $password, $database);

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
  }

$result = 0;

      move_uploaded_file($_FILES["fileImage"]["tmp_name"],
      "ImageFiles/" . $_FILES["fileImage"]["name"]);
      $result = 1;

    $imagesql = "INSERT INTO Image (ImageFile) 
    VALUES (?)";

            if (!$insert = $mysqli->prepare($imagesql)) {
      // Handle errors with prepare operation here
    }

    //Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$img);

//Assign the variable
$img = 'ImageFiles/'.$_FILES['fileImage']['name'];

 $insert->execute();

      }

  }



?>

So for example if I insert 2 images “cat.png” and “dog.png” into “Image” Database table, it will insert it like this:

ImageId         ImageFile

220             cat.png
221             dog.png

(ImageId is an auto increment)

Anyway what my question is that when a file is uploaded, not only is the data inserted into the table above, but I want to also be able to retrieve the ImageId that was inserted above and place it in the Image_Question table below so it would be like this:

 ImageId         SessionId      QuestionId

    220             cat.png      1
    221             dog.png      4

MY question is that how do I retrieve the ImageId from the Image Table and insert it into the Image_Question Table? Below is my code which will insert data into the Image_Question table using mysql, just need to be able to retrieve the ImageId from the Image Table:

 $imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId) 
        VALUES (?, ?, ?)";


        if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) {
          // Handle errors with prepare operation here
        }

    $sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');

    $insertimagequestion->bind_param("sss", $sessid, $_POST['numQuestion'][$i]);

            $insertimagequestion->execute();

            if ($insertimagequestion->errno) {
              // Handle query error here
            }

            $insertimagequestion->close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:53:43+00:00Added an answer on June 8, 2026 at 6:53 am

    After the first insert, you can use mysqli_insert_id to get the id, and the use that when you do the second insert into Image_Question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here my code, I need to insert into mysql database I have mysql,php,android 1)
the given code below insert data from an array to the mysql table.as its
I have the code below which grabs data from a table based on what
I have the php code below which help me get a photo's thumbnail image
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
I have a MySQL singleton class written in PHP. Its code is listed below:
Below I have included my code that uploads multiple images to a folder and
I have the PHP code below. It is supposed to select up to ten
I want to use prepared statement to insert and update data in mysqli, Below
I have the below script to import data from a csv file on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.