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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:19:31+00:00 2026-05-28T07:19:31+00:00

I’m having a problem with foreign key in mysql.I’m trying to insert a row

  • 0

I’m having a problem with foreign key in mysql.I’m trying to insert a row in table that contains foreign key but I’m facing with this error:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`prj1`.`tempoccasion`, CONSTRAINT `tempoccasion_ibfk_1` FOREIGN KEY (`tempid`) REFERENCES `temp` (`tempid`) ON DELETE CASCADE ON UPDATE CASCADE)

I’ve done SHOW CREATE TABLE query on tables(temp and occasion are source table-tempoccasion is the table with the foreign key)

CREATE TABLE `temp` (
 `tempid` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
 `image` text NOT NULL,
 `code` text NOT NULL,
 PRIMARY KEY (`tempid`),
 UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT
n   CREATE TABLE `occasion` (
 `occasionid` int(11) NOT NULL AUTO_INCREMENT,
 `occasionname` varchar(30) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
 `year` varchar(11) NOT NULL,
 `month` varchar(11) NOT NULL,
 `day` varchar(11) NOT NULL,
 `comment` text CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
 PRIMARY KEY (`occasionid`),
 UNIQUE KEY `occasionname` (`occasionname`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT
    CREATE TABLE `tempoccasion` (
 `tempid` int(11) NOT NULL,
 `occasionid` int(11) NOT NULL,
 PRIMARY KEY (`tempid`,`occasionid`),
 KEY `occasionid` (`occasionid`),
 CONSTRAINT `tempoccasion_ibfk_2` FOREIGN KEY (`occasionid`) REFERENCES `occasion` (`occasionid`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `tempoccasion_ibfk_1` FOREIGN KEY (`tempid`) REFERENCES `temp` (`tempid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT

the query that makes error is(that is written in php):

$tn=$_GET["tname"];
$on=$_GET["oid"];
$i=$_GET["image"];
$c=$_GET["code"];
$q="insert into temp(name,image,code)values('$tn','$i','$c')";
$r=mysql_query($q)or die("invalid query");

if ($r)
{
$q1="select tempid from temp where name like '$tn'";
$row1=mysql_query($q1)or die("invalid query1");
if ($row1)
{
$tid=$row1['tempid'];
echo $tid;
$q2="insert into tempoccasion (tempid,occasionid)values('$tid','$on')";
$r2=mysql_query($q2)or die("invalid query2");//this query contains error//

It would be great if anyone could help me.
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-28T07:19:31+00:00Added an answer on May 28, 2026 at 7:19 am

    You can’t use the result of a call to mysql_query as an array, as it is a MySQL resultset. You can use mysql_fetch_assoc() to get the individual rows from that resultset. In your case you need to do something like:

    [snip]
    $q1="select tempid from temp where name like '$tn'";
    $result1=mysql_query($q1)or die("invalid query1");
    if ($result1)
    {
    $row1 = mysql_fetch_assoc($result1);
    $tid=$row1['tempid'];
    echo $tid;
    [/snip]
    

    You can also use mysql_insert_id() to get the id of the last inserted record. So instead of doing a SELECT after the INSERT you can use that function to retrieve $tid.

    Furthermore, please read up on SQL injections and spend some time on making your code more secure and formatting (indenting, etc) of your code. There are some other thing wrong with your code that leads me to think you need to do some more research before going on…

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.