• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting data to save with foreign key

#1
I'm trying to save a data that has a foreign key.

On my page I have a drop down and a textbox, as it stands at the moment there is nothing in my database.
When I try to save a menu item I get this error
Quote:Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`webkrunch_ci`.`webkrunc_menus`, CONSTRAINT `webkrunc_menus_parent` FOREIGN KEY (`parent`) REFERENCES `webkrunc_menus` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

INSERT INTO `webkrunc_menus` (`title`) VALUES ('Home')

Filename: /Applications/MAMP/htdocs/webkrunch-codeigniter/modules/menus/models/mdl_menus.php

Line Number: 44

What I would like is for it to save a 0 in the parent section unless I've specified a menu in the drop down list.

My Menus controller
Code:
function create()
    {
        $update_id = $this->uri->segment(3);
        $submit = $this->input->post('submit', TRUE);

        if($submit == "Submit"){
            //person has submitted the form
            $data = $this->get_data_from_post();
        }else{
            if(is_numeric($update_id)){
                $data = $this->get_data_from_db($update_id);
            }
        }

        if(!isset($data)){
            $data = $this->get_data_from_post();
        }


        $titles = array();

        $query = $this->get('title');
        foreach($query->result() as $row){
            $titles[] = $row->title;
        }

        $data['titles'] = $titles;

        $data['update_id'] = $update_id;

        $data['view_file'] = "create";
        $this->load->module('templates');
        $this->templates->admin_template($data);
    }

My create.php view
Code:
<div class="row">
    <div class="col-md-12">
        <h2>Create Menus</h2>  
        <h5>Welcome Jhon Deo , Need to make dynamic. </h5>
    </div>
</div>

<hr />

<?php
    echo validation_errors("<p style='color: red;'>", "</p>");
    echo form_open('menus/submit/'.$update_id);
?>
<div class="row">
    <div class="col-md-12">
        <form role="form">
        <div class="form-group">
        <select name="menus">
            <?php
                foreach($titles as $title){
                    echo $title;
                    echo "<option value=".$title.">".$title."</option>";
                }
            ?>
            </select>
            
            
        </div>

            <div class="form-group">
                <label>Title</label>
                <!-- <input class="form-control" /> -->
                <?php
                    $data = array(
                                'name' => 'title',
                                'id' => 'title',
                                'value' => $title,
                                'class' => 'form-control',
                            );

                    echo form_input($data);
                ?>
            </div>

            <?php
                
                $data = array(
                    'name' => 'submit',
                    'id' => 'submit',
                    'value' => 'Submit',
                    'class' => 'btn btn-success',
                    'style' => 'width: 100%',
                );

                echo form_submit($data);
            ?>
        </form>
    </div>
</div>
<?php    
    echo form_close();
?>

If you need more information or if something doesn't make sense please let me know
Reply

#2
A parent-child relation in the same table must be NULL-able.
So, set your parent column to allow NULL.

Or if you wish to keep it at NOT NULL, you have to create a menu item with ID 0, that will never be shown in the web, and set your parent column to default value 0. Although the first method would be prefered I suppose.

Edit: why nullable? because the top level item can not have a parent.
Reply

#3
Yep that was it. Thank you
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.