Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为php数组编写查询?_Php_Mysql - Fatal编程技术网

如何为php数组编写查询?

如何为php数组编写查询?,php,mysql,Php,Mysql,我有下面的php数组,它从一个充满收音机和复选框的表单中获取所有值 foreach(array('buss_type','anotherfield','anotherfield','...etc') as $index) { if (isset($this->request->post[$index])) { $this->data[$index] = $this->request->post[$index]; } else {

我有下面的php数组,它从一个充满收音机和复选框的表单中获取所有值

foreach(array('buss_type','anotherfield','anotherfield','...etc') as $index)
{
    if (isset($this->request->post[$index])) {
        $this->data[$index] = $this->request->post[$index];
    } else { 
        $this->data[$index] = NULL; 
    }
}
现在,我想知道如何编写查询,将这些值发送到我的数据库,发送到我刚刚创建的新表(零售商)。每个radio/checkform值在my retailer表中都有自己的列,如何编写查询以便$index中包含的所有值都转到它们的特定列

下面是我的其他查询的示例

public function addCustomer($data) {
    //this is the one I am trying to write, and this one works, 
    //but I'd have to add every single checkbox/radio name to the 
    //query, and I have 30!
    $this->db->query("INSERT INTO " . DB_PREFIX . "retailer SET buss_t = '" . 
            (isset($data['buss_t']) ? (int)$data['buss_t'] : 0) . 
            "', store_sft = '" . 
            (isset($data['store_sft']) ? (int)$data['store_sft'] : 0) . 
        "'");
    //Ends Here
    $this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . 
            (int)$this->config->get('config_store_id') . "', firstname = '" . 
            $this->db->escape($data['firstname']) . "', lastname = '" . 
            $this->db->escape($data['lastname']) . "', email = '" . 
            $this->db->escape($data['email']) . "', telephone = '" . 
            $this->db->escape($data['telephone']) . "', fax = '" . 
            $this->db->escape($data['fax']) . "', password = '" . 
            $this->db->escape(md5($data['password'])) . "', newsletter = '" . 
            (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . 
            "', customer_group_id = '" . 
            (int)$this->config->get('config_customer_group_id') . 
            "', status = '1', date_added = NOW()");

非常感谢您提供的见解。

最好的方法是创建一个函数,该函数接受数组和表名作为参数,并执行插入查询

function insertArray($table, $array)
{
  $keys =""; $values = "";
  foreach($table as $k=>$v)
  { 
      $keys.=($keys != "" ? ",":"").$k:
      $values .=($values != "" ? "," :"")."'".$v."'";
  }
  $this->db->query("INSERT INTO ".$table." (".$keys.") VALUES (".$values.");
}
阵列的结构必须如下所示:

 array("db_attribute1"=>"value1","db_attribute2"=>"value2");

将列名和列值存储在单独的数组中,并使用infrade()生成以逗号分隔的列和值列表

$values = array();
$columns = array('buss_type','anotherfield','anotherfield','...etc');
foreach($columns as $index)
{
    if (isset($this->request->post[$index]))
    {
        $this->data[$index] = $this->request->post[$index];
        $values[] = $this->db->escape($this->request->post[$index]);
    }
    else
    { 
        $this->data[$index] = NULL;
        $values[] = "''";
    }
}



$this->db->query("INSERT INTO table_name (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ");

将来,请查看代码的输出并思考,“这看起来格式好吗?”@jprofitt不确定您的意思是什么?如果你有建议,我很乐意考虑,但请具体说明。谢谢。你把大部分代码放在一行上,你必须滚动阅读,这让你很难理解。我编辑它是为了让你在网站上更容易阅读。啊,好的,谢谢,下次会考虑的。谢谢