473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ


PHP and Writing to MySQL Databases

By Blair Ireland
Senior Editor, TheScripts.com

PHP and Writing to MySQL Databases

First of all, this tutorial is based on MySQL, available at http://www.mysql.com/. It's free for Unix based systems, but Windows users can get the shareware copy. You are asked to pay if you use the windows version.

Anyway, I am assuming it is already installed on your server (ask your host why the heck they don't got it on the server if it isn't installed... geez, its free :-) )

Once you know you got MySQL installed, I would recommend a group of PHP scripts to administer your databases. This application is called phpMyAdmin and is available at http://www.phpmyadmin.net/home_page/. This application makes life a breeze.... you won't need to remember all of the syntax for MySQL with it.... but I would still recommend knowing the syntax for a general understanding.

Anyway, I'm getting off track here.....

Before I start, we must set a few things....

We will be using the following html form (recognize it from the other tutorials? :-) ) for the entire tutorial..

<HTML>
<HEAD>
<TITLE>Form Handling with PHP</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<FORM METHOD=POST ACTION="add.php3">
<input type="hidden" name="id" value="NULL">
<TABLE>
<TR height="20">
<TD colspan="2"><FONT SIZE="+0" face="verdana"> Below is a Sample Form for our PHP tutorial</TD>
</TR>
<TR height="50">
<td></td>
</TR>
<TR>
<TD align="left"><FONT SIZE="+0" face="verdana"> <b>Your Name <br>
Your E-Mail Address</b></td>
<td><INPUT TYPE="text" NAME="name">
<br>
<INPUT TYPE="text" NAME="email">
</TD>
</TR>
<tr>
<td colspan="2"><center>
<SELECT NAME="opinion">
<option value="is great">I like your site</option>
<option value="is OK">Your Site is OK</option>
<option value="is horrible">Your Site is horrible</option>
</SELECT>
<INPUT TYPE="submit" value="Tell us!">
</td>
</tr>
</TABLE>
</FORM>
</BODY>
</HTML>

Next we must have an example table made... So lets go with this one;

mysql> CREATE TABLE information (
> id INT NOT NULL AUTO_INCREMENT,
> name VARCHAR (50),
> email VARCHAR (50),
> opinion VARCHAR (30),
> PRIMARY KEY (id)
);

If that syntax is beyond you, read our MySQL tutorial here.

Ok, drink your coffee (or wuddever artificial stimulant you prefer), and lets proceed. Maybe even turn up the music so I don't lose you to that horrible thing we call sleep... as things can get boring (trust me, I just turned up my Limp Bizkit in the background to keep me from falling asleep on the keyboard)

Now, as the form states, we are sending this information to add.php3, so lets make it....

<?
$DBhost = "Your-MySQL-servers-IP-or-domainname";
$DBuser = "your user name";
$DBpass = "Your Password";
$DBName = "The Name of the Database";
$table = "information";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select
database $DBName");

$sqlquery = "INSERT INTO $table
VALUES('$id','$name','$email','$opinion')";

$results = mysql_query($sqlquery);

mysql_close();

print "<HTML><TITLE> PHP and MySQL </TITLE><BODY
BGCOLOR=\"#FFFFFF\"><center><table border=\"0\"
width=\"500\"><tr><td>";
print "<p><font face=\"verdana\" size=\"+0\"> <center>You
Just Entered This Information Into the
Database<p><blockquote>";
print "Name : $name<p>E-Mail : $email<p>Opinion :
$opinion</blockquote></td></tr></table>
</center></BODY></HTML>";
?>
  The Code Explained »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.