PHP help

My php page is returning results before its asked to display results. I’m not sure why

Here is code : :smiley:

Rent a movie Rental
Transaction list
Employee
<?php $dbc = mysqli_connect('localhost', 'root', 'root', 'xrentx') or die('I think that there might be something wrong. Maybe.'); // If statements if(isset($_POST['query'])){ if(isset($_POST['rental'])) { if(!empty($_POST['rental'])) { $rental_execute=$_POST['rental']; } else { $rental_execute = 0; } } if(isset($_POST['trans'])) { if(!empty($_POST['trans'])){ $trans_execute=$_POST['trans']; } else { $trans_execute = 0; } } if(isset($_POST['employ'])) { if(!empty($_POST['employ'])){ $employ_execute=$_POST['employ']; } else { $employ_execute = 0; } } } //Queries $rental_query="SELECT customer.last_name, customer.first_name, customer.phone_number, movie.movie_type, movie.title FROM customer, movie" ; $rental_execute=mysqli_query($dbc, $rental_query) or die('error'); $trans_query="SELECT order.date, customer.last_name, customer.phone_number, movie.title FROM `order`, customer, movie" ; $trans_execute=mysqli_query($dbc, $trans_query) or die('error2'); $employ_query="SELECT employee.first_name, customer.last_name, customer.first_name, customer.phone_number FROM employee, customer ORDER BY employee.last_name" ; $employ_execute=mysqli_query($dbc, $employ_query) or die('error3'); while ($row = mysqli_fetch_array($rental_execute)) { echo $row['last_name']; echo $row['first_name']; echo $row['phone_number']; echo $row['movie_type']; echo $row['title']; } while ($row = mysqli_fetch_array($trans_execute)) { echo $row['date']; echo $row['last_name']; echo $row['phone_number']; echo $row['title']; } while ($row = mysqli_fetch_array($employ_execute)) { echo $row['first_name']; echo $row['last_name']; echo $row['phone_number']; echo $row['first_name']; } mysqli_close ($dbc) ; ?>

Well, you have HTML with PHP inside it. Of course the PHP will do it’s job before the HTML.
PHP is executed SERVER-SIDE. Then, the output of the PHP is served into the CLIENT-SIDE which is the browser. Normally, you create a HTML page that calls the PHP page. Some people use PHP inline with HTML, but, this is not what PHP is really designed for. Here is how HTML with PHP is designed to work:

  1. HTML page is designed with FORM(s) code in it. This allows input from the user and is secure as the data is only on the user’s computer.
  2. A separate page with the PHP form-handling code in it is designed to pull the input from the HTML form and process the data. The results is "ECHO"d to the user which means new code is created and sent to the browser which is rendered into the display.
  3. Forms and HTML are improved by displaying data from PHP code calculations which include data pulled from databases.

Further notes: The above routines are very secure because all database code is UNSEEN by the user as it is in the SERVER-SIDE PHP code and never seen by the user. When you add PHP to the HTML page it is never seen by the browser! To prove this, load any PHP page, RIGHT-CLICK on an empty area and select VIEW-SOURCE. You will see only HTML and Javascript if any. NO PHP will every show up there.

So, now that I have you totally confused, your code is working correctly. It processes all of the PHP first, all echos or prints are “infused” into your HTML and the combined output (minus all PHP code) is sent back to the browser where it gets “rendered”. Rending is where the browser converts the HTML to visual effects, handles Javascript/Jquery and other add-ons such as CSS to create the final visual page.

Now that you understan this, rewrite your code and repost if you have questions… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service