2016-06-14 77 views
-1
public function update($id, $table, $data, $exclude = array()){ 

    $query = "UPDATE $table SET"; 

    $fields = $values = array(); 

    if(!is_array($exclude)) $exclude = array($exclude); 
    foreach(array_keys($data) as $key) { 

     if(!in_array($key, $exclude)) { 
      $fields[] = "`$key`"; 
      $values[] = "'" . $this->db->real_escape_string($data[$key]) . "'"; 
     } 

     $fields = implode(" ", $fields); 
     $values = implode(" ", $values); 

     $query .= $fields . "=" . $values . ","; 

    } 

    $query = $query . "WHERE id = '".$id."' "; 


     if(!$this->db->query($query)){ 
     echo "Something wrong with query "; 

     } 
     else{ 
     echo "successfully updated"; 
     } 

    } 

遇到错误更新的数据动态

Fatal error: [] operator not supported for strings 

从中工作的附加功能,调整了代码。希望在不使用变量的情况下动态更新字段和值,例如$ _POST ['address']。

$query .= $fields . "=" . $values . ", "; 

似乎没有工作。不知道是什么导致了错误:致命错误:[]运算符不支持字符串。如何在sql查询中插入field = value?

+1

你implode数组内foreach和更新在同一个变量,所以他们改变为字符串。 '$ fields = implode(“”,$ fields);',在foreach之外执行,或者在foreach开头初始化'$ fields = $ values = array();'。 –

+0

为什么人们帮助你,如果你不采取建议? –

回答

1

使用这样

if(!is_array($exclude)) $exclude = array($exclude); 
    foreach(array_keys($data) as $key) { 
     if(!in_array($key, $exclude)) { 
      $query .= $key . "='" . $data[$key] . "' ,"; 
     } 
    } 
    $query = substr($query,0,strlen($query)-1); 

    $query = $query . " WHERE id = '".$id."' "; 
+0

谢谢,但不幸的是,除了运行查询失败之外,它不显示任何内容。 – joe

+0

我更新了代码。试试看,并告诉一次 – Mani

+0

耶谢谢。我复制了你的substr行。非常感谢你 – joe

1

看看foreach循环!

foreach(array_keys($data) as $key) { 

    if(!in_array($key, $exclude)) { 
     $fields[] = "`$key`"; 
     $values[] = "'" . $this->db->real_escape_string($data[$key]) . "'"; 
    } 

    $fields = implode(" ", $fields); // Mistake done here 
    $values = implode(" ", $values); // Mistake done here 

    $query .= $fields . "=" . $values . ","; 

} 

改变你的那两条线和循环外的下一条线。可能是这个解决你的问题。

foreach(array_keys($data) as $key) { 
    if(!in_array($key, $exclude)) { 
     $fields[] = "`$key`"; 
     $values[] = "'" . $this->db->real_escape_string($data[$key]) . "'"; 
    }  
} 
$fields = implode(" ", $fields); 
$values = implode(" ", $values); 
$query .= $fields . "=" . $values . ","; 
+0

是啊,这就是我之前已经做过你张贴。但它不起作用。 – joe

+0

现在是什么问题? –

+0

只有该查询失败。 – joe

1

按照我的代码。哪里不需要实现implode()函数。我改变了你的功能代码。尝试一下。

public function update($id, $table, $data, $exclude = array()){ 

    $query = "UPDATE $table SET"; 

    $fields = $values = array(); 

    if(!is_array($exclude)) $exclude = array($exclude); 
    foreach(array_keys($data) as $key) { 

     if(!in_array($key, $exclude)) { 
      $queryArr[] = $key . "='" . $this->db->real_escape_string($data[$key]);     
     } 
    } 
    $query = implode(" ,", $queryArr); 

    $query = $query . "WHERE id = '".$id."' ";  

     if(!$this->db->query($query)){ 
     echo "Something wrong with query "; 

     } 
     else{ 
     echo "successfully updated"; 
     } 

    } 
+0

谢谢。现在sql错误消息:准备失败:(1064)您的SQL语法有错误;请检查与您的MySQL服务器版本相对应的手册,以便在第1行'WHERE id ='1''附近使用正确的语法。 – joe

+0

http://stackoverflow.com/questions/17713278/attempting-to-learn-mysqli-prepared -statements-what-am-i-doing-wrong –

+0

http://www.inmotionhosting.com/support/website/database-troubleshooting/error-1064 –