insert form input value in database in php

问题: I have one form input like <input type="text" class="form-control" name="SITE_URL" id="SITE_URL" required> So what I want is to add name's v...

问题:

I have one form input like

<input type="text" class="form-control"  name="SITE_URL" id="SITE_URL" required>

So what I want is to add name's value SITE_URL in one database column and the value I insert in other column like

v_name      l_value
---------- ----------
SITE_URL  "Inserted value "

l_value is inserted completely but v_name is not inserted my $_post array is like:

Array
(
    [SITE_URL] => value that i inserted
)

My code is:

if($_SERVER['REQUEST_METHOD'] == "POST"){
        $post["l_value"] = $this->input->post('SITE_URL');
        $post["v_name"] = $this->input->post('');
        $addPage = $this->admin_model->addSiteSetting($post);
        exit;

}

And addSiteSetting function is:

 public function addSiteSetting($ins){

    $this->db->insert('tbl_setting', $ins);
    return 1;
}

回答1:

You can process $_POST in foreach loop to get both names and their values, like this:

if($_SERVER['REQUEST_METHOD'] == "POST"){
      $post = array();
      foreach ($_POST as $name => $value) {
      $post["l_value"] = $value;
      $post["v_name"] = $name;
      }

        $addPage = $this->admin_model->addSiteSetting($post);
        exit;
}

回答2:

You should passSITE_URL as static name for $post["v_name"]

you need to change just one line $post["v_name"] = $this->input->post(''); To $post["v_name"] = 'SITE_URL';

if($_SERVER['REQUEST_METHOD'] == "POST"){
        $post["l_value"] = $this->input->post('SITE_URL');
        $post["v_name"] = 'SITE_URL';
        $addPage = $this->admin_model->addSiteSetting($post);
        exit;
}

回答3:

You just need to remove the following line. Because here you store the blank value into the v_name. That's why v_name doesn't get any value and keep it empty.

$post["v_name"] = $this->input->post('');

And Instead of the above line you need to add this line:

$post["v_name"] = 'SITE_URL';

回答4:

You have

$post["l_value"] = $this->input->post('SITE_URL');
    $post["v_name"] = $this->input->post('');

remove

$post["v_name"] = $this->input->post('');
  • 发表于 2019-02-20 00:12
  • 阅读 ( 166 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除