发送更新表单时没有错误,但数据库未更新

时间:2016-03-23 04:01:03

标签: php html mysqli

我试图编辑html表中的数据, 每行都有一个编辑链接,可以将用户重定向到表单以编辑行。

问题是提交表单时数据库没有更新

它将我重定向回 editest.php

editest.php

    <?php

include('dbconn.php');

$query = "SELECT * FROM tbl_locators WHERE accholder_ID='1'";
$result = mysqli_query($con, $query);

?>

<!DOCTYPE html>
<html>
    <head>
            <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="/Cisu/css/footable.core.css">
        <link rel="stylesheet" href="/Cisu/css/footable.metro.css">
        <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
        <script src="/Cisu/javascript/jquery/footable.js"></script>
        <script src="/Cisu/javascript/jquery/footable.paginate.js"></script>  
        <script src="/Cisu/javascript/jquery/footable.filter.js"></script>  
        <link rel="stylesheet" href="/Cisu/css/search.css" type="text/css" media="screen"/>
        <link rel="stylesheet" href="/Cisu/css/footable.editable-1.0.css" type="text/css" media="screen"/>
    </head>
    <body>


        <div id="header">
            <img src="/Cisu/images/sbma.png" class="logo" id="logo"></img>
            <a style = "float:left;">Account Settings</a>
            <a href="#">Home</a>
            <a href="#">Locators</a>
        </div>
        <div id="navigation">
                        <ul class="ul-sidebar">
                <li class="li-sidebar">
                    <a class="active">View Locators</a>
                </li>
            </ul>

        <form class="form-style-1">

        <ul class="ul-sidebar">
        <li class="li-sidebar"> <label class="header-label">Company Name:</label>
            <input type="text" id="filter"/></li>
</select>
        </ul>
        </form>

        </div>
        <div id="section">
            <table class="footable" data-page-size="10" data-filter="#filter" data-filter-minimum="4"  data-first-text="FIRST" data-next-text="NEXT" data-previous-text="PREVIOUS" data-last-text="LAST">
            <thead>
            <tr>
                <th>ID</th> 
                <th>Company Name</th> 
                <th>Nationality</th> 
                <th>Primary Address</th> 
                <th>Industry Code</th>
                <th>TIN</th> 
                <th>Status</th> 
                <th>Employees</th>
                <th>Account Holder ID</th>
                <th>EDIT</th> 
            </tr>
            </thead>

            <tbody>

                <!-- populate table from mysql database -->
            <?php while($row1 = mysqli_fetch_array($result)):;?>
            <tr>
                <td><?php echo $row1['locators_ID'];?></td>
                <td><?php echo $row1['locators_CompanyName'];?></td>
                <td><?php echo $row1['locators_Nationality'];?></td>
                <td><?php echo $row1['locators_PrimaryAddress'];?></td>
                <td><?php echo $row1['locators_IndustryCode'];?></td>
                <td><?php echo $row1['locators_TIN'];?></td>
                <td><?php echo $row1['locators_Status'];?></td>
                <td><?php echo $row1['locators_Employees'];?></td>
                <td><?php echo $row1['accholder_ID'];?></td>
                <?php echo '<td><a href="editlocator.php?locators_ID=' . $row1[0] . '">Edit</a></td>'; ?>

            </tr>
            <?php endwhile;?>

            </tbody>

            <!-- the pagination -->
            <!-- hide-if-no-paging = hide the pagination control -->
            <tfoot class="hide-if-no-paging">
            <td colspan="9">
                <div class="pagination"></div>
            </td>
            </tfoot>
        </table>
    <script type="text/javascript">

       $(document).ready(function(){

           $('.footable').footable();

       });

    </script>

        </div>
        <div id="footer">
            S.B.M.A Centralized Information System 
        </div>
    </body>
    </html>

editlocator.php

   <?php

include("dbconn.php");

function renderForm($id = '', $accid = '', $companyname = '', $nationality = '', $primaryaddress = '', $industrycode = '', $tin = '', $status = '', $employees = '')
{

?>

<html>
    <head>
        <link rel="stylesheet" href="css/add.css" type="text/css" media="screen"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <Title>
        EDIT
        </title>
    </head>
    <body>

        <div id="header">
            <img src="images/sbma.png" class="logo" id="logo"></img>
            <a style = "float:left;">Account Settings</a>
            <a href="AdminMenu.php">Home</a>
            <a href="DeptMenu.php">Department</a>
        </div>
        <div id="navigation">

                </ul>
        </div>
        <div id="section">
        <h1>EDIT</h1>
            <form class="form-style-1" action="" method="post">
             <ul>
                <fieldset class="fieldset-company">
                <input type="hidden" name="locators_ID" value="<?php
    echo $id;
?>" />
                <p>ID: <?php
    echo $id;
?></p>    
                <input type="hidden" name="accholder_ID" value="<?php
    echo $accid;
?>" />
                <p>Account ID: <?php
    echo $accid;
?></p>  
                    <li>
                        <label>Company Name</label>
                        <input type='text' name='companyname' id='companyname' maxlength="50" value="<?php
    echo $companyname;
?>"/>
                    </li>               
                    <li>
                        <label>Nationality</label>
                        <input type='text' name='nationality' id='nationality' maxlength="50" value="<?php
    echo $nationality;
?>"/>
                    </li>
                    <li>
                        <label>Primary Address</label>
                        <input type='text' name='primaryaddress' id='primaryaddress' maxlength="50" value="<?php
    echo $primaryaddress;
?>"/>
                    </li>

                    <li>
                        <label>Industry Code:</label>
                        <input type='text' name='industrycode' id='industrycode' maxlength="50" value="<?php
    echo $industrycode;
?>"/>
                    </li>
                    <li>
                        <label>TIN:</label>
                        <input type='text' name='tin' id='tin' maxlength="50" value="<?php
    echo $tin;
?>"/>
                    </li>
                    <li>
                        <label>Status:</label>
                        <input type='text' name='status' id='status' maxlength="50" value="<?php
    echo $status;
?>"/>
                    </li>
                    <li>
                        <label>Employees:</label>
                        <input type='text' name='employees' id='employees' maxlength="50" value="<?php
    echo $employees;
?>"/>
                    </li>
                    </fieldset>

                         <li>
                      <input type='Submit' name='Submit' value='Submit' />
                         </li>

            </form>
        </div>
        <div id="footer">
    S.B.M.A Centralized Information System 
    </div>
    </body>

</html>

<?php
}


if (isset($_GET['locators_ID'])) {
    // if the form's submit button is clicked, we need to process the form
    if (isset($_POST['Submit'])) {
        // make sure the 'id' in the URL is valid
        if (is_numeric($_POST['locators_ID'])) {
            // get variables from the URL/form
            $id             = $_POST['locators_ID'];
            $accid          = htmlentities($_POST['accholder_ID'], ENT_QUOTES);
            $companyname    = htmlentities($_POST['companyname'], ENT_QUOTES);
            $nationality    = htmlentities($_POST['nationality'], ENT_QUOTES);
            $primaryaddress = htmlentities($_POST['primaryaddress'], ENT_QUOTES);
            $industrycode   = htmlentities($_POST['industrycode'], ENT_QUOTES);
            $tin            = htmlentities($_POST['tin'], ENT_QUOTES);
            $status         = htmlentities($_POST['status'], ENT_QUOTES);
            $employees      = htmlentities($_POST['employees'], ENT_QUOTES);


            // check that companyname and primaryaddress are both not empty
            if ($companyname == '' || $primaryaddress == '') {
                // if they are empty, show an error message and display the form
                echo "Blank Page";
                renderForm($accid, $id, $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees);
            } else {
                // if everything is fine, update the record in the database
                if ($stmt = $con->prepare("UPDATE tbl_locators SET locators_CompanyName = ?, locators_Nationality = ?, locators_PrimaryAddress= ?, locators_IndustryCode = ?, locators_TIN = ?, locators_Status= ?, locators_Employee = ?
WHERE locators_ID= ?")) {
                    $stmt->bind_param("sssssssii", $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees, $id);
                    $stmt->execute();
                    if (!$stmt = $con->prepare("UPDATE tbl_locators SET locators_CompanyName = ?, locators_Nationality = ?, locators_PrimaryAddress= ?, locators_IndustryCode = ?, locators_TIN = ?, locators_Status= ?, locators_Employee = ?
WHERE locators_ID= ?")){ echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; }
                else if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; }
                else if (!$stmt->bind_param("sssssssi", $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees, $id)) { echo "Bind Param failed: (" . $stmt->errno . ") " . $stmt->error; }

                }
                // show an error message if the query has an error
                else {
                    echo "ERROR: could not prepare SQL statement.";
                }

                // redirect the user once the form is updated
                header("Location: editest.php");
            }
        }
        // if the 'id' variable is not valid, show an error message
        else {
            echo "Error!";
        }
    }
    // if the form hasn't been submitted yet, get the info from the database and show the form
    else {
        // make sure the 'id' value is valid
        if (is_numeric($_GET['locators_ID']) && $_GET['locators_ID'] > 0) {
            // get 'id' from URL
            $id = $_GET['locators_ID'];

            // get the record from the database
            if ($stmt = $con->prepare("SELECT * FROM tbl_locators WHERE locators_ID=?")) {
                $stmt->bind_param("i", $id);
                $stmt->execute();

                $stmt->bind_result($id, $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees, $accid);
                $stmt->fetch();

                // show the form
                renderForm($id, $accid, $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees);

                $stmt->close();
            }
            // show an error if the query has an error
            else {
                echo "Error: could not prepare SQL statement";
            }
        }
        // if the 'id' value is not valid, redirect the user back to the view.php page
        else {
            header("Location: editest.php");
        }
    }
}


?>

0 个答案:

没有答案