试图在表单上实现foreach但不确定是否可行

时间:2011-12-03 14:06:28

标签: php mysql forms pdo

我正在尝试根据表格中的模块数量显示多个文本框,以便我可以评分然后将评级提交回数据库。我不知道如何实现这一点。因为我得到的错误是 解析错误:语法错误,意外'<'在/Applications/XAMPP/xamppfiles/htdocs/cm3028/myFinal/choices.php第36行

<?php
 $sql = "SELECT moduleID, COUNT(*) FROM module";
                $stmt = $conn->prepare($sql);
                $st = $conn->prepare($nRows);
            try {
                $stmt->execute();
                $results = $stmt->fetchColumn();

                if (!$results){ // check we have some results
                    echo "No modules Available to rank <br />";
                }
                else{   //generate form

                    foreach ($results as $row ){
                        print <form action= "<?php echo $_SERVER['PHP_SELF']; ?>"  method="post" >
                        <label for="search">Rate 1:</label>
                        <input type="text" name="moduleID" id="addRate" value="" />
                        <input type="submit" name="submit" id="submit" />
                         print </form>
                    }

                }
            } catch ( PDOException $e ) {
                echo "Query failed: " . $e->getMessage();
            }
?>

1 个答案:

答案 0 :(得分:1)

错误在:

print <form action= "<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

你不能把HTML和PHP代码混在一起。您需要像字符串一样打印HTML。

您也可以这样编写此代码:

<?php
...
foreach ($results as $row ){
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<label for="search">Rate 1:</label>
<input type="text" name="moduleID" id="addRate" value="" />
<input type="submit" name="submit" id="submit" />
</form>
<?php
}
...
?>