将数据库记录数组显示为select html标记

时间:2013-06-11 15:23:48

标签: php mysql select

我必须将插入db行的记录拉入html选择框,这样它们就必须显示在选项标签中。

if (isset($_POST['add'])) { 
    require ('connect.php');

    $sql = $db->prepare("INSERT INTO lavoro (provincia,sede,settimana,giorno,ora) VALUES (:provincia,:sede,:settimana,:giorno,:ora)") or die ('Ciao1');
    $sql->execute(array(':provincia'=>$_POST['provincia'],':sede'=>$_POST['sede'],':settimana'=>$_POST['settimana'],':giorno'=>$_POST['giorno'],':ora'=>$_POST['ora'])) or die('Non aggiorna nulla');

    echo '<select>';
    echo '<option></option>';
    echo '</select>';
}

代码OP发布在下面的评论中

if (isset($_POST['search'])) { 
    require ('connect.php'); 
    $sql = $db->prepare("SELECT sede FROM lavoro") or die ('Ciao1'); 
    $sql->execute(); 
    $result = $sql->fetchAll(); 
    print_r($result); 
    echo '<select>'; 
    echo '<option></option>'; 
    echo '</select>'; 
}

1 个答案:

答案 0 :(得分:0)

在评论中使用您的代码,尝试类似

的内容
if (isset($_POST['search'])) { 
    require ('connect.php'); 
    $sql = $db->prepare("SELECT sede FROM lavoro") or die ('Ciao1'); 
    $sql->execute(); 
    $result = $sql->fetchAll();  
    echo '<select name="sede">';
    foreach($result as $value){
            echo "<option value='{$value['sede']}'>{$value['sede']}</option>";
    } 
    echo '</select>'; 
}