简体   繁体   中英

How to add data in database in codeigniter

There are two tables.

  1. category(idcategory,name,url)
  2. subcategory(id,name,url,idcategory)

In codeigniter I have the controller named submenu and view is vwsubmenu. In add function I have to add the name, URL and the idcategory into the subcategory table.But in view page there is three field like one is input type text(name),input type text(URL),select type that is name of category from category table.

Now I want to take the id of category table from the name (which is the input of select type)and insert into subcategory table in idcategory field. How to do that in codeigniter.

Please Help

You can try

$this->db->insert_id();

This will return the last id from category table which then you can insert into sub category table.

You can get more info from here

first improve your database structure.

CREATE TABLE IF NOT EXISTS `category` (
  `idcategory` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `url` varchar(255) NOT NULL,
  PRIMARY KEY (`idcategory`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

Try this ...

http://forum.codeigniter.com/archive/index.php?thread-493.html

 public function add_submenu()
 {

        if(isset($_REQUEST['submit']))
        {

        $namesubcategory=$_POST['namesubcategory'];
        $urlsubcategory=$_POST['urlsubcategory'];
        $parent_menu=$_POST['parent_menu'];
        $a=$parent_menu;
        $query = $this->db->query('Select idcategory from category where namecategory="$a"');
        $row = $query->row();
        $b=$row->idcategory;


        $sql="insert into subcategory values('','$namesubcategory','$urlsubcategory','$b')";
        $val = $this->db->query($sql,array($namesubcategory ,$urlsubcategory,$b));
        redirect('admin/submenu/');
        }

        $arr['page'] = 'submenu';

        $this->load->view('admin/vwAddSubmenu',$arr);
    }

this is the add function in submenu controler

At first you have to send data to model form controller

$result= $this->model_name->model_function($data);

Then you can specify this in model:

public function functionName( $data){
    $this->db->insert('table_name',$data);
}

load 3 input fields in subcategory view

  1. name
  2. URL
  3. idcategory (this should be drop-down of the category table)

then add data to data base using provided settings.

Code

before load subcategory view get category table data in controller

$data['category'] = $this->Model_name->get_category();
$this->load->view('sub_category_view',$data);

In model

public function get_category()
{
    $query = $this->db->query->("SELECT * FROM category");
    $result = $query->result_array();
    return $result;
}

In subcategory view

<form method="post" action="<?php echo base_url() ?>index.php/controller/add_subcategory">
    <input type="text" name="name" value="Sub Category Name">
    <input type="url" name="url" value="Sub Category URL">
    <select name="category">
        <?php
            foreach ($category as $item)
            {
                ?>
                <option id="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
            <?php
            }
        ?>

    </select>
    <input type="submit" name="submit" value="Add Sub Category">

</form>

//adding sub category to database

In Controller

public function add_subcategory()
{

    if(isset($_POST['submit']))
    {
        $name = mysql_real_escape_string($_POST['name']);//if `mysqli` change this mysql_real_escape_string
        $url = $_POST['url'];
        $cat = $_POST['category'];


        $result = $this->Model_name->insert_subcategory($name,$url,$cat);

        if(isset($result))
        {
            echo 'insert successfully';

        }
        else
        {
            echo 'insert Failed';
        }

    }

}

In Model

function insert_subcategory($name,$url,$cat)
{
    $data = array(
        'name' => $name,
        'url' => $url ,
        'idcategory' => $cat
    );

    $this->db->insert('subcategory', $data);
}

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