Thursday, April 3, 2008

1. Knowledge of HTML (average knowledge)

2. Knowldege of PHP (average knowledge)

3. Knowledge of Mysql (average knowledge)





now that you have at least all the three points above, its time to show you how
you can insert the data.



In this example, lets say i want to collect data from a user. For the purpose of
this tutorial guide, i only need the user's name and their email address. so i
only need two text fields. one is username and the second one is user_email. so
this is how the form would look like in HTML:


 


Then next stop is to check if the form has been submitted, for this we will
need PHP code. for cdoe to check if the form has been submited or not will be by
checking the value of "Submit" which is the name of the button in our form.




 


 


the third piece we are going to need is the actual connection to your
database from the PHP script. so in order to do this, you must tell your PHP
script how to connect to your MYSQL database and you will need the 7 following
items, if you don't know these 7 items, you are wasting your time here, because
you need these to be able to put the data into your mysql tables or database.
and they are:



1. hostname (usually localhost)

2. database user name (if you don't have this contact your host company)

3. database user password (if you don't have this contact your host company)

4. database name (if you don't have this contact your host company)

5. database table name (you create this. you can create a table in phpmyadmin)


6. Create a field called user_name in the database table (you create this. you
can create a table in phpmyadmin)

7. Create a field called user_email in the database table (you create this. you
can create a table in phpmyadmin)



OK, once you have confirmed you have all these seven items we can proceed. for
my example, i am going to pretend that these are mine:



my hostname is "localhost"

my database user name is "Kroniksan"

my wallpaperama1 password is "password123"

my database name is "users"

my database table name is "user_info"

my user_name field in the user_info table will be called "user_name"

and user_email field in the user_ifo table will be called "user_email"



if you have phpmyadmin, you can dump this query. just click on the "SQL" tab on
your phpmyadmin and put this code in the text area and submit:



so once i have all these and can put my code together and this is how my php
script would look like:




 


 


now what you can do is open your text editor like notepad and copy and past
the php code above and save it as enter-data.php (if you are using notepad, make
sure to put qoutes in between the file name, like this "enter-data.php")



IMPORTANT: make sure you change the following to match your database, otherwise,
nothing will be entered into your databse if you don't give the correct
information in the following lines in this code:



 


now that yo have saved the enter-data.php, upload to your PHP hosting website
and open it with your brows and you will see how it work, it will enter the data
into your mysql database.



REMEMBER: This is a plain and simple script. There are alot of security issues
you need to implement. The purpose of this tutorial was to give you an idea of
how you can enter data into your mysql database. You can make your scripts more
secure by using the mysql_real_escape_string()
for example.

 

No comments: