FOREIGN KEY - Cannot add or update a child row

Hello

i am getting this error

Problem with query Cannot add or update a child row: a foreign key constraint fails (news.video, CONSTRAINT video_ibfk_1 FOREIGN KEY (sid) REFERENCES video (vid) ON DELETE CASCADE ON UPDATE CASCADE)

i have checked other topic on this issue here but my issue did not got solved.
please let me know what should i do to solve this issue

did you check what the keys are and if they exist? provide an example.

if you need the code then this is code

if(empty($_POST['title'])||empty($_POST['description'])||empty($_POST['link'])){
echo '<span style="color: red;"> Please Fill in the required fields</span>';
				} else {
		
		
		
						$title = clean( $_POST[ 'title' ] );
					$desc = clean( $_POST[ 'description' ] );
					
$query = "insert into video (sid,title,titleimage,description,link,datetime) values ('$_POST[cat]', '$title','$file', '$desc','$_POST[link]',now())";

					$result = mysqli_query( $con, $query );

					if ( $result ) {
						echo '<span style="color: green;">Video Added Successfully</span>';
					} else {
					echo "Problem with query " . mysqli_error($con);
					}
				}
							}	}
				}
							}else{
	echo "Please add Image";
	
	
}	
}

and do the referenced keys you are trying to insert or update exist in the expected columns of the other table?

this is the full code thing yes

<?php

			if ( isset($_POST[ 'submit' ] ) ) {
					
if($_FILES['upload']['size']>0)	{	
if($_FILES['upload']['size']<8000000){
if($_FILES['upload']['type']=='image/png'||$_FILES['upload']['type']=='image/jpeg'||$_FILES['upload']['type']=='image/gif'){
	

	
	$dir="images/";
	$file=$_FILES['upload']['name'];
//
	if(file_exists($dir.$_FILES['upload']['name'])){
		
		
		
		echo "this file allready exist";
		
	}else{
		

		
/*move_uploaded_file($_FILES['upload']['tmp_name'],$dir.$_FILES['upload']['name']);*/

				
if(empty($_POST['title'])||empty($_POST['description'])||empty($_POST['link'])){
echo '<span style="color: red;"> Please Fill in the required fields</span>';
				} else {
		
		
		
						$title = clean( $_POST[ 'title' ] );
					$desc = clean( $_POST[ 'description' ] );
					
$query = "insert into video (sid,title,titleimage,description,link,datetime) values ('$_POST[cat]', '$title','$file', '$desc','$_POST[link]',now())";

					$result = mysqli_query( $con, $query );

					if ( $result ) {
						echo '<span style="color: green;">Video Added Successfully</span>';
					} else {
					echo "Problem with query " . mysqli_error($con);
					}
				}
							}	}
				}
							}else{
	echo "Please add Image";
	
	
}	
}
									 
				?>

			<form method="post" action="" enctype="multipart/form-data">
				<div class="form-group">
					<lable>Video Title</lable>
					<input type="text" class="form-control" name="title"/>
				</div>
				
				
				<div class="form-group">
					<lable>Title Image</lable>
					<input type="file" class="form-control" name="upload"/>
				</div>
				
				<div class="form-group">
					<lable>Video Description</lable>
					<textarea class="form-control" name="description" cols="40" rows="4"/></textarea>
				</div>
				
				<div class="form-group">
					
					<lable>Video Link</lable>
					<input type="url" class="form-control" name="link"/>
				</div>
				
				
				<div class="form-group">
					<lable>Category</lable>
					<select class="form_control" name="cat">
						<?php 
		
		$query="select * from subcategory";
		$sql=mysqli_query($con,$query);
		if(mysqli_num_rows($sql)>0){
			while($row=mysqli_fetch_assoc($sql)){
				
				
		
		?>
						<option value="<?php echo $row['sid']; ?>">
							<?php echo $row['scatname']; ?>
						</option>
						<?php 
				}	
		}		
			?>
					</select>
				</div>

this is a database related problem, no HTML or PHP needed. provide an example with schema and test data.

like…? any example or what should i do to show that

do a SHOW CREATE TABLE for both tables

suggest you do this in the SQL window of whatever front end app you are using

The problem is either you have mismatched/orphan data or the columns you are trying to create the keys on are not the same type.

i.e: one is an integer, the other is varchar. They both would need to be integer.

all ready posted the screenshot for the db you can check above anything else should i do …?

what should we do with a screenshot, how do we convert this into a working example, do you expect us to copy this by hand? You even missed to show half of the tables involved. And this problem relies on the data you are putting into the query, so at least you have to show the queries and some test data

provide an example with schema and test data.

Just use this tool to provide a comprehensable example with all of the necessary data:

You are only showing the details of one of the tables. Need to see both.
If the parent table and the child table have the same column type for the columns you are keying then it is a data problem.

Also, pictures are for hanging on the wall. Post the SQL Schema.

anything else? yeah, please do a SHOW CREATE TABLE for both tables

Static analysis:

7 errors were found during analysis.

  1. Unexpected beginning of statement. (near “vid” at position 0)
  2. Unexpected beginning of statement. (near “sid” at position 7)
  3. Unexpected beginning of statement. (near “title” at position 14)
  4. Unexpected beginning of statement. (near “titleimage” at position 23)
  5. Unexpected beginning of statement. (near “description” at position 37)
  6. Unexpected beginning of statement. (near “link” at position 52)
  7. Unexpected beginning of statement. (near “datetime” at position 60)

SQL query:

vid, sid, title, titleimage, description, link, datetimeSELECT * FROM video WHERE 1

MySQL said:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'vid, sid, title, titleimage, description, link, datetimeSELECT * F' at line 1

# Name Type Collation Attributes Null Default Comments Extra
1 vid int(11) No None AUTO_INCREMENT
2 sid int(11) No None
3 title varchar(50) latin1_swedish_ci No None
4 titleimage varchar(50) latin1_swedish_ci No None
5 description varchar(500) latin1_swedish_ci No None
6 link varchar(50) latin1_swedish_ci No None
7 datetime timestamp No None

Indexes

Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No vid 0 A No
sid BTREE No No sid 0 A No

Partitions

No partitioning defined!

Information

Space usage|Data|16|KiB|
| — | — | — |
|Index|16|KiB|
|Total|32|KiB|

Row statistics|Format|dynamic|
| — | — |
|Collation|latin1_swedish_ci|
|Next autoindex|16|
|Creation|Aug 31, 2018 at 11:55 AM|

Why don’t you stick to the topic and post random error messages unrelated to the main problem? It’s pretty clear you can’t start a query with a list of columns, a simple select statement is the most basic usage of SQL you should know. And you are still hiding the insert/update query you are actually sending to the database which causes the constraint error.

do you need this

INSERT INTO video(vid, sid, title, titleimage, description, link, datetime) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7])

UPDATE video SET vid=[value-1],sid=[value-2],title=[value-3],titleimage=[value-4],description=[value-5],link=[value-6],datetime=[value-7] WHERE 1

look, i’m going to make it real easy for you

1/ go to phpmyadmin’s SQL tab

2/ in the window where you normally run a query, type this –

SHOW CREATE TABLE table1

where table1 is the name of your first table

copy the result (all of it, not the first few characters) and paste it here

3/ repeat for second table

this will show us the relationship (if any) between the tables, which is what we’re trying to help you with regarding the foreign key error you first mentioned way back in post #1

getting this

Error

SQL query:

SHOW CREATE TABLE table1

MySQL said:

#1146 - Table 'news.table1' doesn't exist