Jump to content

[SOLVED] New to PHP! HELP! Connecting PHP to MySQL


ryanwood4

Recommended Posts

I have been set the task to create a login page/registration page. I have created a database named: MemberDirectory with two tables: Login and Membership.

 

I am using the host: HostJunkie.co.uk which provides an admin area with phpMyAdmin, and allows me to create databases quite easily. However after writing the code for the login and registration page, it seems the two won't connect (www.rawtees.co.uk/login.php) Anyone know why or do I need to post anything else to help, i.e. the PHP program code.

 

I new to all this, and would appreciate any help you could provide.

Link to comment
Share on other sites

This is the connection code: (note: the ?????? is sensitive information which has been removed!)

 

<?php

/* Program: Login.php

  Desc:    Login program for the members only section.

*/

session_start();

session_register('auth');

session_register('logname');

include("??????.inc");

switch (@$do)

{

  case "login":

    $connection = mysql_connect($host,$user,$password)

        or die ("Couldn't connect to server.");

    $db = mysql_select_db($database, $connection)

        or die ("Couldn't select database.");

        $sql = "SELECT loginName FROM Member

                WHERE loginNames='$fusername'";

        $result = mysql_query($sql)

              or die("Couldn't execute query.");

        $num = mysql_num_rows($result);

        if ($num == 1) // login name was found

        {

            $sql = "SELECT loginName FROM Member

            WHERE loginName='$fusername'

            AND password=password('$fpassword')";

      $result2 = mysql_query($sql)

            or die("Couldn't execute query.");

      $num2 = mysql_num_rows($result2);

      if ($num2 > 0) // password is correct

      {

 

Link to comment
Share on other sites

This is the entire code:

 

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
/* Program: Login.php
   Desc:    Login program for the members only section.
*/
session_start();
session_register('auth');
session_register('logname');
include("data.inc");
switch ($_GET['do']))
{
   case "login":
     $connection = mysql_connect($host,$user,$password)
         or die ("Couldn't connect to server.");
     $db = mysql_select_db($database, $connection)
         or die ("Couldn't select database.");
         $sql = "SELECT loginName FROM Member
                 WHERE loginNames='$fusername'";
         $result = mysql_query($sql)
               or die("Couldn't execute query.");
         $num = mysql_num_rows($result);
         if ($num == 1) // login name was found
         {
            $sql = "SELECT loginName FROM Member
            WHERE loginName='$fusername'
            AND password=password('$fpassword')";
       $result2 = mysql_query($sql)
            or die("Couldn't execute query.");
       $num2 = mysql_num_rows($result2);
       if ($num2 > 0) // password is correct
       {
         $auth="yes";
         $logname=$fusername;
         $today = date("Y-m-d h:m:s");  
         $sql = "INSERT INTO LOGIN (loginName, loginTime)
            VALUES ('$logname','$today')";
         mysql_query($sql) or die("Can't execute query.");
         header("Location: Member_page.php");
         }
         else     //password is not correct
         {
           unset($do);
           $message="The Login Name, '$fusername' exists,
                      but you have not entered the correct 
                      password! Please try again.<br>";
           include("login_form.inc");
         }
       }
       elseif ($num == 0) // login name not found
       {
          unset($do);
          $message = "The Login Name you entered does not
                      exist! Please try again.<br>";   
          include("login_form.inc");
       }
    break;
    
    case "new";
      foreach($HTTP_POST_VARS as $key => $value)
      {
        if ($key != "fax")
        {
          if ($value == "")
          {
          unset($do);
          $message_new = "Required information is missing.
             Please try again.";
          include("login_form.inc");
          exit();
        }
      }
      if (ereg("{Name)",$key))
      {
       if (!ereg("^[A-Za-z' -]{1,50}$",$key))
       {
         unset($do);
         $message_new = "$lastName is not valid name.
                           Please try again.";
         include("login_form.inc");
         exit();
       }
      }
      $$key = strip_tags(trim($value));
    }
    if (!ereg("^.+@.+\\..+$",$email))  
    {
      unset($do);
      $message_new = "$email_address is not a valid email address.
               Please try again.";
      include("login_form.inc");
      exit();
   }
   /* check to see if the login name already exists */
   $connection = mysql_connect($host, $user, $password)
       or die ("Couldn't connect to server.");
   $db = mysql_select_db($database, $connection)
       or die ("Couldn't select database.");
   $sql = "SELECT loginName FROM Member
       WHERE loginName='$newname'";
   $result = mysql_query($sql)
       or die("Couldn't execute query.");
   $num = mysql_numrows($result);
   if ($num > 0)
   {
     unset($do);
     $message_new = "$newname already used. Select another 
                     Member ID.";
     include("login_form.inc");
     exit();
   }
   else
   {
     $today = time("Y-m-d");
     $sql = "INSERT INTO Member (loginName,createdate,password,first_name,second_name,description) VALUES
           ('$newname','$today',password('$newpass'),
           '$first_name', '$second_name', '$email_address', '$description')";
     mysql_query($sql);
     $auth="yes";
     $logname = $newname;
     /* Send email to new member */
     $emess1="A new member account has been setup for you. ";
     $emess2="Your new member ID and password are: ";
     $emess3="\n\n\t$newname\n\t$newpass\n\n";
     $emess4="We appreciate your membership ";
     $emess5="at RawTees.co.uk\n\n";
     $emess6="If you have any queries or questions please email: ";
     $emess7="ryan.wood@rawtees.co.uk";
     $emess = 
        $emess1.$emess2.$emess3.$emess4.$emess5.$emess6.$emess7;
     $ehead="From: membership@rawtees.co.uk\r\n";
     $subject = "Your new member account at RawTees.";
     $mailsend=mail("$email_address","$subject","$emess","$ehead");
     header("Location: new_member.php");            
   }
break;

default:
     include("login_form.inc");
  }
?>                                                                                                                                        

Link to comment
Share on other sites

Ok, thanks so much for your help, your help has sorted it out now.

 

One more thing that now pops up is this: Notice: Undefined variable: fusername in /home/rawtees/public_html/login.php on line 19

Couldn't execute query.

 

Any suggestions.

 

Line 19 is

WHERE loginName='$fusername'";

Link to comment
Share on other sites

Change

$sql = "SELECT loginName FROM Member
         WHERE loginNames='$fusername'";

 

to

 

$fusername = mysql_real_escape_string($_POST['fusername']);
$sql = "SELECT loginName FROM Member
         WHERE loginNames='$fusername'";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.