创建表错误并使用codeigniter创建多个表

时间:2017-05-16 19:11:52

标签: php codeigniter mariadb

我一直想将数据插入到现有表格中并在有人注册时自动创建表格,但我遇到了一些问题。

这是我的form_reg

<div class="row">   
        <div class="col-md-12">
            <form enctype="multipart/form-data" method="POST" action="<?php echo base_url()?>index.php/tr/reg">
                <table style="width:110%" class="table table-striped table table-bordered table table-hover">
                    <tr>
                        <td>Nama Lengkap</td>
                        <td><input class="form-control" type="text" name="nama"/></td>
                    </tr>
                    <tr>
                        <td>Username</td>
                        <td><input class="form-control" type="text" name="username"/></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input class="form-control" type="password" name="password"/></td>
                    </tr>
                    <tr>
                        <td>Nomor TBS</td>
                        <td><input class="form-control" type="number" name="no_tbs"/></td>
                    </tr>
                    <tr>
                        <td>Program</td>
                        <td><input class="form-control" type="text" name="program"/></td>
                    </tr>

                    <tr>
                        <td></td>
                        <td><button class="btn btn-info" type="submit" name="btnsubmit" value="Simpan">Register</button>

                    </tr>
                </table>
            </form>
        </div>
    </div>

这个我的控制器命名为tr.php

public function reg(){ 
    $posts_fields=array(
    'id'=>array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto increment' => TRUE,'primary key' => TRUE),
    'tanggal'=>array('type' =>'DATE','constraint' => 100),
    'tbs_terhadap'=>array('type'=>'number','constraint' => 100),
    'keterangan'=>array('type'=>'text','constraint' => 100),
    'masuk'=>array('type'=>'number','constraint' => 100),
    'keluar'=>array('type'=>'number','constraint' => 100),
    'saldo_dsp'=>array('type'=>'number','constraint' => 100),
    'tbs_id'=>array('type'=>'INT','constraint'=>11));

for($i = 1; $i <= 100000; $i++) {
   $formattedNumber = sprintf('%05d', $i);
   echo "<tr><td>$formattedNumber</td></tr>";


$this->dbforge->add_field($posts_fields);
$this->dbforge->create_table('dsp');


    $nama = $_POST['nama'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $no_tbs = $_POST['no_tbs'];
    $program = $_POST['program'];

        $data_insert = array(
            'nama' => $nama,
            'username' => $username,
            'password' => $password,
            'no_tbs' => $no_tbs,
            'program' => $program,

        );

        $res = $this->mymodel->RegData('tbs_member', $data_insert);


    if($res >= 1 && $result!==false){
        $this->session->set_flashdata('pesan','Register Success');
        header("location: /tbs/index.php/web?gagal=0&sukses=1");
    }else{
        header("location: /tbs/index.php/web/register?gagal=1");
    }
}

这是mymodel.php

public function RegData($tableName, $data)
{
    $res = $this->db->insert($tableName, $data);
    return $res;
}

更新:使用$this->load->dbforge()后,问题发生了变化,这是我运行后出现的问题

  

发生数据库错误

     

错误号码:1064

     

您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,以便在第3行使用'(100)NULL,tbs_terhadap VARCHAR(100)NULL,keterangan VARCHAR(100)NU'附近使用正确的语法

     

CREATE TABLE dsp$formattedNumberid INT(11)UNSIGNED NOT NULL AUTO_INCREMENT,tanggal DATE(100)NULL,tbs_terhadap VARCHAR(100)NULL,{{1 VARCHAR(100)NULL,keterangan VARCHAR(100)NULL,masuk VARCHAR(100)NULL,keluar VARCHAR(100)NULL,saldo_dsp INT(11) NOT NULL,CONSTRAINT tbs_id PRIMARY KEY(pk_dsp$formattedNumber))DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci

     

文件名:C:/xampp/htdocs/genstore/tbs/system/database/DB_driver.php

     

行号:691

如果可以一次创建多个表,我还需要信息吗?如果是,请告诉我如何

这就是我需要问的一切..告诉我是否有任何我遗漏的信息..我有点新的请告诉我,我的错误是什么?感谢

1 个答案:

答案 0 :(得分:0)

您确定可以设置DATE(100)字段吗?在我看来,您的查询因此失败(您不需要为DATE字段指定长度)

<div id="container">
  <div id="left">
    <img src="Image 1">
    <br>
    <img src="Image 2">
  </div>
  <div id="right">
    <div id="top-right">
      <img src="Image 3">
      <img src="Image 4">
    </div>
    <div id="bottom-right">
      <img src="Image 5">
      <img src="Image 6">
    </div>
  </div>
</div>