增加数据库中的共享价值

时间:2015-12-10 13:18:00

标签: php codeigniter mysqli

我想创建一个轮询系统,用户可以选择单选按钮,选择将在数据库中递增

insert_view.php - 用于选择所需字段的简单表单插入

 <html>
 <body>
 <?php echo form_open('insert_ctrl'); ?>
 <h1>Insert Data Into Database Using CodeIgniter</h1><hr/>
 <?php if (isset($message)) { ?>
  <CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER>    <br>
  <?php } ?>
  <?php echo "which language do you prefer..?";?><br/>
  <?php echo form_radio(array('id' => 'rad1', 'name' => 'rad1','value'=>0)); ?>
  <?php echo form_label('JAVA'); ?> <?php echo form_error('rad1'); ?><br />

  <?php  echo form_radio(array('id' => 'rad2', 'name' => 'rad2','value'=>0)); ?>
  <?php echo form_label('PHP'); ?> <?php echo form_error('rad2'); ?><br />
  <?php echo form_radio(array('id' => 'rad3', 'name' => 'rad3','value'=>0)); ?>
  <?php echo form_label('C\C++'); ?> <?php echo form_error('rad3'); ?><br />

  <?php echo form_radio(array('id' => 'rad4', 'name' => 'rad4','value'=>0)); ?>
  <?php echo form_label('DOTNET'); ?> <?php echo form_error('rad4'); ?><br />

  <?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>

   </body>
   </html>

insert_ctrl.php - 用于通过控制器从视图向模型发送请求。

  <?php
  class insert_ctrl extends CI_Controller {
  function __construct() {
  parent::__construct();
  $this->load->helper('url');
  $this->load->model('insert_model');
  }

  function index() {
  $data = array(

  'Option1' => $this->input->post('rad1'),   //for selecting the required option
  'Option2' => $this->input->post('rad2'),    //for selecting the required option
  'Option3' => $this->input->post('rad3'),    //for selecting the required option
  'Option4' => $this->input->post('rad4')
   );
   $vote = $this->input->post('Submit');

    if($vote=="Option1"){
   $value=$value+1;
   }
   elseif($vote=="Option2"){    
   $value=$value+1;
   }
   elseif($vote=="Option3"){
   $value=$value+1;
   }
   elseif($vote=="Option4"){
   $value=$value+1;
     }
    //Setting values for tabel columns

     //Transfering data to Model
     $this->insert_model->form_insert($data);


    //$data['message'] = 'Data Inserted Successfully';
     //Loading View
      $this->load->view('insert_view', $data);
        }
      }
    ?>

insert_model.php

     <?php
     class insert_model extends CI_Model{
      function __construct() {
      parent::__construct();
      }
     function form_insert($data){
     // Inserting in Table(students) of Database(college)
     $this->db->insert('students', $data);
      }
      }
     ?>

1 个答案:

答案 0 :(得分:0)

班级名称应以大写字母

开头
class Insert_ctrl extends CI_Controller {

如果要递增的值在同一个表中,则将值传递给model,

$data['value'] = $value;
$this->insert_model->form_insert($data);

首先,$value的价值是什么。??

如果该值存在于不同的表中,则按此增加

Update table name set value = value + 1 where question_id = id.

而不是那么多if else

if($vote=="Option1" || $vote=="Option2" || $vote=="Option3" || $vote=="Option4"){
   $value=$value+1;
}