2

我是 codeigniter 的新手,我尝试过表单验证。

这是我的查看代码:

<?php echo validation_errors(); ?>
          <?php echo form_open('enquiry'); ?>
          <table width="580" border="0" cellspacing="0" cellpadding="5">

            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Full Name:<br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="name" value="<?php echo set_value('name'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Company: <span id="mndCompany"> </span><br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="company" value="<?php echo set_value('company'); ?>"  class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *E-mail:</td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="email" value="<?php echo set_value('email'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Phone: <br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="phone" value="<?php echo set_value('phone'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td height="30" align="right" valign="middle" class="normal-text-style"> *Fax: <br /></td>
              <td height="30" align="left" valign="middle" class="normal-style"><input type="text" name="fax" value="<?php echo set_value('comments'); ?>" class="textbox-style01" /></td>
            </tr>
            <tr>
              <td width="179" height="30" rowspan="2" align="right" valign="top" class="normal-text-style"> *Comments: <br /></td>
              <td width="421" height="12" align="left" valign="middle" class="normal-style"><textarea  name="comments" class="msgebox-style01"><?php echo set_value('comments'); ?></textarea></td>
            </tr>
            <tr>
              <td height="50" align="left" valign="top" class="normal-style"><input type="image" name="submit" src=" <?php echo base_url(); ?>/public/images/submit01.png" alt="submit" /></td>
            </tr>
          </table>
          </form>

这是我的控制器

function index()
    {
        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('company', 'Company', 'trim|required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('phone', 'phone', 'trim|required|xss_clean');
        $this->form_validation->set_rules('fax', 'fax', 'trim|required|xss_clean');
        $this->form_validation->set_rules('comments', 'comments', 'trim|required|xss_clean');


        $data['header_menu']=$this->load->view('header_menu', '', TRUE);
        $this->load->view('inner_header',$data);
        $data['left_menu']=$this->load->view('left_menu', '', TRUE);

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('enquiry',$data);
        }

        else
        {
           $this->load->view('enquiry/formsucess',$data);   
        }    
        $this->load->view('footer');
        //$this->load->view('enquiry',$data);


    }
    function formsucess()
    {


        $name = $this->input->post('name');
        $company = $this->input->post('company');
        $email = $this->input->post('email');
        $phone = $this->input->post('phone');
        $fax = $this->input->post('fax');
        $comments = $this->input->post('comments');
        $this->db->query("INSERT INTO `st_enquiry` 
        (name, company,email,phone,fax,comments,status,created_on)
        VALUES
        ('$name','$company','$email','$phone','$fax','$comments','1',now())");

        $data['header_menu']=$this->load->view('header_menu', '', TRUE);
        $this->load->view('inner_header',$data);
        $data['left_menu']=$this->load->view('left_menu', '', TRUE);
        $this->load->view('media',$data);
        $this->load->view('footer');


    }

提交表单后,我收到如下错误:

A Database Error Occurred

Error Number: 1146

Table 'table.users' doesn't exist
4

4 回答 4

3

签出此链接

这里

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

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

必须在任何条件下工作。

于 2012-06-06T09:25:36.597 回答
2
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[users.email]');  

这里会有一些变化
is_unique[users.email]
01.这里 users 表示您的表名 02.email 表示您表
中的电子邮件列。

所以它会是

$this->form_validation->set_rules('email','Email','required|valid_email|
is_unique[table name.email column in your table]');
于 2014-04-15T18:51:37.673 回答
0

发生数据库错误

错误号:1146

表“table.users”不存在

我通过更改 phpmyadmin 中的表名解决了 Codeigniter 中的这个问题,希望这可以帮助你

于 2017-12-16T12:48:48.943 回答
-2

在模型中创建一个函数,如:

$this->db->insert($this->table_name,$db_data);
return $this->db->insert_id();

并将数据从控制器发送到模型

于 2016-05-19T06:55:25.667 回答