1

我有桌子:平台

此表列内:1. first_scan_date, 2. next_scan_date 3. scan_frequency

现在,我从基于网络的公式中得到了这样的数组:

Array
(
    [scan_freq1] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [first_scan_date1] => Array
        (
            [0] => 0000-00-00
            [1] => 0000-00-00
            [2] => 0000-00-00
            [3] => 0000-00-00
        )

    [next_scan_date1] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
        )

)

如何使用来自 PHP 数组的内容在数据库中更新它?我想我需要像 foreach 这样的东西,但不知道......

更新平台 (first_scan_date,next_scan_date,scan_frequency) 值 (?,?,?)...

帮助?

4

3 回答 3

1

试试下面的方法

$cnt = count($arr['scan_freq1']);
for($i=0;$i<$cnt;$i++){
  echo $arr['scan_freq1'][$i];
  echo $arr['first_scan_date1'][$i];
  echo $arr['next_scan_date1'][$i];

//now you have three values make query and update into db

$db->prepare(
     "UPDATE `platforms` SET `first_scan_date`=?, `next_scan_date`=?,
     `scan_frequency`= ? WHERE platformtype='os'"
 )->execute(array($arr['first_scan_date1'][$i],$arr['next_scan_date1'][$i],$arr['scan_freq1'][$i]));
}
于 2012-12-08T17:59:25.663 回答
1

我将遍历数组以准备一个要插入到单个查询中的字符串,如下所示:

$sql = "INSERT INTO table (first_scan_date,next_scan_date,scan_frequency)
VALUES ('0000-00-00','0000-00-00',1),('0000-00-00','0000-00-00',1),('0000-00-00','0000-00-00',1)
ON DUPLICATE KEY UPDATE first_scan_date=VALUES(first_scan_date),next_scan_date=VALUES(next_scan_date),scan_frequency=VALUES(scan_frequency);";

你可以这样做:

$freq = $arr['scan_freq1'];
$date1 = $arr['first_scan_date1'];
$date2 = $arr['next_scan_date1'];
foreach ($date1 as $key => $value){
    $row[] = "('".$value."','".$date2[$key]."',".$freq[$key].")";
}
$list = implode(',', $row);
$sql = "INSERT INTO table (first_scan_date,next_scan_date,scan_frequency)
VALUES ".$list."
ON DUPLICATE KEY UPDATE first_scan_date=VALUES(first_scan_date),next_scan_date=VALUES(next_scan_date),scan_frequency=VALUES(scan_frequency);";
于 2012-12-08T18:03:42.133 回答
0

这可能为时已晚,但可能会在现在或将来对某人有所帮助我相信这是实现这一目标的最短方法

public function update($table,$values,$where_clause)
{
  foreach ($values as $key => $val)
  {
    $valstr[] = $key." = '$val'";
  }

 $update_query = 'UPDATE '.$table.' SET '.implode(', ', $valstr)
 ." WHERE ".$where_clause; 
  return $update_query;
}

在一个codeigniters核心文件中找到了它,它只是循环遍历所有值,将它们存储在一个数组中,然后将数组转换为一个字符串并将其添加到查询中,最后一个where子句..我希望这对某人有所帮助

于 2019-03-09T00:26:53.140 回答