从sql中获取信息并将其放在表单中

时间:2014-11-14 19:02:43

标签: php mysql sql forms

所以我需要从sql中获取信息并将其放入我的表单下拉列表中。这就是我所拥有的......我非常迷失..信息已经预先填充到了sql中。我相信顶部是相对正确的,然后我不知道如何在表单中引用它。

PHP

<?php
$id= $_GET['id'];

$conn = mysql_connect("localhost", "root", "") or die (mysql_error());

mysql_select_db("assignment 3", $conn);

$sql = "select schoolname FROM schooltable WHERE id=$id";

$result=mysql_query($sql, $conn) or die(mysql_error());

while ($row=mysql_fetch_assoc($result)){
    foreach($row as $name => $value){
        print "$name = $value</br>";
    }
}

mysql_data_seek($result, 0);
while ($row=mysql_fetch_assoc($result)){
    //select id, firstname, lastname from userlist
    $school = $row["schoolname"];
    $grad = $row["lastname"];
}
?>

HTML

                     <div class="form-group">
                    <label class='col-xs-4 control-label'>What school did you go to for your undergrad?  </label>
                    <div class='col-xs-8'>
                        <select class="form-control background" id='dropdown'>
                          <option>"<?php print $schoolname ?>"</option>
                          <option>"<?php print $schoolname ?>"</option>
                          <option>"<?php print $schoolname ?>"</option>
                          <option>"<?php print $schoolname ?>"</option>
                          <option value="bing">"<?php print $schoolname ?>"</option>
                        </select>
                        <input type="hidden" name="id" id='id' value="<?php print $id ?>">
                        <input type="hidden" name="editMode" value="edit">
                    </div>
                </div

1 个答案:

答案 0 :(得分:1)

你去吧

<?php
if(!isset($_GET['id']]){
    echo 'id= not present in URL. Exiting.';
    return false;
}
$id = intval($_GET['id']);

$conn = mysql_connect("localhost", "root", "MIS42520!$") or die (mysql_error());

mysql_select_db("assignment 3", $conn);

$sql = "select * FROM schooltable WHERE id='" . mysql_real_escape_string($id) . "'";

$result = mysql_query($sql, $conn) or die(mysql_error());

$schools = array();
while ($row = mysql_fetch_assoc($result)) {
    $schools[] = $row;
}
?>

<div class="form-group">
    <label class='col-xs-4 control-label'>What school did you go to for your undergrad?  </label>
    <div class='col-xs-8'>
        <select class="form-control background" id='dropdown'>
            <?php foreach($schools as $school){?>
                <option value="<?php echo $school['schoolname'];?>"><?php echo $school['schoolname'];?></option>
            <?php } ?>
        </select>
        <input type="hidden" name="id" id='id' value="<?php echo $id ?>">
        <input type="hidden" name="editMode" value="edit">
    </div>
</div