I have a 'login script' for users to register and log in, etc. My website is hosted by a good friend who has setup a MySQL database for me on his server(located at his house). I have no problems connecting to the MySQL server and it seems to work ok, except...
At the moment I can register a new user and it says the user is saved into the database, but it actually isn't putting anything into the fields from what I assume. When I go to log in it, error message 'username cannot be found' is posted. The lineage of the code seems to correct through the whole process, but doesn't add a user although it says it does.
Is the database set up correctly on his end or is this a fault in my code somewhere?
Although I am fairly new to PHP, I understand the logic very well as the code is easy to read and understand as well as calls, commands, etc. But I do not understand MySQL(I have installed 5.1.33 on my Mac) very well. I have used my Terminal extensively using help from others around the internet to access the server, add a database myself, etc, but I still have no idea where it would be located, or how to view the tables once info has actually been placed in them or even really how to use MySQL.
Please help me. Thanks in advance for any input
* if you want my dbtable.sql or and .php code, let me know
Check your connection script, to make sure that you're connecting to the right database. (You said you installed MySQL on your local machine, so maybe you're writing to the local database and not the server's database).
If your SQL statement (INSERT xxx INTO xxxx etc. etc.) gives you no errors when your register a user, then something must be happening.
We'll need to see some source code to help further.
If any other code is needed, let me know. Again, registration works. No return false, etc. And the problem may be as simple as changing the DB constants or tables. I'm just not sure.
For example, the first field in the table should be called username (or something like that) and be of type varchar. If the fields are not in the right order or of the wrong type the insert wont work.
Have you tried echoing the $retval to the screen to see if the insert was successful or what the failure could be?
I haven't tried echoing anything to see if it was inserting correctly, but I will try that. Didn't think to do that.
As for the database.... the dbtables.sql has it in my post above with the code. It has username first and is a type of varchar, then password also a varchar, then userid, etc. I do believe the order is correct in my users table (viewable above). But again, not 100% sure. If more code is needed, I could just the whole system.
...and it will print the $retval to screen. The page will probably throw errors after that but it doesn't matter, we're only interested in $retval for now. You can remove it after you know the value.
Is there a different way to write to the database? I have these commands but figure there must a better way to write to the database.
process.php (this is called first when registration form is submitted):
Code:
function procRegister(){
global $session, $form;
/* Convert username to all lowercase (by option) */
if(ALL_LOWERCASE){
$_POST['user'] = strtolower($_POST['user']);
}
/* Registration attempt */
$retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['company']);
from it goes to session.php:
Code:
function register($subuser, $subpass, $subcompany, $subemail){
global $database, $form, $mailer; //The database, form and mailer object
/* Username error checking */
$field = "user"; //Use field name for username
if(!$subuser || strlen($subuser = trim($subuser)) == 0){
$form->setError($field, "* Username not entered");
}
else{
/* Spruce up username, check length */
$subuser = stripslashes($subuser);
if(strlen($subuser) < 5){
$form->setError($field, "* Username below 5 characters");
}
else if(strlen($subuser) > 30){
$form->setError($field, "* Username above 30 characters");
}
/* Check if username is not alphanumeric */
else if(!eregi("^([0-9a-z])+$", $subuser)){
$form->setError($field, "* Username not alphanumeric");
}
/* Check if username is reserved */
else if(strcasecmp($subuser, GUEST_NAME) == 0){
$form->setError($field, "* Username reserved word");
}
/* Check if username is already in use */
else if($database->usernameTaken($subuser)){
$form->setError($field, "* Username already in use");
}
/* Check if username is banned */
else if($database->usernameBanned($subuser)){
$form->setError($field, "* Username banned");
}
}
/* Password error checking */
$field = "pass"; //Use field name for password
if(!$subpass){
$form->setError($field, "* Password not entered");
}
else{
/* Spruce up password and check length*/
$subpass = stripslashes($subpass);
if(strlen($subpass) < 4){
$form->setError($field, "* Password too short");
}
/* Check if password is not alphanumeric */
else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
$form->setError($field, "* Password not alphanumeric");
}
/**
* Note: I trimmed the password only after I checked the length
* because if you fill the password field up with spaces
* it looks like a lot more characters than 4, so it looks
* kind of stupid to report "password too short".
*/
}
/* Email error checking */
$field = "email"; //Use field name for email
if(!$subemail || strlen($subemail = trim($subemail)) == 0){
$form->setError($field, "* Email not entered");
}
else{
/* Check if valid email address */
$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
."\.([a-z]{2,}){1}$";
if(!eregi($regex,$subemail)){
$form->setError($field, "* Email invalid");
}
$subemail = stripslashes($subemail);
}
/* Errors exist, have user correct them */
if($form->num_errors > 0){
return 1; //Errors with form
}
/* No errors, add the new account to the */
else{
if($database->addNewUser($subuser, md5($subpass), $subcompany, $subemail)){
if(EMAIL_WELCOME){
$mailer->sendWelcome($subuser,$subemail,$subpass);
}
$retval="0"; //New user added succesfully(I have used return true here instead with no change in results)
}else{
$retval="2"; //Registration attempt failed(I have used return false as well)
}
}
}
and from there it goes to database.php
Code:
function addNewUser($username, $password, $company, $email){
$time = time();
/* If admin sign up, give admin user level */
if(strcasecmp($username, ADMIN_NAME) == 0){
$ulevel = ADMIN_LEVEL;
}else{
$ulevel = USER_LEVEL;
}
$q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '$company', '0', $ulevel, '$email', $time)";
return mysql_query($q, $this->connection);
}
and finally the dbtables.sql:
Code:
DROP TABLE IF EXISTS users;
CREATE TABLE users (
username varchar(30) primary key,
password varchar(32),
userid varchar(32),
userlevel tinyint(1) unsigned not null,
email varchar(50),
timestamp int(11) unsigned not null,
company varchar(30)
);
I am sure now that it is not actually posting anything into the database fields because I can attempt to register the same user info over and over without an error. (it does check if the user is already registered)
I do find it interesting that in sessions.php the use of $subuser, $subpass, $subemail, $subcompany is implemented. Why would that be? Should I use $username, $password, $email, $company?