0

I have a problem with the Hostgator DB server. first thing first, I am a new baby on this. So, DB connects and I can log in but it doesn't allow me to save new user details and to pull out any info from DB. Everything works perfectly on localhost. So I realise that it's because of HostGator DB or $_SESSION. The code -

  include 'connection.php';
  session_start();
$id=$_SESSION['id'];
$query=mysqli_query($db,"SELECT * FROM users where user_id='$id'")or die(mysqli_error());
$row=mysqli_fetch_array($query);

And if I manually change $id to The No of user-id example-(1) everything works perfectly so I can save it and any changes appear on DB same as on a webpage. So seems it is not recognising Session id for the user who changes his user information.

The code -

include 'connection.php';
  session_start();
$id=$_SESSION['id'];
$query=mysqli_query($db,"SELECT * FROM users where user_id='1'")or die(mysqli_error());
$row=mysqli_fetch_array($query);

Does anyone know what is the issue??? I don't care about security issues on the code. Just need to be able to run the PHP code on the HostGator host and DB.

The full code after I edited regarding suggestions -

    <?php
session_start();    
include('connection.php');
$id=$_SESSION['id'];
$query=mysqli_query($db,"SELECT * FROM users where user_id=$id");
$row=mysqli_fetch_array($query);
?>
<!DOCTYPE html>
<html lang="en-US">
  <head>
  <title>IT SourceCode</title>
  <link rel="stylesheet" href="libs/css/bootstrap.min.css">
  <link rel="stylesheet" href="libs/style.css">
  <link rel="stylesheet" href="main.css">
  <style>
img {
  border-radius: 50%;
}
</style>

  </head>
  
  <h1>Profile</h1> 
<div class="profile-input-field">
   

        <form method="post" action="#" >
          <div class="form-group">
            <td> <img src="<?php echo 'images/' . $row['profile_image'] ?>" width="100%" height="100%" alt="" class="button" value="<?php echo $row['profile_image']; ?>"> </td>
          </div>

          <div class="button" value="<?php echo $row['full_name']; ?>">
              <h1><?php echo $row['full_name']; ?></h1>
          </div>

            <div class="form-group">
              <a href="https://www.facebook.com/<?php echo $row['facebook']; ?>" type="button" class="btn btn-primary button" name="facebook" value="<?php echo $row['facebook']; ?>" style="width:20em;">Facebook Page</a>
          </div>

          <div class="form-group">
              <a href="https://www.facebook.com/<?php echo $row['email']; ?>" type="button" class="btn btn-primary button" name="email1" value="<?php echo $row['email']; ?>" style="width:20em;">Send Email</a>
          </div>
 
            <center>
             <a href="form.php">Edit Profile</a>
           </center>
          <center>
             <a href="logout.php">Log out</a>
           </center>
        </form>
      </div>
      </html>

Got in Error log - PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given on line 6

Okeystm
  • 9
  • 3
  • 2
    Where do you save session data? Is `$_SESSION['id']` populated here? Should use parameterized queries but likely a later issue..unless other script is taking usernames/passwords in SQL – user3783243 Apr 02 '21 at 15:24
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 02 '21 at 16:13
  • Do you get any errors? Was the session started properly? What debugging have you done so far? – Dharman Apr 02 '21 at 16:14
  • No errors. Everything works it just not saves and not shows info on the user Columns. the problem is that when the user logs in, all info like email address not showing from DB and if the user modifies the email address it does not save the new one on DB. Login works, page works, also it goes to next page after info is updated and submitted but it does not save it on DB. – Okeystm Apr 02 '21 at 17:21
  • 2
    Let me repeat myself once more. Implement prepared statements, remove that useless `mysqli_error` and enable mysqli error reporting [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Apr 02 '21 at 18:59
  • Duplicate of https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Dharman Apr 02 '21 at 19:00
  • 1/3 done. Now enable mysqli error reporting to see the proper error and then use prepared statements to fix it. – Dharman Apr 02 '21 at 19:14
  • Thank You Dharman. Did that. On the error log, I got PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given on line 6. But I don't know how to properly enable mysqli error reporting. I promise to go and learn that. ;) – Okeystm Apr 02 '21 at 19:17
  • 1
    It's easy. Everything is explained in the post I linked. Just add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before opening the connection – Dharman Apr 02 '21 at 19:17
  • You are star Dharman. Let me read and try that. Thank you guys. – Okeystm Apr 02 '21 at 19:19

0 Answers0