PHP中如何合并数组插入代码,插入数据库?
1、默认数组字段插入代码:$post=$this->set($post);//赋值给数组foreach($postas$k=>$v){if(in_array($k,$th...
1、默认数组字段插入代码:
$post = $this->set($post); //赋值给数组
foreach($post as $k=>$v) {
if(in_array($k, $this->fields)) { $sqlk .= ','.$k; $sqlv .= ",'$v'"; }
}
$sqlk = substr($sqlk, 1);
$sqlv = substr($sqlv, 1);
$this->db->query("INSERT INTO {$this->table} ($sqlk) VALUES ($sqlv)");
$this->itemid = $this->db->insert_id();
2、表单数组字段插入代码:
$sheetData = $this->readexcel($upfile);
$keyfileds = array ('title','qty','brand');
foreach ( $sheetData as $key => $words ){
if ( $key != 1 ){
$sql = "insert into {$this->table} (" . implode( $keyfileds, "," ) . ") values ('" . implode ( $words, "','" ) . "')";
$result = $this->db->query ( $sql );
3、如何将上述两段代码合并,每插入一条表单数据时同时插入默认字段数据?要求只执行一次插入操作。
操作同一数据表,谢谢! 展开
$post = $this->set($post); //赋值给数组
foreach($post as $k=>$v) {
if(in_array($k, $this->fields)) { $sqlk .= ','.$k; $sqlv .= ",'$v'"; }
}
$sqlk = substr($sqlk, 1);
$sqlv = substr($sqlv, 1);
$this->db->query("INSERT INTO {$this->table} ($sqlk) VALUES ($sqlv)");
$this->itemid = $this->db->insert_id();
2、表单数组字段插入代码:
$sheetData = $this->readexcel($upfile);
$keyfileds = array ('title','qty','brand');
foreach ( $sheetData as $key => $words ){
if ( $key != 1 ){
$sql = "insert into {$this->table} (" . implode( $keyfileds, "," ) . ") values ('" . implode ( $words, "','" ) . "')";
$result = $this->db->query ( $sql );
3、如何将上述两段代码合并,每插入一条表单数据时同时插入默认字段数据?要求只执行一次插入操作。
操作同一数据表,谢谢! 展开
2014-11-20
展开全部
你参考下tp的关联模型
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询