0

I'm trying to edit the data from the html table, each row has an edit link that redirects the user to the form to edit the row.

the problem is that the database did not update when the form is submitted

it redirected me back to the 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");
        }
    }
}


?>
  • You have `WHERE locators_ID= ?` but don't supply the `locators_ID` value in `$stmt->bind_param("sssssss", $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees);` – Sean Mar 23 '16 at 04:18
  • @Sean thank you for the reply. I added the $id in the bind param but its still not updating – JK. Constantine Mar 23 '16 at 04:29
  • did you also define `$id` before using it? as currently I only see `$id` defined after your `UPDATE` in your `else` block where you do your `SELECT`. and the only other `$id` is in your `function renderForm()`, so it is not in scope for your `UPDATE` – Sean Mar 23 '16 at 04:31
  • Looking at your edit - you added the `$id`, but did not add an `i` to the `sssssss` -> `$stmt->bind_param("sssssss", $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees, $id);` **should be** `$stmt->bind_param("sssssssi", $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees, $id);` – Sean Mar 23 '16 at 04:38
  • @Sean I did `$id = $_POST['locators_ID'];` after the line if `(is_numeric($_POST['locators_ID'])) {` and before the `$stmt->bind_param` – JK. Constantine Mar 23 '16 at 04:39
  • Did you see my last comment? `sssssss` should be `sssssssi`, as although you added the `$id` to the params list, you did not add the type. – Sean Mar 23 '16 at 04:40
  • @Sean I'm sorry about that the `ssssssi` is present in the code i thought i included it in the edit – JK. Constantine Mar 23 '16 at 04:42
  • Have you checked [`$stmt->error`](http://php.net/manual/en/mysqli.error.php) on after/on your `$stmt->execute();` to see if it throwing an error message? – Sean Mar 23 '16 at 04:46
  • @Sean **Uncaught Error: Call to a member function execute() on boolean**. on the line `if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; }` did i put the `$stmt->error;` correctly ? – JK. Constantine Mar 23 '16 at 05:07
  • `on boolean` tells you that `$stmt` is now boolean (most likely `false`), and not a mysqli object. So now you need to debug backwards to find where it is failing. So try also `if (!$stmt->bind_param("sssssssi",...all your params...)) { echo "Bind Param failed: (" . $stmt->errno . ") " . $stmt->error; }` – Sean Mar 23 '16 at 05:18
  • Just saw your edit. You are doing `$stmt->execute(); $stmt->close(); } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; }` where you are checking `!$stmt->execute()` after you already did `$stmt->execute()` and closed the connection `$stmt->close();`. You need to replace the 1st `$stmt->execute()` with your ` if (!$stmt->execute()) { ... }` – Sean Mar 23 '16 at 05:20
  • @Sean the error i got is **E_ERROR Error in file �editlocator.php� at line 198: Uncaught Error: Call to a member function bind_param() on boolean** on the line ` if (!$stmt->bind_param("sssssssi", $companyname, $nationality, $primaryaddress, $industrycode, $tin, $status, $employees, $id)) ` – JK. Constantine Mar 23 '16 at 05:26
  • 1
    Well that same message `on boolean` tells you that `$stmt` is boolean (most likely `false`) and not a mysqli object. So now you need to do the same debugging on your `$stmt = $con->prepare(...)`. You have to keeping going until to you find the actual error that is causing your `$stmt`/mysqli object to change from the mysqli object to boolean. – Sean Mar 23 '16 at 05:29
  • Just saw your edit. Why do you continue to do your error checking **AFTER** your code, and **AFTER** you have closed your connection? You are doing `...$stmt->bind_param(...); $stmt->execute(); $stmt->close(); } if (!$stmt->bind_param(...))...`. Notice how you made the same mistake that I mentioned in [this comment](http://stackoverflow.com/questions/36169615/update-form-is-sent-with-no-errors-but-the-database-did-not-update#comment59977452_36169615)? You are doing a `->prepare()` then `->execute()`, then `->close` **AND** then a 2nd `-prepare()`??? That needs to be in the place of the 1st. – Sean Mar 23 '16 at 05:35
  • @Sean oh sorry i updated is it correct ? and i tried to test, its not showing any errors its just like before – JK. Constantine Mar 23 '16 at 06:13

0 Answers0