我正在使用html和php制作学生注册表单。
首先要求您输入您的姓名,密码和电子邮件,当您单击“提交”时,它会转到另一个页面(ChooseDepartment.php
),您可以从该页面获取我的数据库中所有部门的列表以选择您自己的部门
现在,我是一个新手,所以这是我在SELECTDepartment.php中遇到的PHP代码的一部分:
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo </form>;
}
我试图让单选按钮的值带有值
部门ID,然后我可以使用当前NULL
的学生部门更新我的数据库,但我无法弄清楚如何在同一行正确使用html和php!这给了我语法错误!
答案 0 :(得分:3)
因为您在PHP
,所以您不需要打开和关闭PHP tag
。
您获取语法错误的原因仅仅是因为您没有正确操作字符串。
错误在于此行
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
^ here ^ here
因此,您需要删除PHP
标记,并且需要正确连接字符串,如:
echo '<input type="radio" name="department" value="'.$row['DEPT_ID']. '">';
和这一个
echo </form>;
您在form
标记周围缺少引号。所以它应该是,
echo '</form>';
还有其他一些拼写错误,因此您的最终代码将如下所示。
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
//echo "<br />"; add this <br /> tag to next echo
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
//or you can do this way
//echo "<input type='radio' name='department' value='$row[DEPT_ID]'><br />";
//echo "<br />"; appended in upper statement.
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
//echo </form>; closed already(above statement).
}
没有评论,更清洁:)
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
}
答案 1 :(得分:0)
无需再次打开php标记
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0) {
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while ($row = mysql_fetch_array($ShowPossibleDep)) {
echo $row['NAME'];
echo '<input type="radio" name="department" value="' . $row['DEPT_ID'] .'">';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo "</form>";
}
答案 2 :(得分:0)
看看字符串运算符
http://php.net/manual/en/language.operators.string.php
你可以将php中的两个字符串与一个点组合在一起,这样你的部分代码就会变成这样:
{
echo $row['NAME'];
echo '<input type="radio" name="department" value="'.$row['DEPT_ID'].'">';
echo "<br />";
}