dongshiqin1352 2012-10-19 14:42
浏览 62
已采纳

无法将数据插入数据库(PHP MVC)

I want to allow people to submit their contacts by entering it into a field and it would eventually send it to the database but been trying it for quite some time and cant find out whats wrong with it, if possible do let me know how to return a echo saying "SENT" when its in database. Redirecting to the same page, if possible , dont even need a refresh to get that SENT shown to people.

This is my Controller

  $this->config->db_config_fetch();
$this->load->model('skills_model');

//Get Form Data    
$this->input->post('submitsms')  ;
//GEt phone number from field
$type = $this->input->post('phonenumber');
//Call model
$this->skills_model->addsms($type);
}

This is my View @ home page

    <form method="post" action="<?php echo site_url(''); ?>" name="form"  enctype="multipart/form-data">
  <label>SMS Subscription</label>
  <input type="text" name="phonenumber" placeholder="Phone Number here">      
  <button class="btn btn-info" name="submitsms"  type="submit">Subscribe</button>
  </form>

This is my Model

function addsms($type){
     $this->db->set('number', $type); 
     $this->db->insert('subscribers');
 }

I also tried the following

function addsms($type){     
    $sql = "INSERT INTO 'subscribers' ('number') VALUES ($type)";
    $this->db->query($sql);
    echo $this->db->affected_rows();
 }

You advice would be of a great help! thank you!

  • 写回答

1条回答 默认 最新

  • doutuan6158 2012-10-19 14:49
    关注

    A small example ...........

    View file

    <form action="<?php echo ROOT_FOLDER ?>/add_price" method="post">
    
    <input type="text" class="text" name="amount" value="<?php echo set_value('amount'); ?>" />
    
    <input type="submit" class="submit" value="Approve" /> 
    </form>
    

    Controller...........

    public function post_add_price()
    {
      $data = array(
    'amount'=>$this->input->post('amount'),
     );
    $this->model->add_amount($data);  //sending amount to model to insert in dataabse
    echo "Amount added to database";
    }
    

    Model................

    public function add_amount($data)
    {
        $this->insert_helper('order_amount_table',$data);
    }
    
    public function insert_helper($table_name, $data_array){
        $this->db->insert($table_name,$data_array);
        return $this->db->insert_id();
    
    }
    

    I hope this example will help you .........if you have any doubts ask

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?