questions about mysqli displaying variables

Discussion in 'PHP' started by pshaw, Apr 8, 2023.

  1. #1
    My project seems simple, update a couple of database tables, one from a little form, print a receipt
    and echo out the receipt# and other values. The updates work but the values echoed are wrong or
    absent. Cmon brave souls, tell me what I'm doin rong.

    <!DOCTYPE html><html>
    <head>
      <title>record payment</title>
    <html><head>
    <script type="text/javascript">
    var monthNames = [ "January","February","March","April","May","June","July",
      "August","September","October","November","December"  ];
    var today = new Date(); 
    var date = monthNames[today.getMonth()] + " - " + today.getDate() + " - " + today.getFullYear();
    </script>
    </head>
    <body><center>
    Date:<script type="text/javascript">document.write(date);</script><p>
    <img src="apt-pic.jpg" alt="apartment" height=100 width=300><p>
    <?php
    $link = mysqli_connect("localhost", "root", "", "homedb");
    // Check connection
    if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }
    $unit=$_POST['unit'];
    $amtpaid=$_POST['amtpaid'];
    $paidday=$_POST['paidday'];
    $id = '';
    
    $amtpaid = 0;
    $amtdue =  0;
    $dueday = '';
    
    $prevbal =  0;
    $latechg =  0;
    
    $secdep =  0;
    $damage =  0;
    $courtcost =  0;
    $nsf =  0;
    $paidday = '';
    
    $bizname = '';
    $bizstreet = '';
    $bizcity = '';
    $bizzip = '';
    $bizemail = '';
    $receiptno = 0;
    $owed = $amtdue - $amtpaid;
    $due= '';
    /* Perform a query, check for error */
    $sql = "UPDATE crttbl SET
    receiptno = receiptno + 1 where id=1";
    
    echo "receipt# $receiptno";  // line 50
    
    if(mysqli_query($link, $sql)){ echo "receiptno updated"; }
    else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
    
    $due = $amtdue + $prevbal + $latechg + $secdep + $damage + $courtcost + $nsf;
    
      /* if no pay or part pay, add $35 to latechg field and amount not paid to prevbal field */
      if ($amtpaid < $due) { $prevbal = $due - $amtpaid; $latechg = $latechg + 35.00;
    $amtpaid = 0; $secdep = 0; $damage = 0;  $courtcost = 0;
    $nsf = 0; $hudpay = 0; $comments = ' '; $paidday = ' ';   }
    /* if amt paid = amt due,  */
      if ($amtpaid == $due) { $amtpaid = 0; $prevbal =  0; $latechg = 0;;
    $secdep = 0; $damage = 0;  $courtcost = 0;
    $nsf = 0; $hudpay = 0; $comments = ' '; $paidday = ' '; } 
    /* if over-payment subtract over-payment from prevbal */
      if ($amtpaid > $due)  { $prevbal = $amtpaid - $due;
    $amtpaid = 0; $latechg = 0; $secdep = 0; $damage = 0;  $courtcost = 0;
    $nsf = 0; $hudpay = 0; $comments = ' '; $paidday = ' ';   }
    
    /* Perform a query, check for error */
    $sql = "UPDATE paytbl SET
    $amtpaid = '$amtpaid', dueday = DATE_ADD(dueday, INTERVAL 1 MONTH), prevbal = '$prevbal',
    latechg = '$latechg', paidday = '$paidday' WHERE unit = '$unit'";
    if(mysqli_query($link, $sql)){ echo ""; }
    else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
    ?>
    
    <?php echo $bizname;echo "<br />"; ?>  // line  78
    <?php echo $bizstreet;echo "<br />"; ?> 
    <?php echo $bizcity;echo "<br />"; ?> 
    <?php echo $bizzip;echo "<br />"; ?><br> 
    <?php echo $bizemail;echo "<br />"; ?>  // line 82 
    
    For:<SELECT name="options">
    <option value="#990033" style="background-color: Violet;">Rent payment</option>
    <option value="#003300" style="background-color: Aquamarine;">Background Check</option>
    <option value="#6600cc" style="background-color: Pink;">Security Deposit Payment</option>
    <option value="#003300" style="background-color: Aquamarine;">Damages Payment</option>
    <option value="#990033" style="background-color: Violet;">Late Charges Payment</option>
    <option value="#003300" style="background-color: Aquamarine;">Court Costs Payment</option>
    <option value="#6600cc" style="background-color: Pink;">NSF Payment</option>
    <option value="#990033" style="background-color: Violet;"> </option>
    </SELECT><p>
    
    Tenant paying is: <?php echo $_POST["unit"]; ?> -
    Amount paid is: <?php echo $_POST["amtpaid"]; ?> -
    
    Balance due is:<?php echo $owed; ?><br>  // line 98
    <b><input type="text" size="35" maxlength="35" name=frm.Name" value="sign" STYLE="color: #000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''"><br>
    <h3>We Thank You</h3>
    </center></body></html>
    <?php
    // Close connection
    mysqli_close($link);
    ?>
    PHP:
    result:
    apartment
    receipt# 0receiptno updatedERROR: Could not able to execute UPDATE paytbl SET 0 = '0',
    dueday = DATE_ADD(dueday, INTERVAL 1 MONTH), prevbal = '0', latechg = '0', paidday = ' '
    WHERE unit = 'unit1'. You have an error in your SQL syntax; check the manual that corresponds
    to your MariaDB server version for the right syntax to use near '0 = '0', dueday = DATE_ADD(dueday,
    INTERVAL 1 MONTH), prevbal = '0', latech...' at line 2
    // line 78
    
    // line 82 For: Tenant paying is: unit1 - Amount paid is: 530.00 - Balance due is:0
    // line 98
    Code (markup):

     
    Last edited by a moderator: Apr 8, 2023
    pshaw, Apr 8, 2023 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,790
    Likes Received:
    4,529
    Best Answers:
    123
    Trophy Points:
    665
    #2
    A quick search on receiptno shows you set it to 0, then increment it, I'm not sure I get your logic.

    BTW wrap your code examples in bbcode to make it easier to read
    [PHP][/PHP]
    Code (markup):
     
    sarahk, Apr 8, 2023 IP
    seomanualsubmission likes this.
  3. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    The updates work, receiptno is incremented. all I need to know is how to define the variable correctly and
    as important, why the echoes don't work? Please answer this.

     
    Last edited by a moderator: Apr 9, 2023
    pshaw, Apr 9, 2023 IP
  4. moh-joh

    moh-joh Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Line 20
    You set:
    $amtpaid=$_POST['amtpaid'];
    PHP:
    But in line 24 you overwrites it and set as 0:
    $amtpaid = 0;
    PHP:
    Line 42 you set
    $receiptno = 0;
    PHP:
    And in line 49 you just display its not modified value:
    echo "receipt# $receiptno"; // line 50
    PHP:
    I assume that you want to display its value after it was incremented in database.

    So you need to modify line 51:
    if(mysqli_query($link, $sql)){ echo "receiptno updated"; }
    PHP:
    to this:
    if(mysqli_query($link, $sql)){
    echo "receiptno updated"; 
    $query = mysqli_query($link, "SELECT receiptno FROM crttbl WHERE id=1");
    $row = mysqli_fetch_assoc($query);
    
    $receiptno = $row['receiptno'];
    echo "receipt# $receiptno";
    }
    PHP:
    In line 71 it should be (probably):
    amtpaid = '$amtpaid'
    instead of
    $amtpaid = '$amtpaid'

    No dollar sign in the column name (I assume amtpaid is a column name)

    ---

    In general, you have a lot of variables defined, and I don't know whether they should be modified through the values submitted by the form and present in $_POST or retrieved from the database.
     
    moh-joh, Apr 13, 2023 IP
  5. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    thanks so much for your response. recode as suggested.
    this is what is displayed:
    receipt# 0
    Tenant paying is: unit1 - Amount paid is: 530.00 - Balance due is:-530
    ---------------------------------------------------------------------
    the receiptno is 250
    the amtdue is 530.00, amtpaid is 530.00, balance due should be 0 ?
     
    pshaw, Apr 13, 2023 IP