简体   繁体   English

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

[英]Unable to insert data into database (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. 我希望允许人们通过将其输入到一个字段来提交他们的联系人,它最终会将其发送到数据库但是已经尝试了很长时间并且无法找到它的错误,如果可能的话请告诉我如何返回当它在数据库中时,回声说“SENT”。 Redirecting to the same page, if possible , dont even need a refresh to get that SENT shown to people. 如果可能的话,重定向到同一页面,甚至不需要刷新就可以向人们显示SENT。

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 这是我的View @主页

    <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! 谢谢!

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 我希望这个例子可以帮助你.........如果你有任何疑问的话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM