title: Error in Jframe: Cannot add or update a child row: a foreign key constraint fails
body: <p>I am trying to put multiple rows in <code>productorder</code> table with the same foreign key which is the <code>order_id</code> from <code>orders</code> table but this error is keep showing: </p>
  
  <blockquote>
    <p>com.mysql.jdbc.exceptions.jdbc4.MYSQLIntegrityConstraintViolationException
    Cannot add or update a child row: a foreign key constraint fails
    ('store', 'productorder', CONSTRAINT 'productorder_ibfk_1', FOREIGN
    KEY ('order_id'), REFERENCES 'orders' ('order_id'))</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <pre><code> private void SUBMIT1ActionPerformed(java.awt.event.ActionEvent evt) {
    try{
        Class.forName("com.mysql.jdbc.Driver");
          Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/homemade","root", "");
  
          String sql3 = "insert into productorder(order_id, prod_id, quantity, subtotal_price) values (LAST_INSERT_ID(), ?, ?, ?)";
          String sql4 = "SELECT MAX(order_id) FROM orders ";
          PreparedStatement pst3 = conn.prepareStatement(sql3);
          PreparedStatement pst4 = conn.prepareStatement(sql4);
          String sql5 = "insert into productorder(order_id, prod_id, quantity, subtotal_price) values (LAST_INSERT_ID(), ?, ?, ?)";
          String sql6 = "SELECT MAX(order_id) FROM orders ";
          PreparedStatement pst5 = conn.prepareStatement(sql5);
          PreparedStatement pst6 = conn.prepareStatement(sql6);
          String sql7 = "insert into productorder(order_id, prod_id, quantity, subtotal_price) values (LAST_INSERT_ID(), ?, ?, ?)";
          String sql8 = "SELECT MAX(order_id) FROM orders ";
          PreparedStatement pst7 = conn.prepareStatement(sql7);
          PreparedStatement pst8 = conn.prepareStatement(sql8);
          String sql9 = "insert into productorder(order_id, prod_id, quantity, subtotal_price) values (LAST_INSERT_ID(), ?, ?, ?)";
          String sql10 = "SELECT MAX(order_id) FROM orders ";
          PreparedStatement pst9 = conn.prepareStatement(sql9);
          PreparedStatement pst10 = conn.prepareStatement(sql10);
          String sql11 = "insert into productorder(order_id, prod_id, quantity, subtotal_price) values (LAST_INSERT_ID(), ?, ?, ?)";
          String sql12 = "SELECT MAX(order_id) FROM orders ";
          PreparedStatement pst11 = conn.prepareStatement(sql11);
          PreparedStatement pst12 = conn.prepareStatement(sql12);
          String sql13 = "insert into productorder(order_id, prod_id, quantity, subtotal_price) values (LAST_INSERT_ID(), ?, ?, ?)";
          String sql14 = "SELECT MAX(order_id) FROM orders ";
          PreparedStatement pst13 = conn.prepareStatement(sql13);
          PreparedStatement pst14 = conn.prepareStatement(sql14); 
  
      if(shanghai1.isSelected() == true){
         pst3.setString(1, "4");
         pst3.setString(2, shangqua.getText());
         pst3.setString(3, subShang1.getText());
         pst3.executeUpdate();
         pst4.executeQuery();
     }
  
  
  
  
      if(siomai1.isSelected() == true){
         pst5.setString(1, "5"); 
         pst5.setString(2, sioqua.getText());
         pst5.setString(3, subSiomai1.getText());
         pst5.executeUpdate();
         pst6.executeQuery();
      }
  
  
  
      if(burger1.isSelected() == true){
         pst7.setString(1, "1"); 
         pst7.setString(2, burqua.getText());
         pst7.setString(3, subBurger1.getText());
         pst7.executeUpdate();
         pst8.executeQuery(); 
  
      }
  
  
  
      if(embotido1.isSelected() == true){
         pst9.setString(1, "2");  
         pst9.setString(2, embqua.getText());
         pst9.setString(3, subEmbotido1.getText());
         pst9.executeUpdate();
         pst10.executeQuery();
      }
  
  
      if(skinless1.isSelected() == true){
         pst11.setString(1, "6"); 
         pst11.setString(2, skiqua.getText());
         pst11.setString(3, subSkinless1.getText());
         pst11.executeUpdate();
         pst12.executeQuery();
      }
  
  
      if(nuggets1.isSelected() == true){
          pst13.setString(1, "3"); 
          pst13.setString(2, nugqua.getText());
          pst13.setString(3, subNuggets1.getText());
          pst13.executeUpdate();
          pst14.executeQuery();
      }
  
  
      JOptionPane.showMessageDialog(null, "Record updated successfully!");
  
  
  
      }
     catch(Exception e){
         JOptionPane.showMessageDialog(null, e);
     }
  </code></pre>
  
  <p>and here is the table diagram: </p>
  
  <p><a href="https://imgur.com/015drgX" rel="nofollow noreferrer">Table diagram image</a></p>