PHP doesn't process... I think

First of all I don’t know if PHP is the correct category for my question so, if not, please excuse.

I built a complete website using PHP and MYSQL. I did this in XAMP and it all looks good. All the code works. It connects to the database and displays all the information as requested. The forms work… everything works. It’s grand.

So. I purchased a domain name and got a hosting account with hostgator. I uploaded all the files and found that the database wasn’t connecting. I got a “couldn’t connect to database error” which I should have expected. I changed the username and password and database name in my include/dbconn.inc
file which is the file that connects to the database. That worked because the “connect error” disappeared. Now, I have a new problem. Anything associated with php doesn’t show. If it is between the ‘<?php' and '?>’ then it may as well not exist. The html shows up fine. And… oddly enough, some of the forms show but not all. I try to login and it just shows a blank page after I log in.

There are no errors at all so I really don’t know where to start.

Thanks for any help you can give me.

Steve

Try to find more information about your PHP installation and what version is installed (if at all) on your account. If your account only supports a certain version of PHP, it may not support the mysql api you used. I think mysqli requires up to php 5.5 or 7.0.

I had a similar problem while I was setting up my LAMP stack on this new laptop. I had php installed but was missing some other dependencies for apache to utilize the php and I had to restart the server.

You might want to post your code. Hard to believe hostgator does not have Php properly installed. Do you have error reporting turned on? Also, check your error log.

Emgo… PHP version is 5.4. Can’t find MYSQL version.

benanamen… I’m sure hostgator has PHP setup just fine. My error_log shows the connect errors I was having and then an include error. Both of which I fixed. After that there are no errors at all.

The example of the blank page after logging in is just one example of many. The SELECT queries don’t work on the main page or the subsequent pages. the registration page is blank and the page for suggesting stores for inclusion into the website (basically a form page like the registration page) are all blank. The login form shows. The zipcode form shows. However, each page displays a header with a navigation bar which is called for using the <?php include 'includes/header.inc'; ?> code so I’d have to say that the PHP definitely works. Maybe instead it is the mysql data that has a problem.

I exported the database from XAMP and imported it to Hostgator with no errors. All went smooth. I checked and all the rows and columns in all the tables are populated.

Still don’t know where to start.

Thanks for the help. Need to think about this a little longer.

Really hard to help when you have not posted any code. We don’t have a crystal ball.

This is my login.inc.php file

<?php Session_start();

if (isset($_POST[‘submit’])) {

include 'dbconn.inc';

$uid = mysqli_real_escape_string($conn, $_POST['uid']);
$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

//error handlers
//check if inputs are empty
if (empty($uid) || empty($pwd)) {
    header("Location: ../index.php?login=empty");
    exit();
} else {
    $sql = "SELECT * FROM user WHERE user_name = '$uid' or email='$uid'";
    $result = mysqli_query($conn, $sql); 
    $num_rows = mysqli_num_rows($result);
        if  ($num_rows < 1) {
        header("Location: ../index.php?login=username_error");
        exit();
    } else { 
        if ($row = mysqli_fetch_assoc($result)) {
            //de_hashing the password
            $passwordcheck = $row['user_pwd'];
            $hashedPwdCheck = password_verify($pwd, $passwordcheck);
            if ($hashedPwdCheck == false) {
                header("Location: ../index.php?login=password_error");
                exit();
                } elseif ($hashedPwdCheck == true) {        
                    //log in the user here
                    $_SESSION['u_id'] = $row['user_id'];
                    $_SESSION['u_uid'] = $row['user_name'];
                    $_SESSION['u_first'] = $row['firstname'];
                    $_SESSION['u_last'] = $row['lastname'];
                    $_SESSION['u_zip'] = $row['zipcode'];
                    $_SESSION['u_email'] = $row['email'];
                    $_SESSION['u_city'] = $row['city'];
                    $_SESSION['u_state'] = $row['state'];
                    header("Location: ../index.php?login=success");
                exit();
                }
            }
      } 
}   

} else {
header("Location: ../index.php?login=error2");
            exit();

}

?>

Here is a SELECT query.

<?php
//Display store name with products and prices
$sql = "SELECT sid, pid, store, address, zipcode, product, current_price, lastdate, user_updated, unit "
        . "FROM store, product, prices "
        . "WHERE store.city = '$city' "
        . "and prices.sid = store.id "
        . "and prices.pid = product.ID "
        . "and product.product = '$product' "
        . "and product.category = 'beef'"
        . "AND current_price > '0.00'"
        . "ORDER by current_price, store, address";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row

    while($row = $result->fetch_assoc()) {
        ?>
        
<div class="store_output">
<span class="store_title"><?php echo $row["store"] ?></span> - 
<span class="current_price">$<?php echo $row["current_price"] ?></span>/
<span class="unit"><?php echo $row["unit"] ?></span><br>
<span class="address"><?php echo $row["address"] ?></span><br>
<span class="last_date"><?php echo $row["lastdate"] ?></span><br>
<span class="user_update">Last update by: <a href="profile_page.php?name=<?php echo $row["user_updated"] ?>"><?php echo $row["user_updated"] ?></a></span>
               </div>
       
     <form action= 'update_price.php' method='POST'>
              <input type ='submit' value = 'Update'>
              <input type ='hidden' name = 'namepid' value ='<?php echo $row['pid']; ?>'>
              <input type ='hidden' name='namesid' value='<?php echo $row['sid']; ?>'>
    </form>

After the opening <?php in login.inc.php add this line:

error_reporting(E_ALL);

That will crank the error reporting up to maximum, my gut feeling is that there’s a parse error somewhere

Also passwords should always be stored in hash form. There is no excuse whatsoever for any site storing passwords in plain text form. It’s possible that storing passwords in plain text form could potentially be illegal in some countries under data protection laws

1 Like

Have you checked that your hosting company supports mysqli? @emgo suggested that it needs 5.4 or later, though the doc page doesn’t really mention a specific version number as far as I can see.

How about writing a simple page that just connects to the database and outputs debugging information step by step as it does so? By-pass the complexity of everything else and just plod through a step at a time until you see the issue.

I wondered if it might be a page naming issue, but I think if the server wasn’t parsing PHP code it would just display it.

That’s true, hosts need to support the API as well, not just the correct PHP version. I’m not sure what control hostgator gives you in this regard.

phpinfo() should show you what there is and is not.

You should begin the debugging process by analyzing the error log.

You have numerous issues in this code. As to your problem though, Your entire script depends on the name of a button to be submitted in order to work. (submit) This is incorrect and will absolutely fail in certain cases which I suspect applies to you. Not that you should add it, but your submit button has no name attribute. The proper method is to check the REQUEST METHOD.

Correct
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’){
//process form
}

Do not ever do anything like this:

if (isset($_POST[‘submit’])) {

Make this one change and see what happens then I will address the other issues.

1 Like

I’ve always understood that it was good to do both, in-case the form was ever forced to submit some other way. Certainly couldn’t hurt to do both, but as you said, definitely not only the submit button o_o

I don’t know where you understand that from. The Server Request Method will handle ALL cases without fail whether by form submit, cURL, a browser extension, or any other method. It not only works for POST, but also for GET, HEAD, PUT, DELETE, OPTIONS and CONNECT requests.

I never said “not only the submit button”, I said NEVER. It is pointless to do both.

There are many applications what have multiple forms on a page and so using a POST key may also be applicable at times.

I didn’t say a POST key, I said submit. You can’t depend on your whole application working hoping the name of a button will always be submitted. If you’re going to put multiple separate forms one page then you would use a hidden form field and check for that after you check the REQUEST METHOD. You would still check the REQUEST METHOD. You are naively assuming the submit will always POST. As stated, if you are checking for submit, it will completely fail in certain circumstances. If you do not know under what circumstances it will fail then you have some learning to do. I will leave it to you to learn what they are. There are several.

  • Just noticed I typo’d and missed the underscore in post #14 for REQUEST_METHOD.

Example

<?php
if ($SERVER['REQUEST_METHOD'] == 'POST')
    {

    if (isset($_POST['form1_hidden_value']))
        {
        //process form 1
        }

    if (isset($_POST['form2_hidden_value']))
        {
        //process form 2
        }
    }
?>

Forms

Okay I never said you said only the button, calm down. Thanks for the info though, kinda appreciated.

I was agreeing that you should not use only a check for the submit button.

Thanks for the info. I’ll do that and see what happens.