【问题标题】:CodeIgniter -> Submitting a form into databaseCodeIgniter -> 提交表单到数据库
【发布时间】:2019-03-07 15:50:13
【问题描述】:

创建我自己的应用程序来学习 CodeIgniter,通过遵循 CI 文档,我无法将我的数据发布到数据库中。

我正在使用 create.php:

<p class="h4 mb-4 text-center"><?= $title ?></p>

<label>Username</label>
<input type="text" id="textInput" class="form-control mb-4" placeholder="Your username" name="username">

<label for="textInput">Title</label>
<input class="form-control mb-4" placeholder="Your title" name="title">

<label>Your message</label>
<textarea class="form-control mb-4" placeholder="Your message content" name="body"></textarea>
<div class="row">
    <div class="col-8">
    <select class="browser-default custom-select mb-4" id="select" name="genre">
        <option selected="">Choose your Genre</option>
        <option value="1">Male</option>
        <option value="2">Female</option>
    </select>
    </div>
    <div class="col-2">
        <input class="form-control mb-4" placeholder="Your age" name="age">
    </div>
</div>
<select class="browser-default custom-select mb-4" name="platform">
    <option value="" disabled="" selected="">Choose your Platform</option>
    <option value="1">Snapchat</option>
    <option value="2">Kik</option>
    <option value="3">Instagram</option>
    <option value="4">Telegram</option>
</select>

<button class="btn btn-info my-4 btn-block" type="submit">Sign up</button>

<div class="text-center">
    <p>By clicking
        <em>Sign up</em> you agree to our
        <a href="<?php echo base_url('tou'); ?>" target="_blank">terms of use</a>.
    </p>
</div>

这是控制器:

public function create(){
        $data['title'] = 'Add your Username';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('body', 'Body', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('genre', 'Genre', 'required');
        $this->form_validation->set_rules('platform', 'Platform', 'required');
        $this->form_validation->set_rules('age', 'Age', 'required');

        if($this->form_validation->run() === FALSE){
            $this->load->view('templates/header');
            $this->load->view('posts/create', $data);
            $this->load->view('templates/footer');
        } else {
            $this->post_model->create_post();
            redirect('posts');
        }
    }

这是模型:

public function create_post(){
        $slug = url_title($this->input->post('title'));

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body'),
            'username' => $this->input->post('username'),
            'genre' => $this->input->post('genre'),
            'age' => $this->input->post('age'),
            'platform' => $this->input->post('platform'),
        );
        return $this->db->insert('posts', $data);
    }

我还添加了自动加载 (libraries->form_validation) 和 (helper->form)。 我已经在这些代码之前测试了所有内容,并且运行良好。问题是 HTML 中的那些数据我想将它们发送到数据库中,但即使验证器也适用于空白表单(提交没有内容)或插入内容,它只是重新加载页面,因为它应该将其重定向到帖子 url。 有人可以帮助我吗?

【问题讨论】:

  • create() 的开头显示var_dump($_POST); die; 的结果!
  • @SherifSalah C:\wamp64\www\application\views\posts\create.php:1: 数组 (size=0) 为空
  • 不确定这是否是原因,但您的视图中没有 &lt;form&gt; 标记,因此您的表单实际上什么都不做。您需要让浏览器知道您希望将表单数据发布到哪里。由于您正在自动加载表单助手,而不是编写表单标签,您可以在表单之前 &lt;?php echo form_open('yourcontroller/create'); ?&gt; 并在提交按钮之后 &lt;?php echo form_close(); ?&gt; 并让 CI 为您创建必要的 HTML 元素

标签: php html database forms codeigniter


【解决方案1】:

你的 post 方法在哪里?

例如:

<form method="post" name="from_submit" action="">

【讨论】:

    【解决方案2】:

    你忘了添加表格

     <form method="post" name="myform" action="">
    //Your form code---
    
    </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2013-06-03
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2013-06-03
      相关资源
      最近更新 更多