2017-04-25 5 views
0

imgを更新して、各セルにimagesフォルダ内に1つのimgパス/名前の値があるようにします。画像のパスと名前に応じて列を更新

$path = "../images/"; 
$items = glob($path . '*.jpg'); 
foreach($items as $img) { 

$db->query("update posts set img ='" . $img . "'"); 

結果 - すべてのセルの内容が同じです。

助けが必要ですか?

+0

更新する行を指定するには、where句が必要になります。 SQL文は、すべての行を更新するように指示します。 –

+0

レコードを更新する場合は、ID(またはそのレコードの他の一意の識別子)を持つ 'WHERE'節が必要です。それ以外の場合、データベースは更新するレコードを認識しません。 –

+0

@MagnusEriksson、列全体、つまり各セルを異なる単一のimgパスで更新したい – bonaca

答えて

1

ため 例

update post set img = 'your_image_path.png' where post_id='your_post_id';] 

読むthisあなたがそれらを一つずつ更新することができますので、私は最初にすべてのポストIDを取得します。

ここにいくつかの擬似コードがあります。

// Get all post ids 
// Change the database call to match your db library 
$ids = $db->query("select id from posts"); 

$i = 0; 
foreach (glob('../images/*.jpg') as $img) { 
    if (!array_key_exists($i, $ids)) { 
     // There's no more ids, so let's stop the loop 
     // and consider the script to be done. 
     break; 
    } 

    // Update one record with one image 
    $db->query("update posts set img = '{$img}' where id = " . $ids[$i]['id']); 

    // Increment the index so we will get the next 
    // post id during the next iteration. 
    $i++; 
} 
0

コンテンツを更新する前に、結果にフィルタリングするためにクエリにwhere句を使用する必要があります。詳細

関連する問題