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

garrette

New Member
Messages
27
Reaction score
0
Points
0
PHP:
<?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:
<?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:

ellescuba27

Member
Messages
273
Reaction score
3
Points
18
Correct me if I'm wrong, but shouldn't print() and all the text inside be on one line with \n for any newlines?
 

garrette

New Member
Messages
27
Reaction score
0
Points
0
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.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:
<?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.



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:
Top