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 8278663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:05:33+00:00 2026-06-08T09:05:33+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
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;

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

//RETRIEVE IMAGEID FROM IMAGE TABLE

$lastID = $mysqli->insert_id;

//INSERT INTO IMAGE_QUESTION DATABASE 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", $lastID, $sessid, $_POST['numQuestion'][$i]);

$insertimagequestion->execute();

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

$insert->close();

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             cat.png      1
    221             dog.png      4

The problem is that it is not inserting any data into the second table “Image_Question”, does anyone know why it is not inserting any data? There is no errors in the php file.

To upload a file, the user selects a file for the ajax uploader in the “QandATable.php” page, when the user clicks on upload, using AJAX it will go onto the imageupload.php page and does the uploading there. So the problem I have is that no errors will appear as they are on seperate pages.

  • 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-08T09:05:35+00:00Added an answer on June 8, 2026 at 9:05 am

    First, save the insert ID gained from your record addition (after the $insert->execute):

    $lastID = $mysqli->insert_id;
    

    Then reference $lastID later.

    To pull up my comment from below:

    $lastID = $insert->insert_id;
    

    I think it’s to do with swapping the handle names around – $mysqli, $insert etc.

    Hope I read the question correctly…

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

Sidebar

Related Questions

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
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 (
I'm using a code below to upload file (tens of MB) in streaming mode.
I'm trying to upload PHP file via php using ftp details as mentioned below.

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.