CGI + Database
Ok i setup a database using the instructions on htmlgoodies... now it doesnt really say how to get info on there.. i mean sure it gives variables and codes. But it deosnt say where to apply the codes. it's very simple database... first name lastname and email. Now how do i write a script to send stuff to there?
ok sorry... bare with me i'm fairly new at all this database stuff.... Ok here is my scenario, I just learned how to make a table database (real basic) using Access (downloading SQL as we speak). Anyway, My database table consists of first_name last_name and email.. as per example of htmlgoodies database. Now the next section after making the database is inserting text into the database. How exactly do i Insert stuff into the required fields. Do i have to make an html page with certain values that tells the information where to be added into the database? My question basically is how do i send information to be collected to the database?
First we should be clear about what language you plan on doing this in.
Here's a sample script that will input data into a MySQL database using PHP:
That will put the values "Dave", "Clark", and "firstname.lastname@example.org" in the columns "firstname", "lastname", and "email". If the query fails, the die() function is called, which stops the script with an error message you specify. Obviously, you'll have to change the first four variables to fit your particular database, except for $hostname, which should be localhost in your case. If your database doesn't have a password, you can just omit that parameter from the mysql_connect() function call. Also, it's always smart to put these variables in a .php include file, for tighter security. Actually, because it seems you're hosting your own site, it would be even more logical to put the variables in a directory seperate from your web root, so that it can't be accessed at all outside of the script (and directly by you).
$hostname = "localhost";
$db_name = "name of the database";
$db_user = "your database username";
$db_pass = "your database password";
$input_fname = "Dave";
$input_lname = "Clark";
$input_email = "email@example.com";
$query = "INSERT INTO name_table (firstname, lastname, email) VALUES ('$input_fname', '$input_lname', '$input_email')";
// Connect to database using variables already defined
mysql_connect($hostname, $db_user, $password);
// Input information in variables or stop script
$result = mysql_query($query) or die("MySQL Error");
Note that this script assumes that MySQL is set up, that there is a database called "name of the database", and that there are three columns with appropriate names in that database. Setting up the database and tables can be done with another script, which I'll write for you if you want.
(sorry to widen the topic)
:-D You wrote the post above mine, and you have a first and last name in your user name. Anyway, I thought you'd like being put into zyous's database.
Thank you... that makes for easier comprehension for all of us here :-)
Here's a sample script that will input data into a MySQL database using PHP
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)