在codeigniter中将form_dropdown数据插入mysql数据库
Posted
技术标签:
【中文标题】在codeigniter中将form_dropdown数据插入mysql数据库【英文标题】:insert form_dropdown data into mysql database in codeigniter 【发布时间】:2017-06-16 03:12:30 【问题描述】:我是 codeigniter 的新手,正在尝试将 form_dropdown 数据插入数据库,但我无法将所选选项插入数据库。
这是我的模型:
public function insertUser()
$data1 = array(
'id'=>$this->input->post('id'),
'full_name'=>$this->input->post('full_name'),
'email'=>$this->input->post('email'),
'contact_no'=>$this->input->post('contact_no'),
'role'=>$this->roles[0],
'status'=>$this->status[0]
);
$this->db->insert('users',$data1);
$data2['user_id']=$this->db->insert_id();
$data2 = array(
'type_of_paper'=>$this->input->post('type_of_paper'),
'deadline'=>$this->input->post('deadline'),
'acadmic_level'=>$this->input->post('acadmic_level'),
'quality'=>$this->input->post('quality'),
'noofpage'=>$this->input->post('noofpage'),
'subject_area'=>$this->input->post('subject_area')
);
$this->db->insert('orders',$data2);
return $this->db->insert_id();
这是我的控制器:
public function order()
$data['title']='Order';
$data['page']='order';
$data['type_of_paper'] = $this->writer_model->getTypeOfPaper();
$data['deadline'] = $this->writer_model->getDeadline();
$data['prices'] = $this->writer_model->getPrice();
$data['acadmic_level']=$this->writer_model->getAcadmicLevel();
$data['quality']=$this->writer_model->getQuality();
$data['subject_area']=$this->writer_model->getSubjectArea();
$data['currencies']=$this->writer_model->getCurrency();
$this->form_validation->set_rules('full_name', 'Full Name', 'trim|required|min_length[5]|max_length[50]|is_unique[users.full_name]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
));
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]',
array(
'required' => 'You have not provided correct %s.',
'is_unique' => 'This %s already exists.'
));
$this->form_validation->set_rules('contact_no', 'Contact No', 'trim|required');
$this->form_validation->set_rules('type_of_paper', 'Type Of Paper', 'trim|required');
$this->form_validation->set_rules('deadline', 'Deadline', 'trim|required');
$this->form_validation->set_rules('acadmic_level', 'Acadmic Level', 'trim|required');
$this->form_validation->set_rules('quality', 'Quality', 'trim|required');
$this->form_validation->set_rules('noofpage', 'No Of Pages', 'trim|required');
$this->form_validation->set_rules('subject_area', 'Subject Area', 'trim|required');
if ($this->form_validation->run() == FALSE)
$this->load->view('header', $data);
$this->load->view('navbar', $data);
$this->load->view('order', $data);
$this->load->view('footer', $data);
else
if($this->user_model->isDuplicate($this->input->post('email')))
$this->session->set_flashdata('flash_message', 'User email already exists');
redirect(site_url().'/home/login');
else
$config=array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '',
'smtp_pass' => '',
'mailtype' => 'html'
);
$clean = $this->security->xss_clean($this->input->post(NULL, TRUE));
$id = $this->user_model->insertUser($clean);
$token = $this->user_model->insertToken($id);
$qstring = $this->base64url_encode($token);
$url = site_url() . '/home/complete/token/' . $qstring;
$link = $url;
$message = '';
$message .= 'You have signed up with our website.
Please click here to verify your email address:'. $link;
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from('','Gourav shukla');
$this->email->to($this->input->post('email'));
$this->email->subject('This is an email Test');
$this->email->message($message);
$this->email->send();
/*if($this->email->send();)
echo 'Your email was sent, fool.';
else
show_error($this->email->print_debugger());
echo $message; send this in email
exit;*/
;
这是我的视图代码:
<?php // Change the css classes to suit your needs
$attributes = array('class' => 'form-horizontal', 'id' => 'order_form', 'name' => 'order_form');
echo form_open('/home/order', $attributes); ?>
<h3>Customer Information :</h3>
<?php echo form_input(array('name'=>'full_name', 'id'=> 'full_name', 'placeholder'=>'Full Name', 'class'=>'full_name', 'value' => set_value('full_name'))); ?>
<?php echo form_error('full_name');?>
<?php echo form_input(array('name'=>'email', 'id'=> 'email', 'placeholder'=>'Email', 'class'=>'email', 'value'=> set_value('email'))); ?>
<?php echo form_error('email');?>
<?php echo form_input(array('name'=>'contact_no', 'id'=> 'contact_no', 'placeholder'=>'Contact No', 'class'=>'contact_no', 'value'=> set_value('contact_no'))); ?>
<?php echo form_error('contact_no');?>
<hr />
<h3>Order Pricing :</h3>
<?php $attributes = 'id="type_of_paper" class="form-control"';
echo form_dropdown('type_of_paper', $type_of_paper, set_value('type_of_paper'), $attributes);?>
<?php echo form_error('type_of_paper'); ?>
<?php $attributes = 'id="deadline" class="form-control"';
echo form_dropdown('deadline', $deadline, set_value('deadline'), $attributes); ?>
<?php echo form_error('deadline'); ?>
<select class="form-control" id="acadmic_level" name="acadmic_level" onchange="calculateTotal()">
<option value=""> - Acadmic Level - </option>
<?php foreach($acadmic_level as $row)
echo '<option value="'.$row->value.'" id="'.$row->level_name.'">'.$row->level_name.'</option>';
?>
</select>
<?php echo form_error('acadmic_level'); ?>
<select class="form-control" name="quality" id="quality" onchange="calculateTotal()">
<option value=""> - Quality - </option>
<?php foreach($quality as $row)
echo '<option value="'.$row->q_value.'" id="'.$row->q_type.'">'.$row->q_type.'</option>';
?>
</select>
<?php echo form_error('quality'); ?>
<br />
<input type="text" maxlength="150" name="noofpage" id="noofpage" placeholder="No Of Pages" onkeyup="calculateTotal()"/>
<?php $attributes = 'id="price" class="form-control"';
echo form_dropdown('prices', $prices, set_value('prices'), $attributes); ?>
<input id="amount" name="amount" placeholder="Amount in British Pound(£)" type="text" />
<?php echo form_error('noofpage'); ?>
<select class="form-control" name="currency" id="currency" onchange="getCurrency()">
<?php foreach($currencies as $row)
echo '<option value="'.$row->rate.'">'.$row->name.'</option>';
?>
</select>
<?php echo form_error('currency'); ?><br />
<div id="finalPrice"></div><br />
<div id="totalPrice" name="totalPrice"></div>
<hr />
<h3>Order Information :</h3>
<select class="form-control" id="subject_area" name="subject_area">
<option value=""> - Subject Area - </option>
<?php foreach($subject_area as $row)
echo '<option value="'.$row->subject_area.'">'.$row->subject_area.'</option>';
?>
</select>
<?php echo form_error('subject_area'); ?>
<?php echo form_input(array('name'=>'required_topic', 'id'=> 'required_topic', 'placeholder'=>'Required Topic', 'class'=>'required_topic', 'value' => set_value('required_topic'))); ?>
<?php echo form_error('required_topic');?><br />
<?php echo "<input type='file' name='userfile' size='20' />"; ?><br />
<?php $data = array('name'=> 'vc_desc','id'=> 'vc_desc','value'=> set_value('vc_desc'),'rows'=> '6','cols'=> '10','placeholder'=>'Detailed Instructions','style'=> 'width:50%; margin-left:100px','class'=> 'form-control');
echo form_textarea($data); ?><br />
<hr />
<h3>Select Payment Method :</h3>
<?php echo form_radio("gender", "Female", NULL, set_radio('gender', 'Female')); ?>Female
<?php echo form_radio("gender", "Male", NULL, set_radio('gender', 'Male')); ?>Male<br />
<input type="checkbox" name="accept_terms_checkbox" value="Accept TOS" /> Accept Terms of Services<br>
<?php echo form_error('accept_terms_checkbox') ?>
<?php echo form_submit(array('value'=>'Place Order', 'class'=>'btn btn-md btn-primary btn-block')); ?>
<?php echo form_close(); ?>
当我运行这段代码时,我得到了这个输出:
output image
但我想将选项名称存储到数据库中,而不是选项中的值。 请解决这个问题。 提前致谢。
【问题讨论】:
当您获取帖子数据时,然后从数据库中获取所有选项名称数据,然后匹配它的值并更改帖子数组中的值。 【参考方案1】:我从你的最后两行得到的数据和图像正在插入数据库但值正在插入并且你想要插入名称,
如果我正确的话, 你必须在选项值中使用你的名字,比如
<option value="name">name or anything you want</option>
在 form_dropdown 中
$style=array("id"=>"any_id","class"=>"any_class");
$dd_data=array();
$dd_data['']="-select-";
foreach($data as $row) // $data coming from server
$dd_data[$row['name']]=$row['name or anything'] // $row['name'] names that you wants to add into database
echo form_dropdown('field_name',$dd_data,'',$style);
此代码只是一个示例,未经过测试。
【讨论】:
以上是关于在codeigniter中将form_dropdown数据插入mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章
在 Codeigniter 中将 http 重定向到 https