Jump to content

Mysql db data doesn't register


Html

Recommended Posts

Hi,

I am using a free website host to display a login/registration system, the exact same one as the one I am using on a paid service. For what ever reason the free host doesn't seem to store any registered data.

I'm using the same php files, there isn't anything different, since they are from the same setup. The php version on the free host is 7.

I checked both mysql setting pages, I don't know what is the problem with this. If it worked on the other free host I was using months ago, but I closed that account due to pop up ads, this free host doesn't put any on a page.

Thank you.

Link to comment
Share on other sites

  • Replies 64
  • Created
  • Last Reply

Top Posters In This Topic

Index.php is the link in the address bar after registration. So it should of been profile.php after.

As for checking errors, I don't remember about that.

Edited by Html
Link to comment
Share on other sites

I can't comment on code that I haven't seen, but in general it's your responsibility to check for database errors, PHP usually does not report them automatically, and you need to make sure that your PHP settings are set up so that you can find any error messages from PHP.  If there is an error then there will be an error message if everything is set up correctly.

Link to comment
Share on other sites

Well having recall my previous posts, you'll know i'm not very good at web languages.

From what I can remember, the php settings are the same as the previous free website host I did use, and the paid host.

I have a feeling that free host I am using is may be a reseller account from fasthost web hosting. It only has ads on the cpanel area.

As for the error code, I have the index

so index.php

<?php 
/* Main page with two forms: sign up and log in */
require 'db.php';
session_start();
?>
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <?php include 'css/css.html'; ?>
</head>

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
    if (isset($_POST['login'])) { //user logging in

        require 'login.php';
        
    }
    
    elseif (isset($_POST['register'])) { //user registering
        
        require 'register.php';
        
    }
}
?>

And then, what other code would be needed to determine errors?

Thank you.

Edited by Html
Link to comment
Share on other sites

For starters, add this to the top of every page:

ini_set('display_errors', 1);
	error_reporting(E_ALL);

You wouldn't want to use those settings for a production site, you would want to use error logging instead, but that's fine for development.  You didn't show any database code there, it's just a bunch of includes, but you need to check the return values of your database functions to figure out if an error happened.  

http://php.net/manual/en/mysqli.error.php

If you're using PDO I would recommend using exceptions for errors.

http://php.net/manual/en/pdo.error-handling.php

Link to comment
Share on other sites

I tried that for index.php, it doesn't load anything once done,

This is all it loads after registration.

ini_set('display_errors', 1); error_reporting(E_ALL); 

I have a feeling it must be the free host, because nothing has changed between the paid host, and the free host.

Not sure what I am using, all I know is I got the files from that video tutorial as a start really, after the previous video one didn't go any further.

Edited by Html
Link to comment
Share on other sites

And yes I did try that too. Also this affects the CSS on the page too.

I tried placing that code in the first part, like so.

 

<?php 
/* Main page with two forms: sign up and log in */
require 'db.php';
session_start();
ini_set('display_errors', 1);
	error_reporting(E_ALL);
?>

That has solved that problem, and then this was displayed after registration.

mysqli::escape_string(): Couldn't fetch mysqli in  line 12 and 18 in register.php

I guess this is line 12 $first_name = $mysqli->escape_string($_POST['firstname']);

and 18 $result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

Edited by Html
Added more info
Link to comment
Share on other sites

That sounds like you're not connecting correctly.  Again, if there's an error with MySQL, like with connecting, it is your responsibility to check for that error and output a message if you want to.  It does not happen automatically.

You're also on the wrong track if you're trying to escape data manually, you should use prepared statements instead to avoid SQL injection attacks.

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

I always prefer using PDO instead of mysqli though:

http://php.net/manual/en/book.pdo.php

http://php.net/manual/en/pdo.prepared-statements.php

Link to comment
Share on other sites

2 hours ago, Html said:

mysqli::escape_string(): Couldn't fetch mysqli in  line 12 and 18 in register.php

I guess this is line 12 $first_name = $mysqli->escape_string($_POST['firstname']);

and 18 $result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

This sounds like an error with your mysqli object.

The Database Location, Username, Password, and Database Name on the free plan are the same as the paid plan?

 

If they are (Or you've already checked that), check if you're closing your connection anywhere.
That error can arise when you're trying to use a closed connection object.

 

I agree with justsomeguy, in regards to Prepared Statements. They're not too difficult, so give it a shot!

Link to comment
Share on other sites

Hello funce, thanks for some input.

Justsomeguy,

I downloaded what I have from a video tutorial. So I don't know about PDO, I'm just editing what there was there.

As for the paid host, there is no difference between the two, technical side I'm sure there is. But if the other free host worked to get a registration for the least, as well as my paid host, then why this problem on this free host. How can I be something wrong, there is no difference in the php, its all the same crap. As for the account registration validating stuff, I never got that far with that. Could be the code I am using just is too basic for anything really serious. But then again I doubt that was  really the problem even though you stated the way I was going about trying to get this tutorial to go one way and then hopefully another would add to it, and all that. I tried rereading the book I have, but I don't get it. I need a proper know how video tutorial, and there isn't anything like that yet..

I'm checking out this host since I can't keep continuing to have a paid host as much as I want to keep it. Free hosts are usually rubbish with their pop up ads, this how ever is nice. Just too nice to be true I guess.

May be I should contact the free web host try and see if they can check out their php setup.

Edited by Html
Added a little more info.
Link to comment
Share on other sites

But if the other free host worked to get a registration for the least, as well as my paid host, then why this problem on this free host.

There's no reason to guess when there are literally error messages just waiting to tell you specifically what the problem is.

How can I be something wrong, there is no difference in the php, its all the same crap.

You're running your crap on a different environment.  There are all kinds of differences.

I need a proper know how video tutorial, and there isn't anything like that yet.

That's correct.  Every video tutorial I've seen for programming is complete crap.

May be I should contact the free web host try and see if they can check out their php setup.

They're going to tell you that the server is fine and they don't debug people's code for free.  It's the programmer's responsibility to debug their own code.

Link to comment
Share on other sites

Well I've learnt something new then, so there setup is different, and the example code I have used since I got it, is poor quality.

Do you think you could tidy any of it? You don't assist for free obviously.

I'm going to have to may be call it quits if I can't go anywhere with this.

I did take a look at the php book again 207 sessions and cookies.

This was the tutorial I viewed in Feb, having learnt from the previous video I watched last year. I was able to edit some details. So the files are all there, if you wanted to may be make it work, the account creation system using what there is there, patch it up, I'd appreciate it. I want a comment box for each account once created to post to an index, as well the ability to upload an image. But I'd be happy with just the account creation working, so once registered the page works to login to what ever account is listed on the db.

files from the uploader via site

 

Edited by Html
Added some info and link
Link to comment
Share on other sites

That tutorial is a pretty good example of the kinds of things I've seen.  They give you everything, or tell you exactly what to type, and offer a brief and shallow explanation.  He's not teaching you why he did anything, he's only showing you what he did.  That's fine if you need to watch a video on how to change a light bulb, because you don't need to know how the light bulb works, but it doesn't work as well when you're trying to learn a craft or skill like programming.  I wouldn't hire someone to build a house who learned how to build houses by watching videos of what other people did, for example.  I would expect them to be formally taught or have learned through several years of apprenticeship, preferably both.

The code isn't the best, either.  He's still trying to escape values to prevent SQL injection, which should have been replaced around 13 years ago, he doesn't always check for database errors (only sometimes), and in the brief parts he showed he'll do things like use both isset and empty, which is redundant.  But even while he's doing that, he's still not telling you why he did that.  All together, it suggests that he doesn't have a great understanding of programming himself, and it's hard to teach anything to anyone if you don't really understand it yourself.  Unfortunately though that can be typical of programming tutorials, for whatever reason people want to try to teach things to others that they don't really understand themselves.  That's probably why you're not learning, because he's not teaching you why to do things, only showing you what he did.

I suspect the reason for publishing things like this is financial, those half a million views on that video have to count for something if he's spreading outdated and insecure code.

But, even so, I doubt that I'm going to be able to help you.  He gave you a working system, even though it has several problems, that needs to be edited in several places to get it to work right in a new environment.  You would get the same kind of thing from me, a bunch of files that you'll need to edit to get it to work in your environment.  That's still not going to teach you anything though, other than how to install a PHP application.  I don't have time to walk everyone though everything, I help people in writing, and if you can't understand things just by reading them then I'm not sure how to help you.  I can explain things to you but I can't understand them for you.

If you're dead-set on video tutorials and can't get anything from a book, then at least look for tutorials on the basics.  If you don't know how to program at all don't look for a tutorial on how to build an application, look for tutorials on how to process forms, or how to interact with a database, or how to use the session, or how to manage file uploads.  Or, even more basic, a tutorial on variables, and functions, and classes.  A half-hour video on an application that took several hours to write is going to leave a lot out.  A half-hour video on only how to process forms, or only how to interact with a database (using PDO, with prepared statements) or the other individual parts that you need to know, will go a lot farther to helping you understand how things work than someone just giving a summary of what they did to build an application.

Link to comment
Share on other sites

Hmm. That is quite a perspective on the subject in hand.

I don't know what to do really now. You state to try and do some small tutorials on processing data.

The books are so much the problem, but it is the way they express everything, in a video you have something visually displaying what is going on, even though as you've stated a lot of these aren't good quality. So in the end a video series is unlikely, unless somebody creates one for the right reason.

As for patching up the code there, well if you could in anyway get something going, but in the long run it won't serve much of anything. I could suggest the free host I use, may be you could tailor it to that. If you are up for it then may be. Otherwise forget it.

 

Thanks.

 

 

Link to comment
Share on other sites

I can't fix something if I don't know what the problem is, and I haven't seen the error messages saying what the problem is.  Literally the first step in debugging is getting the error messages, and we haven't gotten there yet.  So there's nothing that I can suggest.  It's like calling a mechanic and telling them something is wrong with your car but not giving any other details, they can't be expected to make any suggestions.

Link to comment
Share on other sites

Okay I decided to view the Learning php book,

<?php
//
$db= new PDO('sqlite:dinner.db')
//check if meals
$meals=array('breakfast','lunch','dinner')

if (in_array($_post['meal'], $meals)) {

$stmt = $db->prepare('select dish, price FROM meals WHERE meal LIKE ?');
$stmt->execute(array($_post['meal']));
$rows = $stmt->fetchAll();

if (count($rows) ==0) {
print "No dishes available.";
} else {
print '<table><tr><th>Dish</th><th>price</th></tr>';
foreach ($rows as $row) {
print "<table><tr><th>$row[0]</th><th>$row[1]</th></tr>";

}
print "</tables>";
  }
} else {
print "unknown meal.";
}
?>

This piece of code was form chapter one, which shows I guess, modern php on how to display from a db, that is what I wanted for displaying an account. or an index page of a registered name.

So, then on chapter 7, there is example code of validating data. May be I can use the new code and edit the existing examples I have from the tutorial.

 

 

Link to comment
Share on other sites

So clearly the code is the problem, some of it is out of date, not PDO or something. 🤔

I'm sure the existing stuff is okay to use, it needs tweaking, that is a start. You stated PDO in previous posts. There are two types of php interpretation from what I read in the php site pages the link above.

So index doesn't appear to be the problem, yet somehow with register and login, they are the problem. Login needs to display a email registered user, profile.php which then needs to have an index page which must show from the db like the name of the user.

The chapters in the book are heavy, and confusing.

You know you'd think better quality code on youtube would actually bring those users good revenue, now that would may be getting rich quick. I can't see how any short term php project would get anyone any finances, it can't work.

<?php
/* User login process, checks if user exists and password is correct */

// Escape email to protect against SQL injections
$email = $mysqli->escape_string($_POST['email']);
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'");

if ( $result->num_rows == 0 ){ // User doesn't exist
    $_SESSION['message'] = "User with that email doesn't exist!";
    header("location: error.php");
}
else { // User exists
    $user = $result->fetch_assoc();

    if ( password_verify($_POST['password'], $user['password']) ) {
        
        $_SESSION['email'] = $user['email'];
        $_SESSION['first_name'] = $user['first_name'];
        $_SESSION['last_name'] = $user['last_name'];
        $_SESSION['active'] = $user['active'];
        
        // This is how we'll know the user is logged in
        $_SESSION['logged_in'] = true;

        header("location: profile.php");
    }
    else {
        $_SESSION['message'] = "You have entered wrong password, try again!";
        header("location: error.php");
    }
}

May be this is missing or needs replacing.

if (in_array($_post['meal'], $meals)) {

$stmt = $db->prepare('select dish, price FROM meals WHERE meal LIKE ?');
$stmt->execute(array($_post['meal']));
$rows = $stmt->fetchAll();

 

Link to comment
Share on other sites

Again, $_post doesn't exist, you need to use $_POST to access post data.  

So index doesn't appear to be the problem, yet somehow with register and login, they are the problem. Login needs to display a email registered user, profile.php which then needs to have an index page which must show from the db like the name of the user.

A login page would just be used for logging in, to validate the login information and authenticating against the database and whatever else.  If someone successfully logs in, then after the login process you would redirect the user to wherever you want them to go after they log in.  Maybe you want to send them to the profile page, maybe you want to send them somewhere else, however you want it to work.  Again, if you want a profile page where anyone can look at anyone else's profile, then you need to tell the profile page which user to display, so you would pass the username or email address or whatever your unique user identifier is.  The profile page gets that username or email, looks up the user in the database, and displays whatever information from the database you want to show on the profile page.

In practice, I wouldn't use an email address as the only unique identifier for a user, because you'll show everyone's emails on the pages and those are going to be picked up by web scrapers to send spam to them.

Link to comment
Share on other sites

The code in the examples files from that video tutorial showed the id code.

$user_id="";

if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (isset($_GET["id"])) {
$user_id=$_GET["id"];
}

///process profile to id retrieved

}

So once registered via register.php, the profile.php page code kicks in, which any user would have same page, but unique to the user. It won't be register five accounts and they all have the same profile of one name, that wouldn't make sense.

<a href="profile.php?id=<?php echo $user_id; ?>"><button class="button button-block" name="View profile"/>View profile</button></a>

This link above from profile.php would send a user to their own index page.

<?php
/* Displays user information and some useful messages */
session_start();

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");    
}
else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
}

$user_id="";

if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (isset($_GET["id"])) {
$user_id=$_GET["id"];
}

///process profile to id retrieved

}

?>
<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Welcome <?= $first_name.' '.$last_name ?></title>
  <?php include 'css/css.html'; ?>
</head>

<body>
  <div class="form">

          <h1>Welcome</h1>
          <a href="profile.php?id=<?php echo $user_id; ?>"><button class="button button-block" name="View profile"/>View profile</button></a>
          <br>
          <p>
          <?php 
     
          // Display message about account verification link only once
          if ( isset($_SESSION['message']) )
          {
              echo $_SESSION['message'];
              
              // Don't annoy the user with more messages upon page refresh
              unset( $_SESSION['message'] );
          }
          
          ?>
          </p>
          
          <?php
          
          // Keep reminding the user this account is not active, until they activate
          if ( !$active ){
              echo
              '<div class="info">
              Account Created!
              </div>';
          }
          
          ?>

          <h2><?php echo $first_name.' '.$last_name; ?></h2>
          <p><?= $email ?></p>
          
          <a href="logout.php"><button class="button button-block" name="logout"/>Log Out</button></a>

    </div>
    
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>

</body>
</html>

 

Edited by Html
Link to comment
Share on other sites

That profile page is only going to show someone's own information, not any other user.  It gets the data from the session for whoever is logged in, and prints that information on the page.  That's fine if that's what you want.  If you want people to look at other peoples' pages then you need to look up that user data in the database.

Link to comment
Share on other sites

Right, you can do that.  You can build it however you want it to work.  You just need to have it do what you intend to do.  If you want everyone to only see their own profile, then use the session to fill out the profile.  If you want people to look at other peoples' profiles, then get the unique user identifier from the URL and look up their information in the database.  You can do whatever you want it to do.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...