简体   繁体   中英

When using post in php to update record the page just refreshes

I have a php page called edit.php . This page queries a table for the current records and displays them in a HTML table. The results are displayed in forms so they can be edited. After completing the editing, you can press the update button which triggers <form method="post" action="edit.php">... . However, when I click on the update button and submit the change, the page refreshes back to its original state and the update does not take place in the mysql database.

Any suggestions are greatly appreciated. See code below for edit.php :

<!DOCTYPE html>
<html>

<head>
<title></title>

<link rel="stylesheet" type="text/css" href="/css/stylesheet.css">

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Menu
        <span class="caret"></span></button>
            <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
              <li role="presentation"><a role="menuitem" tabindex="-1" href="/index.php">Home</a></li>
              <li role="presentation"><a role="menuitem" tabindex="-1" href="/includes/insert.html">Insert Bowler Info</a></li>
              <li role="presentation"><a role="menuitem" tabindex="-1" href="/includes/edit.php">Edit Bowler Info</a></li>
            </ul>
    </div>

    <div class="edit-main-content">
        <?php
            $hostname = "localhost";
            $username = "root";
            $password = "";
            $databaseName = "CIS2250";

            $dbConnected = mysql_connect($hostname, $username, $password);
            $dbSelected = mysql_select_db($databaseName);

            $dbSuccess = true;
            if ($dbConnected) {
                if (!$dbSelected) {
                    echo "<div>" . "DB connection FAILED" . "</div>";
                    $dbSuccess = false;
                }
            } else {
                echo "<div>" . "MySQL connection FAILED" . "</div>";
                $dbSuccess = false;
            }

            if ($dbSuccess) {

                if (isset($_POST['update'])) {

                    $updateQuery = "UPDATE bowlers SET firstName='$_POST[firstname]', lastName='$_POST[lastname]', age='$_POST[age]', average='$_POST[average]', WHERE id='$_POST[hidden]'"; 
                    $updateResult = mysql_query($updateQuery, $dbConnected);
                }

                $query = 'SELECT id, firstName, lastName, age, average FROM bowlers';
                $result = mysql_query($query);
                $resultRows = mysql_num_rows($result);


                if ($resultRows > 0) {
                    // output data of each row
                    while ($row = mysql_fetch_assoc($result)) {
                        echo "<table class='table table-striped'>"
                            ."<tr>"
                            ."<th>ID</th>"
                            ."<th>First Name</th>"
                            ."<th>Last Name</th>"
                            ."<th>Age</th>"
                            ."<th>Average</th>"
                            ."</tr>";
                        echo "<form action='edit.php' method='post'>";
                        echo "<tr>";
                        echo "<td>" . "<input type='number' name='id' value=" . $row["id"] . " </td>";
                        echo "<td>" . "<input type='text' name='firstname' value=" . $row["firstName"] . " </td>";
                        echo "<td>" . "<input type='text' name='lastname' value=" . $row["lastName"] . " </td>";
                        echo "<td>" . "<input type='number' name='age' value=" . $row["age"] . " </td>";
                        echo "<td>" . "<input type='number' name='average' value=" . $row["average"] . " </td>";
                        echo "<td>" . "<input type='hidden' name='hidden' value=" . $row["id"] . " </td>";
                        echo "<td>" . "<input type='submit' name='update' value='update' id='submit'" . "</td>";
                        echo "</tr>";
                        echo "</form>";
                    }
                } else {
                     echo "0 results";
                }
            }
            mysql_close($dbConnected);
        ?>
    </div>
</body>
</html>

EDITED QUESTION:

mysql_error() error now says: "Update query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id='2'' at line 1".

SOLUTION

<!DOCTYPE html>
<html>

<head>
<title>Jeff Weimer CIS 2250 Final Project</title>

<link rel="stylesheet" type="text/css" href="/css/stylesheet.css">

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Menu
        <span class="caret"></span></button>
            <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
              <li role="presentation"><a role="menuitem" tabindex="-1" href="/index.php">Home</a></li>
              <li role="presentation"><a role="menuitem" tabindex="-1" href="/includes/insert.html">Insert Bowler Info</a></li>
              <li role="presentation"><a role="menuitem" tabindex="-1" href="/includes/edit.php">Edit Bowler Info</a></li>
            </ul>
    </div>

    <div class="edit-main-content">
        <?php
            $hostname = "localhost";
            $username = "root";
            $password = "";
            $databaseName = "CIS2250";

            $dbConnected = mysql_connect($hostname, $username, $password);
            $dbSelected = mysql_select_db($databaseName);

            $dbSuccess = true;
            if ($dbConnected) {
                if (!$dbSelected) {
                    echo "<div>" . "DB connection FAILED" . "</div>";
                    $dbSuccess = false;
                }
            } else {
                echo "<div>" . "MySQL connection FAILED" . "</div>";
                $dbSuccess = false;
            }

            if ($dbSuccess) {

                if (isset($_POST['update'])) {


                    $updateQuery = "UPDATE bowlers SET firstName='".$_POST['firstname']."', lastName='".$_POST['lastname']."', age='".$_POST['age']."', average='".$_POST['average']."' WHERE id='".$_POST['hidden']."'";
                    $updateResult = mysql_query($updateQuery, $dbConnected);

                    if (!$updateResult) {
                        echo "Update query failed: " . mysql_error();
                    }
                }

                $query = 'SELECT id, firstName, lastName, age, average FROM bowlers';
                $result = mysql_query($query);
                $resultRows = mysql_num_rows($result);


                if ($resultRows > 0) {
                    // output data of each row
                    while ($row = mysql_fetch_assoc($result)) {
                        echo "<table class='table table-striped'>"
                            ."<tr>"
                            ."<th>ID</th>"
                            ."<th>First Name</th>"
                            ."<th>Last Name</th>"
                            ."<th>Age</th>"
                            ."<th>Average</th>"
                            ."</tr>";
                        echo "<form action='/includes/edit.php' method='post'>";
                        echo "<tr>";
                        echo "<td>" . "<input readonly type='number' name='id' value=" . $row['id'] . " </td>";
                        echo "<td>" . "<input type='text' name='firstname' value=" . $row['firstName'] . " </td>";
                        echo "<td>" . "<input type='text' name='lastname' value=" . $row['lastName'] . " </td>";
                        echo "<td>" . "<input type='number' name='age' value=" . $row['age'] . " </td>";
                        echo "<td>" . "<input type='number' name='average' value=" . $row['average'] . " </td>";
                        echo "<td>" . "<input type='hidden' name='hidden' value=" . $row['id'] . " </td>";
                        echo "<td>" . "<input type='submit' name='update' value='update' id='submit'" . "</td>";
                        echo "</tr>";
                        echo "</form>";
                    }
                } else {
                     echo "0 results";
                }
            }
            mysql_close($dbConnected);
        ?>
    </div>

</body>

</html>

You're missing commas between the columns that you're setting.

if (isset($_POST['update'])) {

    $updateQuery = "UPDATE bowlers SET firstName='$_POST[firstname]', lastName='$_POST[lastname]', age=$_POST[age], average=$_POST[average] WHERE id='$_POST[hidden]'";
    mysql_query($updateQuery, $dbConnected) or die(mysql_error($dbConnected));
}

You should always test the success of mysql_query , and display mysql_error() if it fails, then you'll know why it's not working.

尝试使用以下代码:

$updateQuery = "UPDATE bowlers SET firstName='".$_POST[firstname]."', lastName='".$_POST[lastname]."', age='".$_POST[age]."', average='".$_POST[average]."' WHERE id='".$_POST[hidden]."'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM