Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: need help with echo  (Read 45769 times)

0 Members and 1 Guest are viewing this topic.

guitarzRus

    Topic Starter


    Rookie

    • Experience: Experienced
    • OS: Windows 7
    need help with echo
    « on: July 27, 2022, 12:08:08 AM »
    Hi, I'm trying to print a rental receipt. I enter a tenant name, amtpaid and amtdue into form and
    expect to display the tenant name, amtpaid and balance due(if any). I'm also trying to update the
    receiptno in the numberstbl and display the new one. I've tried echo, print and can't get the
    values to display. please help.

    the receipt code:
    <?php
    $server = "localhost";
    $user = "root";
    $password = "";
    $dbname = "numbersdb";
     
    // Create connection
    $connection = mysqli_connect($server, $user, $password, $dbname);
    // Check connection
    if (!$connection) {
        die("Connection failed: " . mysqli_connect_error());
    }
     
    SET receiptno = $receiptno + 1 where id=$id";
     
    if (mysqli_query($connection, $sql)) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . mysqli_error($connection);
    }
     

    $tenant=$_POST['tenant'];
    $amtpaid=$_POST['amtpaid'];
    $amtdue=$_POST['amtdue'];
    $id="id";
    $receiptno='receiptno';
    $baldue = $amtdue - $amtpaid;

    echo "receiptno is $receiptno";echo "
    ";
    ?>

    <!DOCTYPE html><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>
    <font size="+1">Rent Receipt</font>

    Date: <script type="text/javascript">document.write(date);</script><p>
    <img src="apt-pic.jpg" alt="apartment" height=250 width=800>


    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>


    <input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="Name" value="Business Name -">
    <input type="text" size = 25 STYLE="color: #000000; background-color: #D4D4FF;" name="Addy1" value="Business address -">
    <input type="text" size = 25 STYLE="color: #000000; background-color: #D4AAFF;" name="Addy2" value="City, State, Zip">
     
     
    tenant paying is: <?php echo $_POS
    T["tenant"]; ?> -
    Amount paid is: <?php echo $_POST["amtpaid"]; ?> -
    echo "balance due is: $baldue";



    <input type="text" size=85 name="sign" value="Management signature" STYLE="color:
    #000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''">
    <input type="text" size=15 name="thanks" readonly value="We Thank You:" STYLE="color:
    #000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''">

    </center></body></html>

    the result:
    "; ?>
    Rent Receipt
    Date: July - 26 - 2022
    tenant paying is: - Amount paid is: - echo "balance due is: $baldue";

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: need help with echo
    « Reply #1 on: July 30, 2022, 09:08:43 PM »
    echo "receiptno is $receiptno";echo "
    ";

    looks like your not going to get the value of $receiptno to display because its within " " so your probably seeing $receiptno vs the actual value.

    This below might fix to show the variable value as its now not considered part of string within " "

    echo "receiptno is " $receiptno;
    echo "
    ";

    If this works for you you will want to do same to other areas where you want the value of the variable to display to user. Unable to test it at my end and just debugging what you got by reading it and that caught my attention as incorrect.

    Same issue goes for these instructions to have the variable to display in the echo outside of the " ":

    echo "balance due is: $baldue";

    and

    tenant paying is: - Amount paid is: - echo "balance due is: $baldue";

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: need help with echo
    « Reply #2 on: July 31, 2022, 04:55:49 PM »
    Dave: No- PHP will interpret variable names when the string is enclosed by double quotes.

    It doesn't work for baldue because that's not in the PHP code:

    Code: [Select]
    Amount paid is: <?php echo $_POST["amtpaid"]; ?> -
    echo "balance due is: $baldue";

    It needs to be done similar to the Amount paid part.


    Though it's unclear where the $_POST variables are expected to come from. From the example the amtpaid post variable is not set so whatever form is using this PHP for it's submission is not passing it in.


    Code: [Select]
    echo "receiptno is $receiptno";echo "
    ";
    ?>


    Quote
    SET receiptno = $receiptno + 1 where id=$id";

    This line looks like it is part of the assignment to $sql for the query text, but the rest of the line is missing? And at this point, $receiptno is going to be unassigned as far as I can tell, so is receiptno supposed to be the field in the table? Where is it coming from? further down it gets assigned the string 'receiptno'? I don't understand the purpose that.

    If the intent is to update the existing record's receipt number and add one and get the result, you can use a RETURNING clause on the sql.

    Code: [Select]
    $sql = "UPDATE numberstbl SET receiptno=receiptno+1 where id=$id RETURNING receiptno"

    you would them use the fetch_object of the returned value from mysqli to get the result, something like this:

    Code: [Select]
    $result = mysqli_query($connection, $sql);
    if ($result) {
        $receiptno = $result->fetch_object()->receiptno;
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . mysqli_error($connection);
    }

    I was trying to dereference Null Pointers before it was cool.