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