phpcodeigniterdbforge

Codeigniter DBForge add_column returns only error


I'm trying to add a new column to the database with dbforge of Codeigniter but it returns only error messages doesn't get into else column My Error : Duplicate column name 'github' ALTER TABLE social-settings ADD github VARCHAR(255) I know there is a column name github but it doesn't get into else column, it inserts two tables in database if column adding fails, I need to delete from other tables

$last_id=$this->Forms_model->newArea($this->input->post());
        if ($last_id!=false) {
            $this->load->dbforge();
            $fields = array(
                $this->input->post("element_name") => array(
                        'type' => 'VARCHAR',
                        'constraint' => '255',
                ),
            );
            $table = $this->input->post("element_form_id") == 2 ? "contact-settings" : "social-settings";
            if ($this->dbforge->add_column($table, $fields)) {
                echo json_encode(returnJson(1, "Yeni form alanı ekleme işlemi başarılı."));
            } else {
                echo $this->Forms_model->turnToOld($last_id) == true ? json_encode(returnJson(0, "Yeni form alanı ekleme işlemi bir sebepten dolayı başarısız oldu.")) : json_encode(returnJson(0, "İşlemler yapılırken bazı hatalar oluştu, lütfen geliştiricinizle iletişime geçin."));
            }
        }

Solution

  • There must be a column with that name that already exists in the table.

    You should use $this->db->field_exists() to check if the column exists before issuing the add_column statement.

    $el = $this->input->post("element_name");
    
    if (is_null($el)) {
        show_error('element is null');
    }
    
    if (!$this->db->field_exists($el, 'table_name')) {
       // dbforge
    }
    

    https://www.codeigniter.com/user_guide/database/metadata.html#determine-if-a-field-is-present-in-a-table