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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line