使用WHILE中的SELECT作为html输入PHP SQL中的值


Using SELECT from WHILE as value in html input PHP SQL

我是一个完全的初学者,所以请耐心等待。这只是一个测试项目,我正在努力自学一些基础知识。

我知道我的很多命令都已经过时和/或容易被注入,但我现在宁愿坚持使用(很多原因(。

我刚刚有一个关于尝试使用WHILE中的SELECT的问题,我想明白了,并在页面上得到了正确的响应。

现在,我如何使它成为echo,作为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。如果您的查询返回许多学生,您可以使用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            
    }
?>

我希望这能有所帮助。

我试图构建一个对您有帮助的代码。请记住,您使用last_name,但不返回SQL中的字段。

</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>

WHILE我离开是因为我不知道您的查询是否可以返回多个记录,尽管看起来是一个关键字。如果您不需要检查Ragnar的响应。