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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:50:44+00:00 2026-06-13T08:50:44+00:00

I have two tables, users and comments. I need to add firstname and lastname

  • 0

I have two tables, users and comments. I need to add firstname and lastname to users and the comments to the comments table with the user id as a foreign key in comments. I am using phpmyadmin to do the add the foreign key constrain and relationships.

This is my html form:

<form action="dbconnect.php" method="post">
Firstname: <input type="text" name="firstname"><br />
Lastname: <input type="text" name="lastname"><br />
Comments: <textarea cols="30" rows="5" name="comments"></textarea><br />
<input type="submit">
</form>

This is my php insert code:

mysql_select_db("test", $db_server);

$sql="INSERT INTO users (Firstname, Lastname)
VALUES
('$_POST[firstname]','$_POST[lastname]')";

$sql="INSERT INTO comments (Comment)
VALUES
('$_POST[comments]')";


if (!mysql_query($sql,$db_server))
    {
    die('Error' . mysql_error());   
    }
echo "1 record added" ;

mysql_close($db_server);

and this is the error i’m getting:

Error: Cannot add or update a child row: a foreign key constraint fails
(test.comments, CONSTRAINT comments_ibfk_2 FOREIGN KEY
(useridfk) REFERENCES users (id) ON DELETE CASCADE ON UPDATE
CASCADE)

I am new to php and phpmyadmin so any help is appreciated.

  • 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-13T08:50:45+00:00Added an answer on June 13, 2026 at 8:50 am

    First of all, you need to run the INSERT query on the users table so the user record is created.

    mysql_select_db("test", $db_server);
    
    $sql="INSERT INTO users (Firstname, Lastname)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]')";
    
    mysql_query($sql);
    
    if (!mysql_query($sql,$db_server))
        {
        die('Error' . mysql_error());   
        }
    echo "1 record added" ;
    

    You then need to retrieve the id of the newly created record on the users table

    $newIdQuery = mysql_query("SELECT LAST_INSERT_ID() AS userId FROM users");
    $newId = mysql_fetch_assoc($newIdQuery);
    

    Then you can use that userId to add the comments, with the user id foriegn key properly populated.

    Note: When building MySQL queries using the mysql_* functions, if you wrap a php variable in { } it will insert the value of the php variable into the SQL statement.

    $sql="INSERT INTO comments (id, Comment)
    VALUES
    ({$newId["userId"]}, '$_POST[comments]')";
    
    
    if (!mysql_query($sql,$db_server))
        {
        die('Error' . mysql_error());   
        }
    echo "1 record added" ;
    
    mysql_close($db_server);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables- users and language with a foreign key link of their
I have two tables in database: comments table users table | u_id | text
I am using EF4 with the EDM designer. I have two tables : Users
Say I have two tables, user and comment . They have table definitions that
I have two tables, Users and TempUsers and I need to do operations on
I have two tables. One table contains comments on posts, another contains commenter information
In my application I have two tables: Users and Profiles. A user has a
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
Hi, I have these two tables: users and friends (friend_status = 1 means the

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.