在CI 3中插入数据库不起作用

时间:2015-10-02 03:43:58

标签: php codeigniter-3

H,我想问一下,为什么我的代码不起作用。我一直在谷歌搜索,但没有找到答案

public function insertComment($commentArray=array()) {
    try {
        $this->db->trans_start();

        $name = $this->db->escape($commentArray['name']);
        $email = $this->db->escape($commentArray['email']);
        $message = $this->db->escape($commentArray['message']);
        $blogID = $this->db->escape($commentArray['blogID']);

        $sql = "insert into comment(email,comment,createdon,status,blogID,name) "
. " values($email,$message,now(),0,$blogID,$name)";

        error_log($sql);
        $this->db->query($sql);

        if ($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        } else {
        $this->db->trans_commit();
        }

    } catch (Exception $ex) {

  }
}

但如果我使用本机方式插入数据库,它可以正常工作

public function insertComment($commentArray=array()) {
        try {

            mysql_connect('localhost', 'root', '');
            mysql_select_db('yungfei');

             $sql = "insert into client(client_name) values('xxxx')";

             mysql_query($sql);
             error_log($sql);

} catch (Exception $ex) {

    }
    }

我的数据库配置:

$active_group = 'default';
$query_builder = TRUE;

$db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';

    $db['default'] = array(
            'dsn'   => 'mysql:host=localhost;dbname=yungfei',
            'username' => 'root',
        'password' => '',
        'dbdriver' => 'pdo',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );

我想念的是什么?我使用Code Igniter 3.请帮助我,谢谢

2 个答案:

答案 0 :(得分:1)

$data = array(
        'title' => 'My title',
        'name'  => 'My Name',
        'date'  => 'My date'
);

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

这只是将数据插入db的简单结构,请尝试

答案 1 :(得分:0)

试试这个:

final CharSequence[] items = {"X", "O"};

    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setCancelable(false);
    alertDialog.setTitle("Who goes first?");
    alertDialog.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item] == "X") {
                First = 1;
                Toast.makeText(getApplication(), "Computer goes first.", Toast.LENGTH_SHORT).show();
            } else if (items[item] == "O") {
                First = 2;
                if (First == 2) {
                    Toast.makeText(getApplication(), "2 WORKS", Toast.LENGTH_SHORT).show();
                }
            }
            dialog.dismiss();
        }
    });
    alertDialog.show();

    if (First == 1){
        Toast.makeText(getApplication(), "Inside If", Toast.LENGTH_SHORT).show();
    }
    Toast.makeText(getApplication(), "After If Message", Toast.LENGTH_SHORT).show();

来自CodeIgniter手册页的引用:“确保在运行手动交易时使用$ this-> db-> trans_begin(),而不是$ this-> db-> trans_start()。”