MySQL Database using PHP

They have: 37 posts

Joined: Apr 2000

Here is what the form will look like just so you can check it for me (please)

code:

<form action="file.php" method="get">
Name<br>
<input type="text" name="name"><br>
Address<br>
<input type="text" name="address1"><br>
<input type="text" name="address2"><br>
<input type="text" name="address3"><br>
E-mail Address<br>
<input type="text" name="email"><br>
Phone Number<br>
<input type="text" name="phone"><br>
<input type="submit" value="Send">
</form>[/code] 

They have: 37 posts

Joined: Apr 2000

Hi guys, I need some help here, I'm totally new to PHP and MySQL, however i'm trying to develop a site based arround these two things.

I normally pick things up quite easily, but I need examples to work from.

Can someone please help me by telling me the code to do the following.

Lets say I want to create a database called info.???

The database will have the following fields in it:

name
address1
address2
address3
email
phone

The information will be sent to the service via a form on the site (which I can develop)

Can someone please give me an example of the code needed to do this.

Thanx

------------------
Dan
The World of Dan

They have: 850 posts

Joined: Jul 1999

First you will need to create the database if you havent already. So you login through telnet, login to mysql and copy/paste

CREATE DATABASE databasename;

Than you will need to create the table where the input data will be stored.

CREATE TABLE peopleinfo(
ID INT(5) NOT NULL AUTO_INCREMENT,
Name VARCHAR(40),
Address1 VARCHAR(40),
Address2 VARCHAR(40),
Address3 VARCHAR(40),
Email VARCHAR(40),
Phone int(15),
PRIMARY KEY (ID)
);

Inside the scriptname.php3
have the following:

code:

<?
#Define variables
$host = "localhost";
$lpassword = "pword";
$login = "login";
$database = "databasename";
$table = "peopleinfo";


#Make the connection to the mysql server	
$db =  mysql_connect("$host","$login","$lpassword");
mysql_select_db("$database", $db);	
	
#Make sure the important variables are not empty
if($name & $address1 & $email & $phone)
{
	#The variables needed are not empty, lets add them to the database
	$query="INSERT INTO $table (Name,Address1,Address2,Address3,Email,Phone) VALUES ('$name','$address1','$address2','$address3','$email','$phone')";
	$result = mysql_query($query);
	if($result)
	{
		echo "The following was added to the database:<br>$name,$address1,$address2,$address3,$email,$phone";
	}
	else
	{
		echo "An error occured while trying to add the data to the database";
	}
}
else
{	
	echo "You did not enter in all of the data required";
}
#closing mysql connection
mysql_close();
?>
<form action="<? echo $PHP_SELF ?>" method="get">Name<br><input type="text" name="name"><br>Address<br><input type="text" name="address1"><br><input type="text" name="address2"><br><input type="text" name="address3"><br>E-mail Address<br><input type="text" name="email"><br>Phone Number<br><input type="text" name="phone"><br><input type="submit" value="Send"></form>
[/code]

Please not that I have not tested this code, so if anoyne else find errors in my code, please inform me.

When creating the new table, you can define how long each columns should be, by editing the numbers inside VARCHAR(##) or INT(##).

Hope that helps a little.

------------------
http://www.wiredstart.com  : The Technology Start Page


[This message has been edited by robp (edited 10 May 2000).] 

They have: 37 posts

Joined: Apr 2000

This is cool, but I have a slight problem, I don't have telnet access, I think I might be able to get it, but i'd prefer to do it another way if it's possable.

Any suggestions??

------------------
Dan
The World of Dan

They have: 37 posts

Joined: Apr 2000

What would the databases file extention be?

They have: 453 posts

Joined: Jan 1999

Hi,

developing scripts without telnet/ssh access is very painfull and inefficient.
If your hosting company doesn't provide it, use another one.

You can use phpMysqlAdmin to create the database.

Since you are using a RDBMS (mysql) you don't have to care about the file extension.

btw:
The php and mysql homepages both have links to very good tutorials for this, including examples.

I just started using PHP today, but my mySQL+PHP user-manager already works ...

ciao
Anti

They have: 37 posts

Joined: Apr 2000

I can get SHH (secure telnet access I think) but I need to provide an IP address, and my ISP dynamaticly assigns them Sad

I have got the phpmysqladmin thing, it's just been installed on my server, so I'll have a mess about with that and see what happens

Thanx.

------------------
Dan
The World of Dan

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.