在输入文本字段中返回$ row ['value'],以便可以修改该值并相应地更新数据库

时间:2016-09-09 21:31:51

标签: php mysql

此时$ row ['value']值显示在一个只读字段中。值ID =“。$ row ['name']”和& value =“。$ row ['value' ]没有在地址栏的url中传递toupdatePoll.php。

<?php
            $connection = mysqli_connect("localhost", "root", "");
            mysqli_select_db($connection, "ukelection2015");
            $rs = mysqli_query($connection, "select * from poll");
            ?>
            <hr>
            <form method= 'post' action= ''>;

                <table border=1>
                    <tr>
                        <th>Name</th>
                        <th>Value</th>
                        <th>Color</th>
                        <th></th>
                    </tr>
                    <?php
                    while ($row = mysqli_fetch_array($rs)) {
                        /*  print("<form method= 'post' action= ''>"); */
                        for ($x = 0; $x <= 2; $x++) {
                            if ($x == 0) {
                                print("<tr>");
                            } else {
                              /*  print("<td>" . $row['name'] . "</td>" . "<td><input  type = 'hidden' name= 'input'   value ='" . $row['value'] . "'> " . "</td>" . "<td>" . $row['color'] . "</td>" . "<td><a name = 'ID' href='updatePoll.php?ID = " . $row['name'] . "+ &value = " . $row['value'] . "'>Update</a></td>"); */
                                print("<td>" . $row['name'] . "</td>" . "<td><input  type = 'hidden' name= 'input'  value='".$row['value']."' ></td>" . "<td>" . $row['color'] . "</td>"."<td><a name = 'ID' href='updatePoll.php?ID=".$row['name']."'>Update</a></td>");


                                $x++;
                                if ($x == 1) {
                                    print "</tr>";
                                }
                            }
                        }
                       /* print("</form>"); */
                        // this hidden field is used in updatePoll.php to get the party name to update (i.e. $name=$_POST['name'];)
                        /*  print("<input type='hidden' name ='name' value={$row['name']}>"); */

                        /*   print("<tr>"); */

                        // name
                        // value
                        // color

                        /*  print("<td><input type=\"submit\" value=\"Update\"/></td>"); */
                        /*   print("</tr>"); */
                        /*  print("</form>"); */
                    }
                    mysqli_close($connection);
                    ?>

                </table>
            </form>

<?php

    //update the party value with the data posted here
    $connection = mysqli_connect("localhost","root","");
    mysqli_select_db($connection,"ukelection2015");

    $name=$_POST['name'];
    $value=$_POST['value'];
    print("The values were received");
    //$value=...

      $sql = " UPDATE `poll` SET `value`='".$value."' WHERE `name`='" . $name . "'";
                $result = mysqli_query($connection, $sql);
    mysqli_close($connection);

    // redirect to pollUpdate.php
    header("Location: pollUpdate.php");
    ?>

1 个答案:

答案 0 :(得分:0)

您将id作为“ID”传递给updatePoll,因为数组键区分大小写,您可以像下面一样访问它:

$id = $_GET['ID'];