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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:09:41+00:00 2026-06-04T22:09:41+00:00

Not sure how to describe my problem, but lets try.. I am working on

  • 0

Not sure how to describe my problem, but lets try..

I am working on a small news portal php + mysql project
I have two tables in mysql database.

1: news (
   n_id (primary key)
   n_title
   n_text
)

2: news_image (
   ni_id (primary key)
   n_id  (foreign key from news table)
   ni_name
   ni_caption
)

(n_ prefix is for new and ni_ is prefix for news_image)

news table will hold the news story and news_image table will store the names and captions for the images of the news. separate table for news images is because on later stage, i will be adding more than one images for each story. the script works fine (as i have tested it by entering data in data base through phpmyadmin.

Problem:
I created a php script to add data to news table and to upload images to file system. it works fine. I used this query to add row to news table

INSERT INTO news
SET
n_title = '$title',
n_text = '$text'

n_id is the primary key (integer and is AUTO_INCREMENT).. this works fine but I want a query that will execute just after the execution of above query and will automatically insert a new row in news_image table, taking n_id of the row that has just been inserted in news table. ni_name and ni_capption in news_image table is not a problem, I can get it easily but getting the n_id from news table of the newly inserted row is the problem… How can I get n_id…????? Please provide a detailed answer as I am newbie to php + mysql..
Thanks for reading…waiting for reply..

  • 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-04T22:09:42+00:00Added an answer on June 4, 2026 at 10:09 pm

    Have you tried getting the ID of the last inserted news item? Like:

    $lastItemID = mysql_insert_id();
    

    Then you can use it to create the entry for the image:

    INSERT INTO news_image
    SET
    n_id= '$lastItemID',
    ni_name = '$name',
    ni_caption  = '$caption'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am not sure if I can describe the problem, but I will try
I'm not sure how to describe this best. But I have a problem understanding
I'm not sure how to describe this, but I'll try with some basic code:
Im not sure if this clear enough to describe the problem... I have 2
I'm not sure how to describe this but I'm trying to create a base
I'm not quite sure how to describe what I'm looking to do, but I'll
Not sure how to describe this for sure, but I think I've boiled down
I tried to search for this, but I was not sure how to describe
I am not sure how to describe my Problem. Basically I want to write
Not even sure if this is feasible, but here's the use case: I have

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.