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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:48:54+00:00 2026-05-29T04:48:54+00:00

I have the following two mySQL tables with the ‘child’ table having a DELETE

  • 0

I have the following two mySQL tables with the ‘child’ table having a DELETE CASCADE action set from the ‘parent’:

PARENT TABLE

CREATE TABLE `userdetails` (
  `userid` int(6) NOT NULL auto_increment,
  `forename` varchar(20) NOT NULL,
  `surname` varchar(30) NOT NULL,
  `emailaddress` varchar(150) NOT NULL,
  `password` varchar(200) NOT NULL,
  `passwordhint` varchar(20) NOT NULL,
  `subscriptionexpiration` date NOT NULL,
  `salt` varchar(200) NOT NULL,
  PRIMARY KEY  (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

CHILD TABLE

CREATE TABLE `locations` (
  `userid` int(6) NOT NULL,
  `locationid` int(6) NOT NULL auto_increment,
  `locationname` varchar(80) NOT NULL,
  PRIMARY KEY  (`locationid`),
  KEY `userid` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- 
-- Constraints for dumped tables
-- 

-- 
-- Constraints for table `locations`
-- 
ALTER TABLE `locations`
  ADD CONSTRAINT `locations_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `userdetails` (`userid`) ON DELETE CASCADE;

I’m then trying to run the following php script:

<?php
require("phpfile.php");

// Gets data from URL parameters
$userid = $_POST["userid"];
$locationname = $_POST["locationname"];


// Opens a connection to a MySQL server
$connection = mysql_connect ("hostname", $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}


// Insert new row with user data
$query = "INSERT INTO locations (userid, locationname) VALUES ('$userid', '$locationame')";
$result = mysql_query($query);

if (!$result) {
  die('Invalid query: ' . mysql_error());
}

and I receive this error:

Invalid query: Cannot add or update a child row: a foreign key constraint fails (`dbname/locations`, CONSTRAINT `locations_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `userdetails` (`userid`) ON DELETE CASCADE)

I’m not sure why I’m getting this as there is a record in the parent table. I’ve been working on this for hours now and I just can’t find the answer.

I just wondered whether someone could perhaps take a look at this please and let me know where I’m going wrong.

Many thanks

  • 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-05-29T04:48:57+00:00Added an answer on May 29, 2026 at 4:48 am

    Follow the sequence:

    First I had insert some data into userdetails

    INSERT INTO `test`.`userdetails` (`userid`, `forename`, `surname`, `emailaddress`, `password`, `passwordhint`, `subscriptionexpiration`, `salt`) VALUES (NULL, 'John', 'Doo', 'johndoo@john.com', '1234', 'seq.', '2012-02-02', '!@#$%*ERTYFGHJCVBFGH%*RUJK');
    

    Detail: If I try to insert into child table first it returns to me the message you already got. Then first I need to insert data into userdetails. Pay attention that you have a foreign key, then you need the userid (your key) to insert into child table. Right?

    Now take a look how I had insert the data into child table

    INSERT INTO `test`.`locations` (`userid`, `locationid`, `locationname`) VALUES ('2', '25', 'Brazil');
    

    1 row(s) inserted.

    Can you see the userid (2) ?

    Then I recommend:

    When user register a new account, you also create the insert for location table (child table), ’cause you will need the userid in the location table when user come back to update data. Otherwise you will got this error messages.

    I tested here and works! Let me know if works for you.

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

Sidebar

Related Questions

I have two mysql tables that have the following structure: Table 1: ---ID---------NAME------- ---1-----
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3
using mysql and php, I have two tables, I'm french so the table names
I have the following query in mysql 5.1.41: select distinct table_schema from information_schema.tables where
I have two tables in MySQL that I'm comparing with the following attributes: tbl_fac
I have created two tables ORDERS and ORDERITEMS with the following constraint: alter table
I have two tables in my MySQL database (table1 and table 2). I want
I have the two following tables table A | id | name | |
I have two tables in mysql: Results Table : 1046928 rows. Nodes Table :
I have two tables in MySQL #messages table : messageid messagetitle . . #usersmessages

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.