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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:23:29+00:00 2026-06-14T09:23:29+00:00

**Hi. These are my structures : I got 2 tables in phpMyAdmin : Class

  • 0

**Hi. These are my structures :

I got 2 tables in phpMyAdmin :

  • Class ( classid(pk), className )
  • Students ( studentid(pk), classid(fk), studentName )
    1 form.php and 1 form_post.php

I’ve done this in form.php**

include_once("connection.php");


    $className= $_POST["className"];
    $studentName1= $_POST["studentName1"];
    $studentName2= $_POST["studentName2"];
    $studentName3= $_POST["studentName30"];


    $sql = "insert into class(className) values ('$className')";


    mysql_query($sql);

    $lastid=mysql_insert_id();


    $sql2= "INSERT INTO students (studentid, classid, studentName)
            VALUES (' ', $lastid, '$studentName')";

    mysql_query($sql2);

form_post.php :

            <tr>
              <td width="140"><span class="order">Class Name: </span></td>
              <td><span class="order">
                <input name="className" type="text" id="className" size="35" />
              </span></td>
            </tr>
            <tr>
              <td width="132"><span class="order">Student's Name: </span></td>
              <td><span class="order">
                <input type="text" name="studentName1" id="studentName1" />
              </span></td>
            </tr>
           <tr>
              <td width="132"><span class="order">Student's Name: </span></td>
              <td><span class="order">
                <input type="text" name="studentName2" id="studentName2" />
              </span></td>
            </tr>
            <tr>
              <td width="132"><span class="order">Student's Name: </span></td>
              <td><span class="order">
                <input type="text" name="studentName3" id="studentName3" />
              </span></td>
            </tr>

It’s a form for registering class and students in it.
In phMyAdmin student table, there’s a field/column for studentid(pk), a field/column for classid(fk), a field/column for studentName while this code which I got from my friend got 3 students name field. How to insert the 3 students name into the phpMyAdmin student table’s studentName column with the same class foreign key?
The red colored words are the confusing one.
I’m still a beginner and I tried all the night to get this work. But, I’m still stuck.
Thank you 🙂

  • 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-14T09:23:30+00:00Added an answer on June 14, 2026 at 9:23 am

    You can use insert multiple rows depending on if you’re inserting the same data into the same tables

    Always good to escape your values

    $className= mysql_real_escape_string($_POST["className"]));
    $studentName1= mysql_real_escape_string($_POST["studentName1"]);
    $studentName2= mysql_real_escape_string($_POST["studentName2"]);
    $studentName3= mysql_real_escape_string($_POST["studentName3"]);
    
    $sql2= "INSERT INTO students (studentid, classid, studentName)
            VALUES (' ', $lastid, '$studentName1'),
                   (' ', $lastid, '$studentName2'),
                   (' ', $lastid, '$studentName3')";
    
    mysql_query($sql2);
    

    Recommendations:

    1.Learn to prevent from MySQL Injections: Good Link

    2.Mysql extension is not recommended for writing new code. Instead, either the mysqli or PDO_MySQL extension should be used. More reading: PHP Manual

    Second Solution:

    if(!empty($studentName1)) {
      $sql2= "INSERT INTO students (studentid, classid, studentName)
                VALUES (' ', $lastid, '$studentName1')";
    
      mysql_query($sql2);
    }
    
    if(!empty($studentName2)) {
      $sql2= "INSERT INTO students (studentid, classid, studentName)
                VALUES (' ', $lastid, '$studentName2')";
      mysql_query($sql2);
    }
    
    if(!empty($studentName3)) {
      $sql2= "INSERT INTO students (studentid, classid, studentName)
                VALUES (' ', $lastid, '$studentName3')";
      mysql_query($sql2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got four tables. The structure of these tables is shown below (I am
I have these two data structures that I continually find myself choosing between when
Say I have a database called Poll and have these two table structures. poll
The task is to make changes to current tables structure without recreating these tables.
I've got three tables; Auctions, Auction Bids and Users. The table structure looks something
I got 2 tables storing prices of the product. The main table is Product
The scenario I've got two tables with identical structure. TABLE [INFORMATION], [SYNC_INFORMATION] [ITEM] [nvarchar](255)
I am working on a couple of link tables and I got to thinking
I've got two tables - one is Product and one is ProductSearchResult. Whenever someone
I've got a categories table, and sub-category tables 3 levels deep (in the database

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.