Jump to content
xisto Community

My Sql Database Help?

Recommended Posts

Ok, i am new at web design, i dont know too much about it... I was working on a website for about a month this is what i got, My Web , like i said, i dont know much.. i used FrontPage and did all of that, my current host is AFMU which uses MySQL, but the way it is set up I dont know how to acquire the URL of the database or any information i dont know what it means at all.. i just want to do a simple login feature, i mean this is my first website and i am learning so I think it would be a great feature to add in and learn. i can create data bases from there but do not know what to do afterwards, example: I Create a database name and a user this is what it leaves me with; NOTE! I did not change ANYTHING listed below, this is EXACTLY what it created for me, now that i got that off my chest, here it is:

Current Databases: deatncom_logindb   Users in logindbdeatncom_shnabo (Privileges: ALL PRIVILEGES) Connection Strings Perl $dbh = DBI->connect("DBI:mysql:deatncom_logindb:localhost","deatncom_shnabo","<PASSWORD HERE>"); PHP $dbh=mysql_connect ("localhost", "deatncom_shnabo", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("deatncom_logindb");

Could some on please tell me how i use this information??? please..


I appreciate the time you took to read this, and if you know what to do please post, thank you very much

Edited by shnabo11 (see edit history)

Share this post

Link to post
Share on other sites

In order to connect a web site with a MySQL database, you will need to know more about web development than just using Microsoft FrontPage - you will also need to learn PHP (or ASP, but the former is a better option). However, prior to doing so, it would also be a good idea to start dealing with HTML code instead of using a WYSIWYG (What You See Is What You Get) editor (e.g. FrontPage, Dreamweaver). Be aware that PHP isn't easy, and that it will take at least a month of regular practice for you to be able to create a hack-proof and secure login system.

If all this looks like a too big step to you, there is always the option of using a CMS (Content Management System) for your web site. These are great because they offer easy content management along with a user-friendly interface, as well as a registrations system (in most cases). I must warn you, though, that this option too requires a little HTML/PHP knowledge in order to use it to its full potential. Even if you do decide on the first one, examining how a certain CMS works is a great way of learning new things. Or at least that is my experience :blink:

Share this post

Link to post
Share on other sites

Pyost is right--you definitely need more than just FrontPage.


Okay, if you've already created a database, then just paste the code that was given to you in a file. I'll go with PHP for now:


PHP $dbh=mysql_connect ("localhost", "deatncom_shnabo", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());mysql_select_db ("deatncom_logindb");
Paste this in a .php file and replace <PASSWORD HERE> with the password you specified while creating the database. Once you run the file, the database connection will be opened. Also remember to put <?php and ?> around the PHP code.


Then, afterwards, you want to create a table in the database to hold your login information. (Use PhpMyAdmin if you're using MySQL) You could go with three fields (username, password, email, id) for a very very simple user registration. For the username, password, and email you should leave the field type as varchar with a length of your choice. For the id, use a type of int and set it as a primary key. Also, under extras, set the id to 'auto_increment'. The id is mostly for internal tracking purposes and won't be of much use to the end-user.


Under the different options, you shouldn't need to touch collation, attributes, and null. Default you can set if you want there to be a default value for each field. (Not really applicable in your situation, I think)


Clicking save should also generate PHP code that can be used to create the exact same table if you paste it in a php file.


Then, in order to access the table, you should use mysql. First you'd want to create a registration page (let's say called register.php). In it you'd have:


<form action="register.php" method="post"><label for="username">Username: </label><input type="text" name="firstname"><br /><label for="password">Password: </label><input type="password" name="password"><br /><label for="email">Email: </label><input type="text" name="email"><br /></form>

As a side note, I typed this in the browser, so there's no guarantee it'll work right off the bat. Now, to break apart the tags within <form></form>...


[a] the action attribute within <form> -- this is where the form directs to, and it's also where you want to put the php code to execute data received from the form

the method attribute within <form> -- this tells the browser how to send the information. It can either be POST or GET. If it's GET, the data will appear in a website's url--ex: http://forums.xisto.com/no_longer_exists/ If it's POST, the data is hidden. For something sensitive like registration or logging in, POST should always be the one used

[c] the name attribute in the input tag -- this is the name that's used by PHP to access the data--you'll see later.


Moving on...


Now, to access the form data that a user has entered, in register.php we'd want to have:


<?php$username = $_POST["username"];$email= $_POST["email"];$password= $_POST["password"];?>

Suppose the user entered helpless as the username, iowf83M as the password and weoiwho@gmail.com as the email. Then the variables $username, $email, and $password should contain the respective data. The array variable $_POST is defined by PHP to access data from a form with a method of POST. The ["username"] part of the variable is the value defined in name in the input tag.


Before you do anything with the password, you should md5 has it for security purposes. Ex:


<?php$password = md5($password);?>
md5 is a (nice) built-in function in php that does what it says.


Once you've got the data within variables and hashed accordingly, you'd want to use this to insert the data into the database:


<?phpmysql_query("INSERT INTO <table_name> (username, password, email) VALUES ('$username', '$password', '$email')");?>

To break the mysql query down, INSERT INTO is very obvious. Replace <table_name> with the name of your table. (Most likely users or something like that). The stuff between the parentheses are the fields you want to insert into--username, password and email. The values correspond to their respective fields.


Now that the data is in the database, to retrieve it, we use another mysql query.

$username = $_POST["username"];$email= $_POST["email"];$password= $_POST["password"];$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");

The select query gets the user where username is the username entered and password is the password entered. If $result exists, then the user can be logged in.


This is the most basic aspects of a login system, and it's not even fully complete yet. For a secure login system, check out http://forums.xisto.com/no_longer_exists/ . Of course, you'd need stripslashes to make sure that sql injections won't work. And as incoherent last words, it's best to use a framework for larger projects (I recommend CakePHP)

Edited by Arbitrary (see edit history)

Share this post

Link to post
Share on other sites

Thanks to Arbitrary for this very complete introduction.I would also add a suggestion. If you are hosted here at Xisto, and if you like "learn-by-example", I would suggest you to use the Xisto's "Fantastico" facility, and install a phpbb forum or a 4image gallery, have a look at the sources generated, and see how they manage the database connection and user/passwords handling. I really think that looking at other people's programs help understanding a lot.And, of course, having a working system, which correctly connects to a correctly database, is really helpful.Then, comparing the working scripts to your faulty scripts could help correcting your errors.And, of course, when you will be grown, you will start writing down your own programs from scratch, and they will be far better than any examples that could be provided.RegardsYordan

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.