从sql中获取信息并将其放在表单中
问题描述:
因此,我需要从sql中获取信息并将其放在我的表单中的下拉列表中。这就是我所拥有的......我非常迷茫......信息已被预填充到sql中。我认为最重要的部分是相对正确的,然后我不知道如何在表格中引用它。从sql中获取信息并将其放在表单中
<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
答
在这里你去
<?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
你很容易受到
PHP
HTML [SQL注入攻击(http://bobby-tables.com)和你需要在你的'while()'循环中建立你的html。现在你只需要倾倒4份** SAME **学校名称。 PHP不会奇迹般地记住每个DB出来的名字,并且在每次打印电话时都会改变$ schoolname ..这取决于你 – 2014-11-14 19:04:55
请不要使用'mysql_ *'函数(http: //*.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php),他们不再维护和[正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。学习[准备的语句](http://en.wikipedia.org/wiki/Prepared_statement),并使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。 – 2014-11-14 19:12:40