2014-11-14 76 views
0

因此,我需要从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 
+4

你很容易受到

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 [SQL注入攻击(http://bobby-tables.com)和你需要在你的'while()'循环中建立你的html。现在你只需要倾倒4份** SAME **学校名称。 PHP不会奇迹般地记住每个DB出来的名字,并且在每次打印电话时都会改变$ schoolname ..这取决于你 – 2014-11-14 19:04:55

+2

请不要使用'mysql_ *'函数(http: //stackoverflow.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

回答

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 
+0

它说undefined索引:id并没有在字段中显示 – esaunde1 2014-11-14 19:33:15

+0

这是因为你没有$ _GET。最后打开带有?id = 1的网址 – heXer 2014-11-14 20:16:57

相关问题