Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.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数组进行SQL更新_Php_Sql_Adodb - Fatal编程技术网

使用PHP数组进行SQL更新

使用PHP数组进行SQL更新,php,sql,adodb,Php,Sql,Adodb,假设我有一个这样的数组 $array= Array('id'=>'3', 'name'=>'NAME', 'age'=>'12'); 这个数组中的键是表中列的名称,值是我需要更新的列的值。 我想根据键和值更新表。 我正在使用ADODB 请帮帮我 foreach ($update_array as $key => $testimonials) { $name = mysql_real_escape_string($testimonials->name);

假设我有一个这样的数组

$array= Array('id'=>'3', 'name'=>'NAME', 'age'=>'12');
这个数组中的键是表中列的名称,值是我需要更新的列的值。 我想根据键和值更新表。 我正在使用ADODB 请帮帮我

foreach ($update_array as $key => $testimonials) {
    $name = mysql_real_escape_string($testimonials->name);
    $content = mysql_real_escape_string($testimonials->content);
    $id = intval($testimonials->id);

    $sql = "UPDATE testimonials SET name='$name', content='$content' WHERE id=$id";
    $result = mysql_query($sql);
    if ($result === FALSE) {
        die(mysql_error());
    }
}
资料来源:

需要检查的其他来源。
而且

你可以用这样的方法来实现:

foreach($values as $value) {
  if(!key_exists($value, $item)) {
    return false;
  }

    $table->{$value} = $items[$value];
}
试试这个:

$sql = "UPDATE table SET ";
foreach($array as $key=>$value) {
   $sql .= $key . " = " . $value . ", "; 
}

$sql = trim($sql, ' '); // first trim last space
$sql = trim($sql, ','); // then trim trailing and prefixing commas
当然还有
WHERE
子句:

$sql .= " WHERE condition = value";
您将获得以下字符串:

UPDATE table SET id = 3, name = NAME, age = 12 WHERE condition = value
L.E:您可能需要在字符串中添加撇号,因此我必须将代码更改为以下内容:

$sql = "UPDATE table SET ";
foreach($array as $key=>$value) {
   if(is_numeric($value))
      $sql .= $key . " = " . $value . ", "; 
   else
      $sql .= $key . " = " . "'" . $value . "'" . ", "; 
}

$sql = trim($sql, ' '); // first trim last space
$sql = trim($sql, ','); // then trim trailing and prefixing commas

$sql .= " WHERE condition = value";
这将产生:

UPDATE table SET id = 3, name = 'NAME', age = 12 WHERE condition = value
L.E 2:如果您希望id列处于您的状态,代码如下:

$sql = "UPDATE table SET ";
foreach($array as $key=>$value) {
   if($key == 'id'){
      $sql_condition = " WHERE " . $key . " = " . $value;
      continue;
   }
   if(is_numeric($value))
      $sql .= $key . " = " . $value . ", "; 
   else
      $sql .= $key . " = " . "'" . $value . "'" . ", "; 
}

$sql = trim($sql, ' '); // first trim last space
$sql = trim($sql, ','); // then trim trailing and prefixing commas

$sql .= $sql_condition;
这将产生以下结果:

UPDATE table SET name = 'NAME', age = 12 WHERE id = 3

希望这有帮助!:D

假设键索引始终是id,并且adodb可以使用命名占位符,则可以执行以下操作:

$array = Array('id'=>'3', 'name'=>'NAME', 'age'=>'12');
$set = array();
$data = array();
while(list($key,$value)=each($array)) {
    $data[':'.$key] = $value;
    if($key!='id') {
        $set[] = $key . ' = :' . $key;
        // if no placeholders use $set[] = $key . " = '" . database_escape_function($value) . "'";
    }
}
$sql = "UPDATE table SET ".implode($set, ',')." WHERE id=:id";

//$data is now Array(':id'=>'3', ':name'=>'NAME', ':age'=>'12');
//$sql is now  "UPDATE table SET name=:name, age=:age WHERE id=:id";

$stmt = $DB->Prepare($sql);
$stmt = $DB->Execute($stmt, $data);

检查以下任何答案是否有效?