|
Setting the table
So now we have a database to which we can connect. Before we can put data into it we need to create a TABLE and define the FIELDS (columns) as to their type and size for every ROW (record).
For our example, we need to create a table that contains:
name .... any number of characters
age .... an integer number of three digits or fewer
sex .... any single character (but M or F will suffice)
And since we need to know which ROW (record) those data are in (to edit/delete or retrieve them), we'll have to have one more column in our table. Since it will 'identify' the row we'll name that column id.
A word of CAUTION: database names, table names, and field names are all case-sensitive. Watch what you type!
Creating a (new) table can either be done using a short piece of php code, or more simply by using phpAdmin accessed from your web site Control Panel. Once you use phpAdmin, you'll notice that not only can you create a table but you can also add data to the table. However, what we really want to do is learn how to add data to the database (or otherwise manipulate the database) from a web page. For now, let's assume that you create only the database table using phpAdmin (the database editor provided in your site's Control Panel).
phpAdmin
Visit your web site control panel - usually http://www.yourdomain.tld/cpanel - and log in with the hosting account username and password you were given by your web host (or whatever you changed them to!). Scroll down until you see the phpAdmin link, then click it. You'll be presented with a page that looks like this:
Click on 'databases' in the top left dropdown, and the names of (all) your databases will be displayed below. Now click on the name of your new database (accountname_members), and you'll see a new screen telling you that you have no tables in that database and inviting you to enter a table name and the number of fields for the database table. Enter the table name as details and the number of field as 4.
Once you click the 'go' button beside the form asking for the database table name and number of fields, you'll be presented with another phpAdmin screen allowing you to enter fields - name and characteristics - through a simple form which includes drop-downs and radio buttons (away off to the right). The screen you see is far wider than your monitor, but don't worry.
For our name/age/sex database table named 'details', here is how we want to set the database table fields:
| NAME |
TYPE |
SIZE |
| id |
TINYINT |
4 |
| name |
VARCHAR |
40 |
| age |
TINYINT |
3 |
| sex |
CHAR |
1 |
And for the id column, choose some special characteristics - we want id to be the primary index (it's how we reference the rows), and we want it to 'auto-increment' (so that every time we INSERT data into the database the data is added to the next-numbered record automatically). Having id auto-increment will also make it unique since no other row will have the same value of id. Scroll across the phpAdmin screen and click the radio button named 'primary', and select 'auto-increment' from the drop down under 'extras', in the first row of your database definition - the row where the field name id is detailed.
If all goes well, you should be able to view the STRUCTURE of your database table and have it look just like the image below. If not, use phpAdmin to make the necessary changes:
We're done (the important/tedious bit)
We've established a database, set a username and password for it, authorized ourselves to access it, and created a table for the data .... now let's get to the exciting parts!
« previous | next »
|