我正在使用 codeigniter 的 MVC 构建一个表单,登录用户可以在其中发布问题。
我使用的表单允许登录用户在名为 Questions 的 mysql 表中插入数据(表结构如下所示)。
问题表
______________________________________________
| id | User id | description | category |
|---------------------------------------------|
|1 | 22 | Car parts | 12 |
|---------------------------------------------|
|2 | 54 | Car parts | 8 |
|---------------------------------------------|
|3 | 112 | Car parts | 11 |
|_____________________________________________|
用户提交表单后,用户的 id 和 Category_id(来自“用户表”下方的表)将插入到上面的问题表
用户表
___________________________________
| id | User id |category_id |
|-------------------------------
|1 | 22 | 12 |
|------------------------------ |
|2 | 54 | 54 |
|---------------------------- |
|3 | 112 | 8 |
|_________________________________|
我的 Controller 页面代码
$this->load->model('questions_page');
$data['page_detail'] = $this->questions_page->get_business_page_by_pid($page_id);
if ($this->form_validation->run() == false || $form_validation == false)
{
$data['form_value'] = array(
'country' => $this->input->post('description'),
'street_address' => $this->input->post('street_address'),
'post_code' => $this->input->post('post_code'),
);
$this->load->view('template', $data);
}
这是我的查看页面
<input type="text" name="post_code" value="<?= $form_value['description'] ?>">
<?php echo form_error("description"); ?>
<input type="text" name="post_code" value="<?= $form_value['street_address'] ?>">
<?php echo form_error("street_address"); ?>
<?php // I gathered some info from the users table below which needs to be inserted into the questions table
////////////////////////////
///////////////////////////
$page_detail->category_id;
////////////////////////////
///////////////////////////
?>
我的问题 我在 View 页面的最后一行 ( $page_detail->category_id; ) 中有数据,我想将其传输到 Controller 页面,以便我可以使用 $page_detail->category_id; 将其插入到 mysql 表中>$data['form_value'] = array(); 如 Controller 页面所示。
我该怎么做?
将数据从 View 传输到 Controller 安全吗?
提前致谢
最佳答案
第一:您将用户ID存储在问题表中,因此两个表之间创建了关系,以便您可以加入 两个带有用户ID的表,以便您可以从用户表中获取类别ID。所以不需要在问题表中存储类别id。这是不必要的。如果你存储 .这称为数据冗余。
第二:猜测用户 ID 将从 session 中获取。
第三:仍然希望将category_id从 View 传递到 Controller 意味着只需使用一些隐藏输入
<input type='hidden' name="category_id" value="<?php echo $page_detail->category_id; ?>" />
在 Controller 中:
$data['form_value'] = array(
'country' => $this->input->post('description'),
'street_address' => $this->input->post('street_address'),
'post_code' => $this->input->post('post_code'),
'category_id' => $this->input->post('category_id'),
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
);
关于php - 在codeigniter中将变量从View页面传输到Controller页面是否理想?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45753926/