# [SOLVED] Connecting a webpage to a mysql database



## wmorri (May 29, 2008)

Well, the title basically says it all. I need to connect to forms to a database. I have already created the database with the table and columns that I need but I need to know how to connect to it.

I am aware that I need to use this php script:

```
<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
```
But I enter all my information correctly as I know how and it doesn't connect. Help would be greatly appreciated. 

Cheers!


----------



## carsey (Aug 19, 2006)

*Re: Connecting a webpage to a mysql database*

This is the script I use to connect to my database


```
<?php
$mysql_server = "******";
$mysql_user = "******";
$mysql_password = "******";
$mysql_database = "******";
$timeoutseconds = 300000; 


$connection = mysql_connect("$mysql_server","$mysql_user","$mysql_password") or die ("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=../offline.php\">");

$db = mysql_select_db("$mysql_database") or die ("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=../\">");
?>
```


----------



## wmorri (May 29, 2008)

*Re: Connecting a webpage to a mysql database*

Here is what I am using right now. See attachment

*Removed as contained mysql connection information.*


----------



## carsey (Aug 19, 2006)

*Re: Connecting a webpage to a mysql database*

Im not sure, but that may contain your password. Might be best to remove it if it does and also change it incase others saw it.

Are you selecting which table you want the mysql to connect to?


----------



## wmorri (May 29, 2008)

*Re: Connecting a webpage to a mysql database*

No, I am not selecting which table I want to connect to. I have three fields that I am need information sent to. You can take a look at the web page at: www.uberpaper.com/homepage.htm

Cheers!


----------



## carsey (Aug 19, 2006)

*Re: Connecting a webpage to a mysql database*

You will need to select which table you want the data sent from your forms to go into the mysql.

Heres what you should have:

HTML FORM SIDE


```
<form name="form1" method="post" action="form_submit.php">
First Name
<input name="name" type="text" id="name">
Last Name:
<input name="lastname" type="text" id="lastname">
Email:
<input name="email" type="text" id="email">
<input type="submit" name="Submit" value="Submit"></td>
</form>
```
That will produce the 3 text fields for user entry.

We need to make the mysql table. You will need to run this as a SQL query.


```
CREATE TABLE `users` (
`id` int(4) NOT NULL auto_increment,
`name` varchar(65) NOT NULL default '',
`lastname` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=0 ;
```
Now for the inserting php


```
<?php

$host="localhost"; // Host name
$username="******"; // Mysql username
$password="******"; // Mysql password
$db_name="******"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}

else {
echo "ERROR";
}

// close connection
mysql_close();
?>
```
Obvilously you will need to change the values for the connecting to the database


----------



## wmorri (May 29, 2008)

*Re: Connecting a webpage to a mysql database*

I changed all the code and syntax on my website. When I enter information into to fields and then hit submit, i get a page: www.uberpaper.com/form_submit.php, and it says that "No input file specified."

Do i need to write a page for this?

Thanks, and cheers!


----------



## carsey (Aug 19, 2006)

*Re: Connecting a webpage to a mysql database*

call this form_submit.php


```
<?php

$host="localhost"; // Host name
$username="******"; // Mysql username
$password="******"; // Mysql password
$db_name="******"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, lastname, email)VALUES('$name', '$lastname', '$email')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}

else {
echo "ERROR";
}

// close connection
mysql_close();
?>
```


----------



## wmorri (May 29, 2008)

*Re: Connecting a webpage to a mysql database*

What is the diference between the above ^^ php code, and the code that I put into my webpage? I am just wondering why put it in two places?

Cheers!


----------



## carsey (Aug 19, 2006)

*Re: Connecting a webpage to a mysql database*

You will need to use the form HTML in your existing webpage. 
The HTML form will look for form_submit.php and then the form_submit.php will take over and collect the values entered in the HTML form and enter them into the mysql.


----------



## wmorri (May 29, 2008)

*Re: Connecting a webpage to a mysql database*

<url=http://www.uberpaper.com/form_submit.php>Here</url> is what I get when I added this to my webpage.

It wasn't quite what i expected.

Cheers!


----------



## carsey (Aug 19, 2006)

*Re: Connecting a webpage to a mysql database*

That means you havent entered the database usernames and passwords correctly.


----------



## wmorri (May 29, 2008)

*Re: Connecting a webpage to a mysql database*

Thanks for all your help I will have to do a lot more learning on this before I can use it on my website. I should probably start from the beginning with it all. So for now this is solved.

Cheers!


----------

