drfkl66684 2013-10-20 23:03
浏览 91

如何从数据库表中查询多个字段并输入到选择框?

So, I'm having some issues with calling two fields from a table and inputting them as one entry in a select box that will keep adding each entry in a while statement. In simpler terms I have one field called firstname and the next lastname in a table called professors. I need these fields combined and put together and each one inputted into a select box. Below is the code I have and I receive errors in dreamweaver at the bottom part when I start the while statement, so I know what I have isn't working but I'm having trouble figuring out why. I can't upload a picture with syntax highlighting because I just made my account and don't have enough reputation. The parts of code that I'm only concerned with in this question are the beginning parts up to where I start the table, and down at the bottom where I declare the select box for professor and the loop to grab the first and last names that are in the table. I've been reading many support solutions for similar problems but was unsuccessful. If anyone has any tips or advice I'd be greatful.

$fprofessor = $_GET['p_firstname'];
$lprofessor = $_GET['p_lastname'];
$selected_professor=$fprofessor." ".$lprofessor;

$qp = "SELECT p_id, p_firstname, p_lastname FROM professors WHERE p_firstname='$fprofessor' AND p_lastname='$lprofessor'";
$rp = @mysqli_query($dbc, $qp);

// Table header:
echo '<table align="center" cellspacing="0" cellpadding="5" width="auto">
<tr  bgcolor="' . $bg . '">
<td align="left">Section ID</td>
<td align="left">Course Major</td>
<td align="left">Course ID</td>
<td align="left">Course Name</td>
</tr>
<tr bgcolor="' . $bg . '">
    <form action="add_courses.php" method="post">
    <td align="left"><input type="text" name="section_id" size="7" maxlength="7"/></td>
    <td align="left"><select name="course_major">
    <option value="TBA">-Select Major-</option>
    <option value="CIT">CIT</option>
    <option value="CSE">CSE</option>
    <option value="CIT/CSE">CIT/CSE</option></td>
    <td align="left"><input type="text" name="course_id" size="3" maxlength="3"/></td>
    <td align="left"><input type="text" name="course_name" size="15" maxlength="25"/></td>
 </tr>

<tr  bgcolor="' . $bg . '">
<td align="left">Professor</td>
<td align="left">Available</td>
<td align="left">Semester</td>
<td align="left">Year</td>

</tr>

<tr bgcolor="' . $bg . '">

<td align="left"><select name="course_professor">
<option value="TBA">-Select Professor-</option>'
while ($row = mysqli_fetch_array ($rp, MYSQLI_NUM)) {
 echo'<option value="'.$row[. $selected_professor .].'">'.$row[. $selected_professor .].'</option>'
};          
echo'</td>

So, I've revised the code to something that seems more fitting based on the suggestions I got. Below is what I have now but I'm still receiving an error on the $qp variable and the while statement in this code

<td align="left"><select name="course_professor">'

$qp = "SELECT p_id, p_firstname, p_lastname, CONCAT(p_firstname, p_lastname) AS p_fullname FROM professors";
$rp = @mysqli_query($dbc, $qp);

echo '<option value="TBA">-Select Professor-</option>'
while ($row = mysqli_fetch_array ($rp)) {   
echo '<option value="'.$row['p_fullname'].'">'.$row['p_fullname'].'</option>'
};


echo'</td>
  • 写回答

2条回答 默认 最新

  • dongyuan1160 2013-10-20 23:10
    关注

    I think you might want to do something like:

    $qp = "SELECT p_id, p_firstname, p_lastname, CONCAT(p_firstname, p_lastname) AS p_fullname FROM professors WHERE p_firstname='$fprofessor' AND p_lastname='$lprofessor'";

    Then reference the concatenated field in the query at the bottom.

    If you need a mysql concat reference, you can read: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助