【问题标题】:Incrementing Shared Value in Database增加数据库中的共享值
【发布时间】:2015-12-10 13:18:00
【问题描述】:

我想创建一个投票系统,用户可以在其中选择单选按钮,并且选择将在数据库中递增

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);
      }
      }
     ?>

【问题讨论】:

  • 很高兴我们确切地知道你想要什么..你想要它真的很糟糕..现在..有什么问题?请不要重复超过10次..谢谢
  • 真正的问题是什么?
  • 太棒了..我也想要一辆宝马..但不幸的是我不能在这里写为什么我不能拥有它..
  • 想要增加数据库中的值

标签: php codeigniter mysqli


【解决方案1】:

类名应以大写字母开头,

class Insert_ctrl extends CI_Controller {

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

$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;
}

【讨论】:

  • 我创建它只是为了从视图中增加它..但是谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 2015-11-04
  • 1970-01-01
相关资源
最近更新 更多