-3

Evening All, just trying to figure out why some of my variables are not echoing from my MYSQL database. At first they were going to be Session variables of which i found did not work, so then i changed to a mysql query on my invoice page

$OrderID = $_SESSION[OrderID];
$LName = mysql_result(mysql_query("SELECT LName FROM customers WHERE     Username = '$Username'"), 0);
$Fname = mysql_result(mysql_query("SELECT FName FROM customers WHERE Username = '$Username'"), 0);

$Date =  mysql_result(mysql_query("SELECT Date FROM orders WHERE OrderID = '$OrderID'"), 0);

My Queries above. Order ID and Date are working, yet Lname and Fname are not where everything is named correctly as they are in the database.

Full Code below of where i am trying to echo them.

<div id='containter' style='position:absolute;width:750px;height:800px; left:50% ; margin-left: -375px;  border-style: solid;border-width: 1px;'>

        <div id='heading' style='position:absolute;width:650px;height:100px; left:50% ; margin-left: -325px;top:20px; border-style: solid;border-width: 1px;'> 
            <div style='position:absolute;left:500px;text-align:center;font-size:36px;'> <p>INVOICE </p></div>
            <div style='position:absolute;text-align:center;font-size:38px;width:250px;left:-20px;'> OzScopes</div>
            <div style='position:absolute;text-align:center;font-size:16px;width:250px;left:-37px;top:45px;'> ozscopes.com.au</div> 

                <div id='headingbottom' style='position:absolute;width:100%;height:20px;top:80px;'>
                    <table>
                            <tr>
                                    <td style='position:absolute;left:5%;'>Invoice Number   </td>
                                    <td style='position:absolute;left:25%;'> <?php echo $OrderID;?> </td>
                                    <td style='position:absolute;left:77%;'><?php echo $Date ;?></td>
                            </tr>

                    </table>
            </div>
        </div>

        <div id='invoicecontent' style='position:absolute;width:650px;height:600px; left:50% ; margin-left: -325px;top:140px; border-style: solid;border-width: 1px;'>
            <div id='information'> 
                <div id='customerinfo' style='position:absolute;width:255px;height:75px;top:-210px;padding:10px; border-bottom: 2px solid;border-right:1px solid; display:inline'> 
                   <table>
                                          <tr>
                                              <th>Customer Information</th>
                                          </tr>
                                          <tr>
                                              <td> <?php echo $LName;?>  lnametest</td>
                                          </tr>
                                       </table>



                </div>
                <div id='mailinginfo' style='position:absolute;width:355px;height:75px;left:275px;top:-210px;padding:10px;border-bottom: 2px solid; display:inline'> 
                Mailing Information 
                </div>


            </div>

            <div id='products' style='position:absolute;top:97px;width:650px;height:503px;'>  
<?php
      echo "<table border=\"0\" cellspacing=\"0\" padding=\"0\" width=\"650px\">";
                echo "<tr style='font-weight: bold; font-size:16px;'>";
        echo "<td colspan=\"2\" align=\"center\" style='border-bottom: 2px solid;font-family:'Ek Mukta';color:#464d48;'>Description</td>";
        echo "<td align=\"left\" style='border-bottom: 2px solid;font-family:'Ek Mukta';color:#464d48;'>Quantity</td>";
        echo "<td colspan=\"3\" align=\"center\" style='border-bottom: 2px solid ;font-family:'Ek Mukta';color:#464d48;'>Price (Per Unit)</td>";
        echo "<td align=\"center\" style='border-bottom: 2px solid;font-family:'Ek Mukta';color:#464d48;'>Total</td>";
        echo "</tr>";


      echo "</table>";

?>
                            </div>


            <div id='payment'>


            </div>


        </div>

</div>

Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Aug 14 '15 at 12:48
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 14 '15 at 12:48
  • PHPMyAdmin *is not* a database. It is a web interface for your MySQL database. – Jay Blanchard Aug 14 '15 at 12:48
  • Thanks for the hint, i will look at upgrading to them. And yeah i know PhpMyAdmin isn't the correct terminalogy for the database, just slips in every now and again as i use it so much. – OzScopes2.0 Aug 14 '15 at 12:52
  • Although it doesnt appear to solve my errors of not being able to echo the variables – OzScopes2.0 Aug 14 '15 at 12:52
  • ok here is the hint, `$LName` where it is coming from? if you have error enable in your page, you will defiantly getting undefined variable error – Shehary Aug 14 '15 at 12:54
  • $Username where it is coming from ? – Happy Coding Aug 14 '15 at 12:56
  • Your code doesn't show where `$Username` is coming from. – Zsw Aug 14 '15 at 12:59

1 Answers1

0

Where are you getting $Username? should you get it from the session first? Like so:

$Username = $_SESSION[Username];

otherwise this string is empty and the select statement will come back as empty, because the where clause was based on username = $Username (which in this case is empty).

Get username of the session variable first, then use it in where clause. Apart from that, as others have said, please avoid using mysql queries, use mysqli instead. Hope it helps.

pokrak94
  • 198
  • 1
  • 4
  • 20
  • oh my.. now i feel silly.. i forgot to add that in from when i moved them from session variables to being local variables... and yes, that fixed it. many many many thanks – OzScopes2.0 Aug 14 '15 at 13:01
  • also since they are retrieved from the same db table you can use single select statement for both first name and last name (improving performance, less trips to database) and save them into array, then when echoing refference them like so: array_name[0].fname for example. – pokrak94 Aug 14 '15 at 13:01