Current location: Hot Scripts Forums » Programming Languages » PHP » MySQL help please (simple questions)


MySQL help please (simple questions)

Reply
  #1 (permalink)  
Old 04-28-09, 07:48 PM
bluedogsb bluedogsb is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL help please (simple questions)

So here's what's up:

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

Last edited by bluedogsb; 04-28-09 at 08:02 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 04-29-09, 09:29 AM
therocket954's Avatar
therocket954 therocket954 is offline
Community Liaison
 
Join Date: Jul 2007
Location: Michigan, USA
Posts: 334
Thanks: 2
Thanked 8 Times in 8 Posts
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.
__________________
--Eric Allison
Twitter: http://www.twitter.com/Eric_Allison
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 04-29-09, 11:02 AM
bluedogsb bluedogsb is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
So here's my database areas of adding a new user as well as my constants for the database connection:

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)
);
constants.php :
Code:
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "elite1baller");
define("DB_NAME", "sanderblue");

define("TBL_USERS", "users");
define("TBL_ACTIVE_USERS",  "active_users");
define("TBL_ACTIVE_GUESTS", "active_guests");
Is localhost wrong here?

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']);
      
      /* Registration Successful */
      if($retval == 0){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = true;
         header("Location: ".$session->referrer);
      }
      /* Error found with form */
      else if($retval == 1){
         $_SESSION['value_array'] = $_POST;
         $_SESSION['error_array'] = $form->getErrorArray();
         header("Location: ".$session->referrer);
      }
      /* Registration attempt failed */
      else if($retval == 2){
         $_SESSION['reguname'] = $_POST['user'];
         $_SESSION['regsuccess'] = false;
         header("Location: ".$session->referrer);
      }
   }
database.php :

Code:
function addNewUser($username, $password, $email, $company){
      $time = time();
    
      if(strcasecmp($username, ADMIN_NAME) == 0){
         $ulevel = ADMIN_LEVEL;
      }else{
         $ulevel = USER_LEVEL;
      }
      $q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '$email', '0', $ulevel, $time, '$company')";
      return mysql_query($q, $this->connection);
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.

Thx,
Blue

Last edited by bluedogsb; 04-29-09 at 11:04 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 04-29-09, 02:16 PM
outhowz42 outhowz42 is offline
Newbie Coder
 
Join Date: Jun 2008
Location: USA
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Does your database have these fields in this order and of the correct type.

'$username', '$password', '$email', '0', $ulevel, $time, '$company'

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?
__________________
www.sportsloon.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 04-29-09, 06:11 PM
bluedogsb bluedogsb is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 04-29-09, 08:14 PM
outhowz42 outhowz42 is offline
Newbie Coder
 
Join Date: Jun 2008
Location: USA
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Echo $retval to the screen

I am betting it is a 2.
__________________
www.sportsloon.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 04-30-09, 12:18 AM
bluedogsb bluedogsb is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
I understand the echo command but not how to execute what you are telling me to do. Is $retval failing?

Are you looking for something like: echo "$retval" (I'm not sure how to link it)

Maybe something more along:

echo "<b>DATABASE:</b> ".$process->procRegister($retval)."";
echo "<b>DATABASE:</b> ".$database->addNewUser()."";

Again, I am a php newb.

Last edited by bluedogsb; 04-30-09 at 12:38 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 04-30-09, 09:44 AM
outhowz42 outhowz42 is offline
Newbie Coder
 
Join Date: Jun 2008
Location: USA
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Right after this line in the procRegister function:

* Registration attempt */
$retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['company']);


just do this

echo 'Return Value: '.$retval;

...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.
__________________
www.sportsloon.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 04-30-09, 01:29 PM
bluedogsb bluedogsb is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
The return value is blank, no number or anything. Just pops up with "Return Value: " but nothing after.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 05-01-09, 05:51 PM
bluedogsb bluedogsb is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
MySQL to MSSQL questions phpdoctor Database 4 12-05-07 02:13 PM
MySQL Questions KeYBLeR PHP 4 04-10-06 03:30 PM
Two very simple questions xxvatarxx PHP 1 12-04-05 01:48 PM
Several questions for PHP and MySql starting in Windows XP artaco2000 PHP 4 10-14-04 01:25 PM


All times are GMT -5. The time now is 01:56 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.