help w/mysqli update

Discussion in 'PHP' started by pshaw, Mar 19, 2021.

  1. #1
    Hi, updating a database row from a form, I want to select the row(unit) to update. The form works, apt2 was specified.
    no update was made and unit wasn't selected.
    =====================================================================
    this is the code

    <?php
    //Open a new connection to the MySQL server
    require_once "getprerentdb.php";

    $flash = [];
    $update = filter_input(INPUT_POST, 'update');
    $post_id = filter_input(INPUT_POST, 'id');
    $post_unit = filter_input(INPUT_POST, 'unit');

    if(!empty($update)) {
    $sql = "UPDATE payments SET
    tenant = '{$tenant}', unit = '{$unit}', chgmoyr = '{$chgmoyr}', damage = '{$damage}',
    courtcost = '{$courtcost}', nsf = '{$nsf}', latechg = '{$latechg}', secdep = '$secdep'
    WHERE id='{$post_id}'";
    mysqli_query($sql) or die(mysql_error());
    }
    $flash[] = "Record for unit {$post_unit} has been updated";

    //MySqli Select Query
    /* ---------------------------------------- */
    $results = $mysqli->query("SELECT * FROM payments");
    if (! $results) {
    //$flash[] = $mysqli->error;
    $flash[] = "No listing for unit {$post_unit}. Please select another.";
    }

    /* ---------------------------------------- */
    ?>
    <html>
    <body>
    <h1>Miscellaneous Charges Update</h1>

    <?php
    if (count($flash)){
    foreach($flash as $msg){
    echo "<div class='flash'>{$msg}</div>";
    }
    }

    if (mysqli_num_rows ($results)){
    ?>
    <!-- -------------------------------------------------- -->
    <form method="post" action="#">
    <br />
    <input type="text" name="unit"/> <p>
    <input type="submit" name="submit" value="select unit"/>
    <!-- -------------------------------------------------- -->

    <table border='1' cellpadding="4">
    <thead>
    <tr>
    <TH>Dep#</TH>
    <TH>Tenant</TH>
    <TH>unit</TH>
    <TH>Month incurred</TH>
    <TH>Damage Chgs</TH>
    <TH>Court Costs</TH>
    <TH>N.S.F.</TH>
    <TH>Late Chgs</TH>
    <TH>Sec Deposit</TH>
    </tr>
    </thead>
    <tbody>
    <?php
    /* --------------------------------------- */
    while($row = mysqli_fetch_array($results)) {
    /* ---------------------------------------- */
    echo "<tr>
    <td>{$row['dep']}</td>
    <td>{$row['tenant']}</td>
    <td>{$row['unit']}</td>
    <td>{$row['chgmoyr']}</td>
    <td>{$row['damage']}</td>
    <td>{$row['courtcost']}</td>
    <td>{$row['nsf']}</td>
    <td>{$row['latechg']}</td>
    <td>{$row['secdep']}</td>
    </tr>";
    }
    ?>
    </tbody>
    </table>

    <?php
    }
    ?>
    </form></body></html>
    =======================================================================================
    When form was submitted this was displayed - no update was made and unit wasn't selected.
    The form specified apt2
    ----------------------------------------------------------------------------------------
    Miscellaneous Charges Update
    Record for unit apt2 has been updated

    ______________________
    | |
    |____________________|
    select unit

    Dep# Tenant unit Month incurred Damage Chgs Court Costs N.S.F. Late Chgs Sec Deposit
    0 tenant1 apt1 12/2020 0.00 0.00 0.00 30.00 0.00
    0 tenant2 apt2 0.00 0.00 0.00 0.00 0.00
    0 tenamt3 apt3 0.00 0.00 0.00 0.00 0.00
    0 tenant4 apt4 12/2020 45.00 0.00 0.00 30.00 0.00
    0 tenant5 apt5 0.00 0.00 0.00 0.00 0.00
     
    pshaw, Mar 19, 2021 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,818
    Likes Received:
    4,536
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Have you done a var_export($update) or var_export($_POST) to debug this?
     
    sarahk, Mar 20, 2021 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    In your PHP code you have this:

    $post_unit = filter_input(INPUT_POST, 'unit');

    And your query has:

    unit='{$unit}',


    $unit is not defined anywhere I guess.
    use:
    unit='{$post_unit}',
     
    JEET, Mar 27, 2021 IP