Hi there,
Its a bit hard to work with database if you are beginner and haven't read anything about this before. I don't know which is the best way to help you but let me give you an example for your situation. hopefully this can help you.
First we create a form in HTML called "myForm.html" like this :
"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Work with database in PHP</title>
</head>
<body>
<!--
* I just create a simple form.
ABOUT THIS FORM:
- methods: can be 'post' or get to send the form's data
- action: this will be a .php file where you want to work with your database
- You should give input tags a name.
- You can use a "Submit" button to submit form or any elements go with their actions.
-->
<form name="myForm" action="processForm.php" method="post">
Fullname <input type="text" name="fullname"/>
email <input type="text" name="email"/>
<input type="submit" name="submitForm" value="Send now" />
</form>
</body>
</html>
"
Next create a file .php called "processForm.php" like this:
"
<?php
// Created by Quyen. 08 Jul 09. email: cuncon.it@gmail.com
/*
THIS PAGE WILL START WORK WITH DATABASE.
- First you need to create a simple database called 'mydb', this db will have 1 table called 'users', this table will have 3 fields named:
id (INT AUTO_INCREMENT), fullname (VARCHAR (225)),email(TEXT).
- After you have a database, now you are ready to work with this db.
*/
/*
Now if you want to work with database then go and connect to the db. Now we have
- database name: mydb
- table name: users
- 3 fields: id,fullname,email
*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//You can change the parameters in mysql_select_db("localhost","root","")
$myConnect = mysql_connect("localhost","root","");
if($myConnect)
{
echo "Connected to MySQL successful<br />";
}
else
{
die("Could not connect to MySQL" . mysql_error() . "<br />" );
}
$my_db = "mydb";
$table_name = "users";
//Select the db you will work with
$db_name = mysql_select_db($my_db);
//Now you need to get the values from the form in myForm.html, right? Now see what I have here:
/*If you do not understand why this $_POST[] appears here then go to
http://www.w3schools.com/php/php_post.asp to get the answer
*/
$fullname = $_POST['fullname'];
$email = $_POST['email'];
/*
Now I will try to insert data into the table 'users' and see how it works
*/
//This $sql_insert is a simple inserting query .
$sql_insert = "INSERT INTO $table_name (fullname,email) VALUES ('$fullname','$email')";
// Now we execute this query
$result = mysql_query($sql_insert);
//We are not sure if this query has executed successful or not, then we need to check it.
if($result)
{
echo"<br>Insert sucessfully...<br>";
}
else
{
echo"<br>Inserted error :".mysql_error()."<br>";
}
//You should close this connection after finished
mysql_close($myConnect);
?>
"
Its a bit long but hopefully you can understand (~_~)
Quyen,
Last edited by quyen (2009-07-08 01:04:45)