Jump to content

PHP Register & Login application error


cardwell164

Recommended Posts

hi guys. im a newbie in this forum and im glad that i found this. anyway, i have a problem with my code. i am creating a register and login application using php. however its not passing the data properly. i have tried to echo it and still its not showing. can anybody help me on this please? any assistance would be greatly appreciated  :-*

 

here is my code for my init.php

<?php
session_start();

require 'database/connect.php';
require 'functions/general.php';
require 'functions/users.php';

if (logged_in() == true)
{
	$session_user_id = $_SESSION['userid'];
	$user_data = user_data($session_user_id, 'userid', 'username', 'password', 'firstname', 'lastname', 'email');
}

$errors = array();
?>

and this is my users.php which i think where lies the problem. i will just include the code of the function that creates the error.

<?php

function user_data($userid)
{
	$data = array();
	$userid = (int)$userid;

	$func_num_args = func_num_args();
	$func_get_args = func_get_args();

	if($func_num_args > 0)
	{
		unset($func_get_args[0]);
		$fields = '`' . implode('', $func_get_args) . '`';
		$data = mysql_fetch_assoc(mysql_query("SELECT '$fields' FROM users WHERE userid = '$userid'"));

		print_r($data);

		return $data;
	}
}

?>

supposedly it should print the query stored in $data however its not doing that :(

 

Link to comment
Share on other sites

your query is failing due to a sql syntax error. the fields listed in the SELECT list should not be enclosed in single-quotes. also, your $userid value is an integer, it shouldn't be enclosed by single-quotes in the query.

 

so, two things -

 

1) you need set php's error_reporting to E_ALL and display_errors to ON in your php.ini (the best place) or in your script, to get php to help you by reporting and displaying all the errors it detects.

 

2) you need to ALWAYS have error checking logic in your code to test if a step worked or not before trying to use the result you expect from that step, i.e. don't nest functions like mysql_fetch_assoc(mysql_query(" ...")), where the inner function can fail due to an error. putting error checking logic in your code makes your code self troubleshooting, your code will tell you when and why it is failing, no guessing is needed.

Link to comment
Share on other sites

Change the user_data function to

function user_data($userid)
{
	$data = array();
	$userid = (int)$userid;

	$func_num_args = func_num_args();
	$func_get_args = func_get_args();

	if($func_num_args > 0)
	{
		unset($func_get_args[0]);
		$fields = '`' . implode('', $func_get_args) . '`';
		// check the query executed. mysql_query returns false if there is an error
		if(($result = mysql_query("SELECT '$fields' FROM users WHERE userid = '$userid'")) !== false)
		{
			// check that the query did actually return any results
			if(mysql_num_rows($result))
			{
				return mysql_fetch_assoc($result); // return the result
			}
			// query didn't return any results
			else
			{
				echo 'user_data() query returned no results!';
			}
		}
		// qeuery has failed find out why using msyql_error();
		else
		{
			echo 'user_data() query has failed - ' . mysql_error();
		}
	}
	return false;
}

It should echo out an error message about why the user_data() function is not returning any data.

Link to comment
Share on other sites

thanks again for the help sir. that part is fixed now. however i have encountered another problem its now saying:

 

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\registerlogin\core\functions\users.php on line 34

 

i really dont know why its saying that. i have double checked that function and i dont see anything wrong there. i hope you will still help me. anyway here is my code

function user_exists($username)
{
	$username = sanitize($username);
	$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
	return (mysql_result($query, 0) == 1) ? true : false;
}

could it be something wrong with my apache version?

Link to comment
Share on other sites

 

 

could it be something wrong with my apache version?

Nothing to do with Apache. it is just that phpacademy has coded the login/register script poorly. This is why you are getting so many errors.

 

You need to check that the query has returned a result before you call mysql_result(). Change the function to the following to see what could be wrong

function user_exists($username)
{
    $username = sanitize($username);
    if($result = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"))
    {
        if(mysql_num_rows($result))
          return (mysql_result($result, 0) == 1) ? true : false;
        else
          echo 'user_exists() query returned no results!';
    }
    else
      echo 'user_exists() query return: ' . mysql_error();
}
Link to comment
Share on other sites

What does my code output when you run it? Just because the query appears to be formatted correctly when you echo it does not mean there is nothing wrong with it. 

 

When executing query's you need to check that it executed and that it returned any results. You cannot always assume a query will execute OK.

Link to comment
Share on other sites

 

Nothing to do with Apache. it is just that phpacademy has coded the login/register script poorly. This is why you are getting so many errors.

 

You need to check that the query has returned a result before you call mysql_result(). Change the function to the following to see what could be wrong

function user_exists($username)
{
    $username = sanitize($username);
    if($result = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"))
    {
        if(mysql_num_rows($result))
          return (mysql_result($result, 0) == 1) ? true : false;
        else
          echo 'user_exists() query returned no results!';
    }
    else
      echo 'user_exists() query return: ' . mysql_error();
}

you dont know how thankful i am to you sir. i just solved my problem. i do agree with you that phpacademy's coding style is coded somewhat poorly. however its the best resource i got that gets things explained very well. i have to hand it to alex though, he explains things very well and always says in the end why this particular function/technique etc. is useful. are there any tutorials that you can recommend?

Link to comment
Share on other sites

programming requires more than following along with some code in a tutorial you have found. you must actually understand what the code means and how it contributes to the goal you are trying to achieve. in your latest problem, you probably found you had an incorrect column name in the query. it's your responsibility to keep track what's going on in your code/variables/queries.

 

the code that Ch0cu3r has given you, twice in this thread, is called error checking logic. you need to ALWAYS test if a step that can fail due to an error has worked or not before trying to use the result from that step.

 

if you ALWAYS use error checking logic in your code, your code will help you troubleshoot, because your code will tell you if and why it failed.

Link to comment
Share on other sites

it was a stupid mistake sir. in my connect.php, i called the wrong database name. instead of registerlogin, i had it loginregister. it turned out i was barking at the wrong tree. i wasnt able to include the die function because the tutorial didnt include it. anyhow, your help was the best!

Link to comment
Share on other sites

programming requires more than following along with some code in a tutorial you have found. you must actually understand what the code means and how it contributes to the goal you are trying to achieve. in your latest problem, you probably found you had an incorrect column name in the query. it's your responsibility to keep track what's going on in your code/variables/queries.

 

the code that Ch0cu3r has given you, twice in this thread, is called error checking logic. you need to ALWAYS test if a step that can fail due to an error has worked or not before trying to use the result from that step.

 

if you ALWAYS use error checking logic in your code, your code will help you troubleshoot, because your code will tell you if and why it failed.

thanks for the advice sir. i will take note of that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.