无法使用外键将数据插入mysql表

时间:2019-02-14 19:45:28

标签: php mysqli

我正在尝试使用外键将数据插入子表中。不会插入数据,也不会显示错误。一旦删除子表中具有外键的列,将执行查询并插入数据。

我尝试使用error_reporting E_ALL调试它,我得到的只是2257,我不明白那是什么意思。

这里是插入查询的代码

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;

和儿童表的数据库

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;

需要将数据完整插入外表中。

1 个答案:

答案 0 :(得分:0)

您检查的是空白的$ page,但没有$ parents_name:

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

那么当第四个条件开始时?

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

} else if(empty($page)) { 

} else if(empty($parents_name)) {

} else {
    // When this condition starts?
}