Jump to content
Html

Table has error

Recommended Posts

I had a hunch you may end up stating the example code from the php 7 easy steps was going to be wrong to use as a replacement.

What we know is both code from the example files from the youtube channel are out of date, and now this code isn't workable as a replacement?

This below needs PDO code. This doesn't work on the free host, how ever it worked on the paid host, it did send details to a db, and logged in a name, but doesn't work is logging in again, but that is because it doesn't have a session data or something like.

I got no response from those two people I contacted, I just thought may be the code above could replace the problem I'm having on the free host if you see what I'm getting at.

I'm sure, I'm getting nowhere, and in circles, nearly year ago when I first tried this php stuff via youtube.

<?php
/* Registration process, inserts user info into the database 
   and sends account confirmation email message
 */

// Set session variables to be used on profile.php page
$_SESSION['email'] = $_POST['email'];
$_SESSION['first_name'] = $_POST['firstname'];
$_SESSION['last_name'] = $_POST['lastname'];

// Escape all $_POST variables to protect against SQL injections
$first_name = $mysqli->escape_string($_POST['firstname']);
$last_name = $mysqli->escape_string($_POST['lastname']);
$email = $mysqli->escape_string($_POST['email']);
$password = $mysqli->escape_string($_POST['password']);
      
// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
    
    $_SESSION['message'] = 'User with this email already exists!';
    header("location: error.php");
    
}
else { // Email doesn't already exist in a database, proceed...

    // active is 0 by DEFAULT (no need to include it here)
    $sql = "INSERT INTO users (first_name, last_name, email, password) " 
            . "VALUES ('$first_name','$last_name','$email','$password')";

    // Add user to the database
    if ( $mysqli->query($sql) ){

        $_SESSION['active'] = 0; //0 until user activates their account with verify.php
        $_SESSION['logged_in'] = true; // So we know the user has logged in
        $_SESSION['message'] =
                
                 "Confirmation link has been sent to $email, please verify
                 your account by clicking on the link in the message!";

        mail( $to, $subject, $message_body );

        header("location: profile.php"); 

    }

    else {
        $_SESSION['message'] = 'Registration failed!';
        header("location: error.php");
    }

}

 

Share this post


Link to post
Share on other sites

now this code isn't workable as a replacement?

If that book is telling you to use addslashes and then stick variables inside your SQL queries, then unfortunately the author skipped their database classes.  That specific code doesn't contain any features unique to PHP 7 either, it will run in PHP 5.

This below needs PDO code.

I agree.

This doesn't work on the free host

Be more specific, what specifically happened?  If you doubt that the host supports PDO at all, test only that:

<?php

if (class_exists('PDO')) {
  echo 'PDO is supported';
}
else {
  echo 'PDO is not supported';
}

I just thought may be the code above could replace the problem I'm having on the free host if you see what I'm getting at.

If you're going to solve a problem, the first step is to figure out what the specific problem is.  Just saying that it doesn't work does not indicate what the problem might be.

The code you posted above doesn't contain anything that actually connects to the database, so that's an obvious problem.

Share this post


Link to post
Share on other sites

Yes it does have a db.php Everything above works but only to register on the paid host and logged in once, you can't login again, just gives an error about no user being found, when the details are on the db, for what ever reason it wouldn't work on the free host, the admins on the support board stated there weeks back that the code looked dangerous.

Now whether it is or not, I'm using what there is to achieve a task of registering a name, and logging in with an email and a pass, that is the object here.

You've stated the example files are crap because they are out of date, but surely it can work.

Well the author of the book is Mike Mcgrath Php 7 learn in easy steps which is where the forum code is from. How can it not be php 7? Even that sarcastic impatient user supplied me with the source files which I got the forum working. You are obviously referring to that?

Having registered on the free host using the youtube files. register.php is what is in the link after index.php, after having registered. There is no report on the page, and no recorded info in the db. So every php environment is a little different, yet worked with the paid host to register and go from there to profile.php, and could log out.

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

I suspect the environment may be is blocking the code, because it is either poorly written or out of date.

As with the paid, I guess anything goes since it was paid.

Both the index page, and the forum use the same connect_db.php the difference is one posts to forum, and the other users.

Edited by Html

Share this post


Link to post
Share on other sites

Yes it does have a db.php

There's not a line in the code you posted which includes that file, I can only comment on code that I'm seeing.

for what ever reason it wouldn't work on the free host

It's not all that difficult to figure out the reason, it's just a process of narrowing it down and getting the code to tell you what it's doing.

You've stated the example files are crap because they are out of date, but surely it can work.

The two aren't mutually exclusive, plenty of things that work are also crap which should be replaced.  Or, more importantly to this discussion, they should not be learned at all when there is a better way.

How can it not be php 7?

It will run in PHP 7, I just said that it does not use any features which are exclusive to PHP 7.  It will also run in PHP 5.  Again, I'm only commenting on the code I'm seeing here.

I suspect the environment may be is blocking the code, because it is either poorly written or out of date.

I haven't heard of any PHP or other modules which will do a code inspection and refuse to run code that doesn't meet certain criteria.  It is possible to disable specific functions or classes in PHP, but that would produce an error message.  While it's fine to set display_errors and error_reporting on a page, that code will not be executed, and those options will not be set, at all if the code contains a syntax error.  You always need to understand what the default options are before you can change anything at runtime.  It might be configured to write error messages to an error log, and if that's the case you would see syntax errors in that log.  There is always a way to find an error message as long as they are not completely disabled, you just need to understand how everything is set up.  Basically, if you're assuming one environment is inspecting your code and refusing to run it without leaving any indication of that, that is not a correct assumption.

Share this post


Link to post
Share on other sites

I tried the error code on the register.php page, nothing occurs, and yes I am using a db.php for the examples files. I made sure to do so. The forum uses connect_db

Well the title states php 7, so may be some of the code isn't php 7 I would guess now.

 

Share this post


Link to post
Share on other sites

The only time I've ever seen PHP quit without an error message is when a core dump happens, and that's highly unlikely here.  I would bet $100 that either there is an error message just waiting for you to find it somewhere on the server, or the code is working correctly and just doesn't actually do anything.

Share this post


Link to post
Share on other sites

That registration code,  with

/* Registration process, inserts user info into the database 

is that all of it? IF it is! you are missing

<?php
session_start();

 

Share this post


Link to post
Share on other sites

 

<?php

session_start();

/* Registration process, inserts user info into the database
   and sends account confirmation email message
 */

// Set session variables to be used on profile.php page
$_SESSION['email'] = $_POST['email'];
$_SESSION['first_name'] = $_POST['firstname'];
$_SESSION['last_name'] = $_POST['lastname'];

// Escape all $_POST variables to protect against SQL injections
$first_name = $mysqli->escape_string($_POST['firstname']);
$last_name = $mysqli->escape_string($_POST['lastname']);
$email = $mysqli->escape_string($_POST['email']);
$password = $mysqli->escape_string($_POST['password']);
      
// Check if user with that email already exists
$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {
    
    $_SESSION['message'] = 'User with this email already exists!';
    header("location: error.php");
    
}
else { // Email doesn't already exist in a database, proceed...

    // active is 0 by DEFAULT (no need to include it here)
    $sql = "INSERT INTO users (first_name, last_name, email, password) "
            . "VALUES ('$first_name','$last_name','$email','$password')";

    // Add user to the database
    if ( $mysqli->query($sql) ){

        $_SESSION['active'] = 0; //0 until user activates their account with verify.php
        $_SESSION['logged_in'] = true; // So we know the user has logged in
        $_SESSION['message'] =
                
                 "Confirmation link has been sent to $email, please verify
                 your account by clicking on the link in the message!";

        mail( $to, $subject, $message_body );

        header("location: profile.php");

    }

    else {
        $_SESSION['message'] = 'Registration failed!';
        header("location: error.php");
    }

}

I can't imagine I'd of changed the code, since I know little about php. It does work to register and to load into profile, what doesn't work is to log back in after logging out. The code is out of date and so on, this has been stated here by yourselves.

But yes it doesn't on the free host, so no registering details to the db, or any loading into a profile.php So I can only think of the host having a setup which doesn't permit this.

Share this post


Link to post
Share on other sites

Yes! its old, but! once you have it working its just a matter of updating the code to PDO, PHP 7, coding

The registration page does not require session_start(); because that code is in the index page, where register.php is required() when it passes a specific condition as shown here

https://clevertechie.com/php/20/login-system-php-mysql-database

If you are running directly from register.php or login.php, they won't work as they should be run through the index.php page.

Share this post


Link to post
Share on other sites

Sure, that is what I mean, from index.php it then goes to register.php, on the free host it should then load to profile.php but it is just a blank page. It doesn't work, there are no error reports from register.php I included the error code in it.

And yes I would like to use what I have, but get it working as it should, I can then include a comment box and may be try and use the code from the php 7 easy steps, so I use a bit of this and that.

For the least I need to get this working on the free host, even I can't log back in, like it was on the paid host. The admins commented on the code looking dangerous whether it is or not, doesn't matter, it isn't a financial site or a shopping user site. I'm just trying to get something basic up and running, then be concerned about any security.

Share this post


Link to post
Share on other sites

Yes! but are you verifying the account like the tutorial does, by email, or just taking out those parts of the code hoping it will still run? The code won't let you go any further until the account is set to active, if you have taking out that part, it will never become active.

Share this post


Link to post
Share on other sites

Ah, so it isn't rubbish then, it was just me removing that, I removed it because I didn't have access to some kind of file to permit this sort of feature.

Can you assist me with trying to get this to work as I have it then?

Share this post


Link to post
Share on other sites

It is like you having a fully working car, removing and replacing parts with incompatible parts from another broken down car, ending up with two none working broken down cars.

IF you have all the relative code that allow account access if active, you can change active value to 1

Quote
hussam moussa تركي اليوسف a year ago

I think you need a areal website to get such email, so the localhost cant sent an email via the net.
Is that correct CleverTechie?
you can pass all of this by going to Mysql and edit the (active) column by putting the value (1) instead of (0)
so the user will be registered then

From https://clevertechie.com/php/20/login-system-php-mysql-database

Share this post


Link to post
Share on other sites

Hmm,

Now that I think of this, I recall tinkering with the php code of changing 0 to 1, as for the db may be, may be not now. That didn't solve the problem.

Bare in mind, on the free host this system doesn't work, so even if that could solve the logging in problem once registered, the free host won't accept the register.php.

Share this post


Link to post
Share on other sites

I honestly can't comment! I don't understand the difference between paid and free? You are hacking it seems, two forms of logging systems. You have made many mistakes in code, without actually understanding the code, you work blindly leaving out parts of code hoping it will work! my guess would be that the configuration is wrong somewhere, or a file is inaccessible because the path is wrong, or a feature like php email for instance, is not enabled, IF USED. Only you can gather this information from your web host to identify all of these are correct and enabled.

IF i was you, I would concentrate on the current logging in system, follow exactly code as is. It maybe out of date, but after you have the outdated version working, it would just a matter of updating it so it uses PDO.

I can't believe they would provide a non working code from a book! which this code is from, to only work on a paid or free host account, it just don't make sense.

Share this post


Link to post
Share on other sites

The examples I use are from that rubbish youtube clip that now charges to use the files for anyone who wants a copy of them, the forum is from the php 7 book. I hope to use that as a  comment system for a user profile, so you get my drift, use a bit of this, learn something, and get a result, doing anything further such as inbox feature for an account is another step isn't it.

As for the code, it worked on paid, but didn't on the free. I don't have access to a php.ini file or what ever the the file is to edit email sending.

Well then may be build a new register.php? How hard can this be, email pass, you know send off to a db, and then load into a profile page. 🤨

Share this post


Link to post
Share on other sites

I suggested to easysteps to create a book on PDO, php 7 and so on for a user system. Hopefully in time there will be one. The book series is nice, but I can't help but feel that may be just uploading all the code kind of defeats the purpose. But unfortunately some, they won't get anywhere without.

There is other code available, so I think I'm sorted with something basic and old for the moment. I'm going to try some of the files, and if any problems I will be posting back for hints.

 

Share this post


Link to post
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

×