Khalilov:
I am wondering if you can show me how to link this to html, meaning the user will enter his username/password.... in text boxes. Can you tell me how to make a textbox for username/password...
Let's say your form looks something like this:
<form action="index.php" method="post"> </form
In this case, after the user clicks the "submit" button, it will lead to the file index.php and execute any php scripts that are located in that file. The method "post" simply refers to how the data is transferred from the form to the server--if it is "post", the data is not revealed in the url, whereas if it is request, the data is revealed in the url. A good example of a site that uses "request" methods would be Google--if you look at this url (http://www.google.co... hash functions), you can see that there is data being passed with the q=php+hash+functions.
Meanwhile, to the actual meat of the form, say you add a few text boxes and a submit button to the form so that it now looks something like this:
<form action="index.php" method="post"> <input type="text" name="username" value="" /> <input type="password" name="password" value="" /> <input type="submit" value="Add" /> </form>
Pay special attention to the name attribute in the input tags, as it is the name that determines how to access the data.
If you choose to use "post" as your method, then all form data is stored in a php variable called $_POST after you submit the form. $_POST is an associative array, and its structure looks something like this:
array ( ["name"] => "value", ["name2"] => "value2" )
In order to get the post data, you would use this:
$username = $_POST['username']; $password = $_POST['username'];
This is all relatively straightforward. Then you would want to store the data you retrieve from the form into the database with MySQL. Php has a nice function for executing MySQL queries called mysql_query. So, a sample query to put the data into the database would be something like the following:
mysql_query("INSERT INTO table_name (field1, field2) VALUES ('value1', 'value2')");In order to actually make this work for our earlier example involving inserting a username and password into the database, you'd put replace the table_name with its obvious replacement (say users), field1 and field2 with 'username' and 'password' and value1 and value2 with the variables we defined earlier from our $_POST values. So in short, like this:
mysql_query("INSERT INTO users (username, password) VALUES ('$username', '$password')");The query is relatively obvious--you insert the values of the username and password under the fields username and password into the table users. The current query is obviously immensely insecure, and you should not use it for actual storage of passwords. Before doing anything with passwords it's best to hash them (with a hash function of sha1 or better), and it's even better with a salt (a random string hashed with the password to make it more secure). But as this is supposed to be simple, I'll leave discussion of hash functions to another thread.
There are other MySQL queries you can do, such as editing data or delete data from the table. Those would look something like the following:
For updating:
mysql_query("UPDATE table_name SET field1 = 'one' WHERE field2 = '1'");For deleting:
mysql_query("DELETE FROM table_name WHERE field='robot';");Wikipedia also has a nice collection of SQL queries if you'd like to check it out. (Scroll down to the bottom and you'll see a full list next to the "SQL" tab. [http://en.wikipedia....iki/Select_(SQL)] Have fun.




