在html输入PHP SQL中使用SELECT from WHILE作为值
我是一个完全的初学者,所以请容忍我。这只是一个测试项目,我正试图教自己一些基础知识 我知道我的很多命令都已经过时和/或容易被注入,但出于许多原因,我宁愿坚持使用这些命令 我只是有一个关于如何使用WHILE中的SELECT的问题,我想出来了,并让它在页面上响应正确的响应 现在,我如何使它作为HTML文本框的值进行响应?这不起作用,我也试着寻找打字错误,但坦率地说,我不知道我在做什么 我看到$studentid和$teacherinfo显示得很好,我想是因为它们是正态变量。 我是否可以在页面的更上层为first name和last name再定义两个变量,这样我就不需要在每个输入中包含太多的代码,从而避免出现错误 这是我的页面代码。输入将被隐藏,但为了调试目的,我一直在制作文本框在html输入PHP SQL中使用SELECT from WHILE作为值,php,html,sql,Php,Html,Sql,我是一个完全的初学者,所以请容忍我。这只是一个测试项目,我正试图教自己一些基础知识 我知道我的很多命令都已经过时和/或容易被注入,但出于许多原因,我宁愿坚持使用这些命令 我只是有一个关于如何使用WHILE中的SELECT的问题,我想出来了,并让它在页面上响应正确的响应 现在,我如何使它作为HTML文本框的值进行响应?这不起作用,我也试着寻找打字错误,但坦率地说,我不知道我在做什么 我看到$studentid和$teacherinfo显示得很好,我想是因为它们是正态变量。 我是否可以在页面的更上层
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name` FROM `students` WHERE student_id = '$studentid'",$connection);
?>
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<h5>Student id: <?php echo $studentid ?></br>
Student first name: <?php
while($row = mysql_fetch_array($result)){
echo $row['first_name'];
}
?>
</br>
Voted for: <?php echo $teacherinfo ?>
</h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['first_name'];
} ?>"/>
</br>
<input type="text" name="last_name" value="<?php while($row = mysql_fetch_array($result)){
echo $row['last_name'];
} ?>"/>
</br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>
您不能使用while,因为您的查询只返回一个学生。你必须使用if而不是while。如果您的查询返回许多学生,您可以在查询时使用。
请尝试以下代码:
<?php
if($row = mysql_fetch_array($result)){
?>
Student first name: <?php echo $row['first_name'];?>
</br>
Voted for: <?php echo $teacherinfo ?></h5>
<input type="text" name="student_id" value="<?php echo $studentid; ?>"/></br>
<input type="text" name="first_name" value="<?php echo $row['first_name'];?>"/></br>
<input type="text" name="last_name" value="<?php echo $row['last_name'];?>"/></br>
<input type="text" name="teacher" value="<?php echo $teacherinfo; ?>"/></br>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
<?php
}
?>
我希望这有帮助。我试图构建一个代码来帮助您。请记住,您使用姓氏,但不返回SQL中的字段
而我之所以离开,是因为我不知道您的查询是否可以返回多个记录,尽管看起来是一个键。如果您不需要检查Ragnar的响应。对于初学者来说,您的php代码不应该在头部,它应该在html外部或正文内部。现在,你有一个由5个子问题组成的问题。你应该试着单独求解每一个子集,然后组合在一起,每一个子集以前都求解过很多次。此外,一次获取数组并将值存储在变量中会更有意义,您可以在需要时轻松使用这些变量。例如,从查询数据库开始,看看您是否得到任何结果,仅此一项就足以回答一个问题,但再次强调,这应该很容易,谢谢!现在效果很好。
</head>
<body>
<div align="center">
<form method="post" action="vote_post.php">
<h1>Vote for Teacher of the Month</h1>
<h4>(step 2 of 2)</h4>
<h2>Confirm the Information Below</h2>
<?php
$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$studentid = $_POST['student_id'];
$teacherinfo = $_POST['teacher'];
$result = mysql_query("SELECT `first_name`,`last_name`,`student_id` FROM `students` WHERE student_id = $studentid",$connection);
while($row = mysql_fetch_array($result)){
echo "<h5>Student id: $row['student_id'] </br>" .
"Student first name: $row['first_name'] </br>" .
"Voted for: $teacherinfo </h5> " .
"<input type='text' name='student_id' value='$row[\'student_id\']' /></br>" .
"<input type='text' name='first_name' value='$row[\'first_name\']' /></br>" .
"<input type='text' name='last_name' value='$row[\'last_name\']' /></br>" .
"<input type='text' name='teacher' value='$teacherinfo' /></br>"
}
?>
<input type="submit" value="Submit Vote" class="inputbutton"/></br></br></br>
</form>