# Miscellaneous > Database Programming >  Linking Website to MySQL database

## rasmith

I am currently trying to complete part of my A-level coursework but am stuck.

i am creating a website for a restaurant on which customers can submit information to do with four areas:
---------------------
Bookings
Comments
Job Applications
Pre-order
----------------------

i have used access alot in the past and had no problems creating a running model of my final database. 

i have built my website and created the four tables in the alloted MySQL area that came with my website package.

BIG QUESTION:

how do i create the interface for people to submit data and for it to be sent to my database?

i am a complete beginner to MySQL using it for the first time this week so im not sure what i need to do in the slightest to solve this issue

-------------

any help or advice would be VERY VERY gratefull

----------------
----------------
i am using php with MySQL

----------


## sboerner

You'll have to write a php based website that inserts the data in the database.  :Wink: 

PHP offers many functions for accessing (MySQL) Databases. You can find the documentation here:
http://www.php.net/manual/en/ref.mysql.php

To let the user input the data, use HTML forms.

----------


## bflinn

Here is a simple outline that I use to build a web interface to a MySQL database using PHP.

1) create the database

2) create a HTML form to capture data elements with *.php extension, <FORM ACTION="add_xx.php" METHOD="post"> to pass VALUEs for processing, form field NAME= should be same as database field names

3) add_xx.php = process VALUEs from form input, check for empty required fields, make connection to MySQL database using web2_connect() function found in xx_db2.inc an include file with user permissions to add to database. 

//be sure to addslashes() to data
$field1 = addslashes($field1);
$field2 = addslashes($field2);
...

//include connection code
include ("../include/xx_db2.inc");

//call connection function
  web2_connect()
    or exit ();

Include file is outside the web directory structure. 
========================
<?
# xx_db2.inc
# functions to connect to database

# connect with webuser2 who has select, insert, delete, update privileges
function web2_connect ()
{
 	$db = @mysql_pconnect("localhost", "your_user_here", "your_password_here");
	if ($db && mysql_select_db("your_database_here"))
	{
		return ($db);
	}
	else
	{
		echo "<P>Error: Could not login to server. Please try again later.<P>";	
		return (FALSE);
	}
}
?>
========================
After connecting then INSERT data:

//data inserted needs to be in the same order that the fields are in the database - use describe to get field order 

$query = "insert into your_database_here values
           ('".$field1."', '".$field2."', '".$field3."', '".$field4."')";

$result = mysql_query($query);

//test result and report success
if ($result)
 echo "Success: ".mysql_affected_rows()." entry inserted into database.";
//end of add_xx.php

4) create user interface to view all entires in Table format with link to Edit or Delete entry: display_xx.php

5) create HTML form to Edit entry: edit_xx.php

6) create PHP to Update record after Edits: upd_xx.php

Seems like alot but once you have a working structure it is very easy to duplicate for another application.

If you want a complete template let me know and I'll put one together for you and post it here.

----------

