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

  • Home
  • SEARCH
  • 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 8560475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:17:03+00:00 2026-06-11T16:17:03+00:00

I am using the code below that uploads a file and inserts data into

  • 0

I am using the code below that uploads a file and inserts data into the “Image” table using mysqli:

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

$result = 0;

//UPLOAD IMAGE FILE

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

$result = 1;

//INSERT INTO IMAGE DATABASE TABLE

$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();

$insertimagequestion->execute();

//IF ANY ERROR WHILE INSERTING DATA INTO EITHER OF THE TABLES
if ($insert->errno) {
  // Handle query error here
}

$insert->close();


$lastID = $mysqli->insert_id;

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


    if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) {
      // Handle errors with prepare operation here
       echo "Prepare statement err";
    }

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

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

        $insertimagequestion->execute();

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

        $insertimagequestion->close();

}
?>

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 I want to do 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            AAA             1 
    221            AAB             4

But it is not inserting anything in the Image_Question table. How when I upload an image, it not only inserts data into the “Image” table but be able to insert the data into the “Image_Question” table as well?

  • 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-11T16:17:04+00:00Added an answer on June 11, 2026 at 4:17 pm

    Forgive me if I’m way off, but doesn’t the php execute sequentially, in other words,
    Shouldn’t this: $insertimagequestion->execute();
    go after this: if (!$insertimagequestion = $mysqli->prepare($imagequestionsql))
    ?
    Perhaps the first occurrence of $insertimagequestion->execute(); is wiping out the insert id. I’d put in something to print & make sure you’re getting the insert_id.

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

Sidebar

Related Questions

I am using the code below that uploads a file and inserts data into
I have a mysqli code below where it uploads a file and insert data
Below is the code that I'm using in ms access to list the data
I am using the code below to upload a file to a remote php
im using the below code to upload multiple images, take the image file name,
I searched online for a code that does PHP image upload using ajax. I
I'm trying to display a text using the code below. The problem is that
I am using the below code, but I am not getting that why it
I can insert a row by using code below. USE pmdb; INSERT INTO md5_tbl
Using the code below I can insert a new row to my table (

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.