+ Reply to Thread
Results 1 to 4 of 4

Thread: Problem with php. Connecting to a database. Easy fix?

  1. #1
    garrette is offline x10Hosting Member garrette is an unknown quantity at this point
    Join Date
    Jan 2012
    Posts
    27

    Problem with php. Connecting to a database. Easy fix?

    PHP Code:
    <?php
    $username 
    $_POST['username'];
    $password $_POST['password'];

    if (
    $username&&$password)
    {
    $connect mysql_connect('localhost',"******","******") or die ("Couldn't connect!");
    mysql_select_db('garrette_login') or die("Couldn't Log In!");
    is the code. I have a system in which the user inputs a username and password in the field, and it's supposed to connect to my database if both fields have characters, and the user clicked submit. However, I get the error message which states "Couldn't Log In!". Anyone know why? I can't figure it out. Am I using a wrong command? I am still fairly new to php, so any help would be appreciated!


    I fixed that problem, turns out that I hadn't added the account to the database -.- but I have another problem, and I don't want to fill the threads with all my problems :/

    This one has stumped me. Right now I am trying to create a "members" only part of my site. Here is the full code, sorry it's a bit longish.

    PHP Code:
    <?php

    session_start
    ();

    if (
    $_SESSION['username'])
    { print(
    "

    <HTML>

    <HEAD>

    <META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">

    <TITLE> Friend Zone</TITLE>

    <link HREF=\"friendzonestylesheet.css\" rel=\"stylesheet\" type=\"text/css\" />

    </HEAD>

    <BODY>
    <div id=\"container\">


    <div id=\"header\">
    <IMG SRC=\"FriendZoneheader.jpg\" HEIGHT=\"100\" WIDTH=\"100%\">
    </div>

    <div id=\"maincontainer\">
    <div id=\"sidebar\">
    <Center> <H2> Site Menu </H2> </center>
    <p>All site menu options are hidden from Non-Users. Log in to continue </p>
    <p><IMG SRC=\"friendzone.jpg\" ALT=\"Turco\" width=\"180\" height=\"282\" /></p>
    </div>

    <div id=\"content\">

    <h1>What is <I> Friend Zone</I>? </H1> <p>A future alternative to social networking
    sites such as facebook and myspace. This site will have similar features, but without all the
    useless updates and hopefully faster loading times. When completed, this site will feature 
    many useful functions, but also maintain simplicity. </p>

    <p class=\"style2\"> I am working on this by myself, so this may take a while. </p>

    <p> Below I will allow some users to register to try this site ahead of time. Only a 
    small amount will be allowed, because I can't manage all the information at this stage 
    of development. If interested, fill in the fields below. </p>

    <p class=\"style1\">(Only works if I have you inserted into the database) </p>

    <form action='login.php' method='POST'>
            Username:<input type='text' name='username'><br>
            Password:<input type='password' name='password'><br>
            <input type='submit' value='Log in'>

    </form>




    </div>


    </div>

    <div id=\"footer\">
    &copy; 2012 Garrett Emrick
    </div>

    </BODY>

    </HTML>"
    );


    }
    else
       die(
    "You must be logged in to view this page!");

    ?>
    .

    Now I believe the problem may be that I need to use "print" more often throughout this code, but I have no idea how to incorporate html with php. . . as in I don't know how I would include all this code. I would love it if someone would show a few examples of where or what I should do, or know a resource I could use to help me.

    Wow. I am such a noob at this. I realized what I did wrong. I originally had this as a html file. Then I added the PHP so I could restrict non-logged in users from accessing it. However, when I did this I needed to also switch the file from an html, to a php. This is a fix for me, but if anyone with more knowledge than me thinks this is a wrong way to do it, please say so. I spent so long trying to fix this, and after posting it I immediately realized what I did. Maybe someone else can learn from my mistake? Haha.
    Last edited by garrette; 01-05-2012 at 11:42 AM. Reason: Old problem fixed, new problem

  2. #2
    ellescuba27 is online now x10 Sophmore ellescuba27 is an unknown quantity at this point
    Join Date
    Sep 2011
    Posts
    151

    Re: Problem with php. Connecting to a database. Easy fix?

    Correct me if I'm wrong, but shouldn't print() and all the text inside be on one line with \n for any newlines?

  3. #3
    garrette is offline x10Hosting Member garrette is an unknown quantity at this point
    Join Date
    Jan 2012
    Posts
    27

    Re: Problem with php. Connecting to a database. Easy fix?

    Well it may not be "proper" coding structure, but the webpage displays fine. It works this way, I'm sure it could work another as well.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Problem with php. Connecting to a database. Easy fix?

    Quote Originally Posted by garrette View Post
    I fixed that problem, turns out that I hadn't added the account to the database -.- but I have another problem, and I don't want to fill the threads with all my problems :/
    A new thread would be better to keep the scope of each more specific. For one thing, someone searching for a solution to one problem won't be interested in the other problems; they'll just be noise.

    Don't use die when outputting HTML. You'll get invalid HTML.

    Instead of using print, echo or some other PHP construct, simply switch from PHP to HTML:

    PHP Code:
    <?php
    // init.php will start session, along with any other initialization.
    include_once('init.php');
    // User class handles user identity. A separate class would handle authentication.
    $user User::current();
    // Authorizer class should tell whether a given agent has access to a given resource
    $authorizer Authorizer::getDefault();

    ?><!DOCTYPE ...
    <html>
      <head>
          ...
      </head>
      <body>
        <?php if ($authorizer->userIsAuthorized($user$_SERVER['REQUEST_URI'])): ?>
          <!-- page content -->
          ...
        <?php else: ?>
          <p class="error">You must be logged in to view this page.</p>
          <!-- include login form, or link to login page. -->
        <?php endif; ?>
      </body>
    </html>
    You could use the same script for all protected content by redirecting appropriate requests with the server's rewrite engine to the script, which would load the appropriate content where the example says "<!-- page content -->".

    All HTML documents should start with a doctype.

    Instead of learning this stuff willy-nilly in forum posts, find good books on programming in PHP and web development. Note: programming is only a part of development; there is much more you should learn.



    Quote Originally Posted by ellescuba27 View Post
    Correct me if I'm wrong, but shouldn't print() and all the text inside be on one line with \n for any newlines?
    Unlike many other languages, PHP supports embedded newlines in strings. However, heredoc strings are usually more readable than double-quoted strings when there are more than a few embedded newlines.
    Last edited by misson; 01-06-2012 at 02:28 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Problem connecting to database
    By jnoalexander in forum Free Hosting
    Replies: 4
    Last Post: 08-05-2011, 03:20 PM
  2. Problem with connecting to MySql database
    By evgeny.lychev90 in forum Free Hosting
    Replies: 1
    Last Post: 01-19-2011, 08:07 AM
  3. problem to connecting your mysql database
    By muthu18 in forum Free Hosting
    Replies: 1
    Last Post: 03-24-2010, 05:25 AM
  4. Replies: 9
    Last Post: 02-18-2010, 07:34 PM
  5. ASP.NET - Problem connecting to database
    By machado in forum Free Hosting
    Replies: 2
    Last Post: 10-18-2009, 10:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers