title: I am trying to save depart_id in the table student and it is a foreign key in another table but unfortunately it is giving me an error in php:
body: <p>I am trying to save <code>depart_id</code> in the table <code>student</code> and it is a foreign key in another table but unfortunately, it is giving me an error in PHP:</p>
  <blockquote>
  <p>Can not update or insert: foreign key constraint</p>
  </blockquote>
  <p>Code:</p>
  <pre><code>$sid_exist='';
  </code></pre>
  <p>if (isset($_POST['submit'])) {</p>
  <pre><code>$studid = $_POST['studid'];
  $stud_name = $_POST['stud_name'];
  $telno = $_POST['telno'];
  $email = $_POST['email'];
  $program=$_POST['program'];
  $location = $_POST['location'];
  $gender = $_POST['gender'];
  $departid=$_POST['departid'];
  $sql_exist=mysqli_query($conn, &quot;select * from student where studid='$studid'&quot;);
  
  if ($sql_exist) {
    
      header(&quot;Location:addStudent.php&quot;);
      $sid_exist=$studid.&quot; &quot;.&quot;Exists&quot;;
    
  }else{
  
      $sql = &quot;INSERT INTO `student`(`studid`, `stud_name`, `telno`,`email`, `program`,`location`, `gender`,`depart_id`) VALUES ('$studid',' $stud_name ','$telno','$email','$program','$location','$gender','$departid')&quot;;
      $result = $conn-&gt;query($sql);
  
      if ($result == TRUE) {
      
          header(&quot;Location:index.php&quot;);
          echo &quot;New record created successfully.&quot;;
          
      } else {
  
          echo &quot;Error:&quot;. $sql . &quot;&lt;br&gt;&quot;. $conn-&gt;error;
  
      } 
  }
  </code></pre>