使用while循环插入表中

时间:2014-01-26 09:41:36

标签: php mysql sql

美好的一天。我想在我的tblEvaluation中插入值。

这是我的代码 * HTML *

<form  method="POST" action="<?php $_PHP_SELF ?>">
            <?php

        $query = "select * from tblEvaluation";
        $request = mysql_query($query)or die(mysql_error());?>
        <h3>Evaluation</h3>
        <table class="table table-hover" style="width:auto;">
        <?php
        $i = 1;
        while ($row = mysql_fetch_array($request)) {

            ?>
        <tr>
        <td style="width:20px;"><?php echo $i; ?></td>
        <td><textarea class="form-control" name="question[]" readonly style="width:250px;"><?php echo $row['Question']; ?></textarea></td>
        <input type="hidden" value="<?php echo $row['id']; ?>" name="id[]">
        <td><select class="form-control" name="answer[]">
        <option>Excellent</option>
        <option>Very Good</option>
        <option>Good</option>
        <option>Need Improvement</option>
        <option>Poor</option>
        </select></td></tr>
        <?php
        $i++;
    }
    ?>
        </table>
        <div style="float:right;"> 
        <input type="submit" class="btn btn-success" name="evaluate" value="Evaluate">
        <input type="button" value="Back" class="btn btn-danger" name="Back" onclick="window.location.href='client-home.php'">
         </div>
        </div>
        </div>
        </form>

PHP

if (isset($_POST['evaluate'])) {
$count = count($_POST['answer']); //get total number of array element
for ($i = 0; $i < $count; $i++) { // loop through array and assign values in variable and insert itin database
    $answer = $_POST['answer'][$i];
    $id   = $_POST["id"][$i];
        $sql = "Insert into tblEvaluation (Answer) VALUES ('$answer') Where id = '$id'";
        $success = mysql_query($sql) or die (mysql_error());
        if ($success == TRUE) {
            ?>
            <script>
                alert('You have successfully update account.');
            window.location.href='client-home.php';
            </script>

        <?php
        }
         else {
            ?>
            <script>
                alert('Error.');
            </script>

        <?php
        }

}

} 这是我得到的错误 您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行的'Where id ='17'附近使用正确的语法

我猜它在循环中,但我不知道该怎么做。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:4)

Insert Query中没有where子句。 您可以使用更新查询(数据已经是exixts); e.g

Update table set column='value' where id='yourid';