简体   繁体   中英

Data could not be inserted into mysql table with foreign key

I'm trying to insert data into a child table with foreign key. Data will not be inserted and no errors displayed. Once i delete the column with the foreign key in the child table, the query will be executed and data inserted.

I have tried to debug this, using error_reporting E_ALL and all i get is 2257, which i don't understand what that means.

HERE IS THE CODE FOR THE INSERT QUERY

if(isset($_POST['submenu'])){
    $page = mysqli_real_escape_string($con, strtolower($_POST['page']));
    $parents_name = mysqli_real_escape_string($con, strtolower($_POST['menu_id']));

   $UniqueMenuID = ''; 
    if( isset( $_POST['menus_id'])) {
        $UniqueMenuID = $_POST['menus_id']; 
    }      

   $sub_query = '';

   if(empty($page) AND ($parents_name)){
       $sub_error = "<span class='animated flash'>Select a Page to Add to Menu!</span>";
   } 
   else if(empty($page)){
       $sub_error = "<span class='animated flash'>Select a Page to Add to Menu!</span>";
   }
    else if(empty($parents_name)){
       $sub_error = "<span class='animated flash'>Select a Parent to Add Sub Menu!</span>";
   }
    else{
         $check_query = "SELECT * FROM menu_items WHERE title = '$page'";
         $check_run = mysqli_query($con, $check_query);
         if(mysqli_num_rows($check_run) > 0){
             $sub_error = "<span class='animated flash'>Sub Menu Already Exist!</span>";
         }
         else{
             $sub_query = "INSERT INTO `sub_menu`(`sub_id`, `sub_title`, `parent`, `menus_id`) VALUES ('NULL', '$page', '$parents_name', '$UniqueMenuID')";

             if(mysqli_query($con, $sub_query)){
                 $sub_msg = "Sub Menu Has Been Added";
             }
             else{
                $sub_error = "Sub Menu Has Not Been Added";
             }
        }
    }

}

AND THE DATABASE FOR THE PARENT 

CREATE TABLE `menu_items` (
  `id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `page` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `menu_items`
--

INSERT INTO `menu_items` (`id`, `title`, `page`) VALUES
(52, 'promotions', 'promotions'),
(58, 'products', 'products');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `menu_items`
--
ALTER TABLE `menu_items`
  ADD PRIMARY KEY (`id`),
  ADD KEY `id` (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `menu_items`
--
ALTER TABLE `menu_items`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=59;
COMMIT;

AND THE DATABASE FOR THE CHILD TABLE

CREATE TABLE `sub_menu` (
  `sub_id` int(11) NOT NULL,
  `sub_title` varchar(255) NOT NULL,
  `parent` varchar(255) NOT NULL,
  `menus_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `sub_menu`
--
ALTER TABLE `sub_menu`
  ADD PRIMARY KEY (`sub_id`),
  ADD KEY `menus_id` (`menus_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `sub_menu`
--
ALTER TABLE `sub_menu`
  MODIFY `sub_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=31;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `sub_menu`
--
ALTER TABLE `sub_menu`
  ADD CONSTRAINT `Foreign key to menu id` FOREIGN KEY (`menus_id`) REFERENCES `menu_items` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;

Need to get the data inserted into the table with the foreign key intact.

You check for empty $page but no $parents_name:

if(empty($page) AND ($parents_name)) {

So when the fourth else condition starts?

if(empty($page) AND ($parents_name)) { 

} else if(empty($page)) { 

} else if(empty($parents_name)) {

} else {
    // When this condition starts?
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM