Friday, December 7, 2012

PHP Connection With MySql DataBase








Let's learn to connect the php with MySQL Database to store and retrieve data 
<?php
$host='localhost';
$user='root';
$pass='';
$database_name='user';
?>

<?php
    require_once 'config/config.php';
    $connect = mysql_connect($host,$user,$pass) or die("Connection to server failed");
    mysql_select_db($database_name,$connect) or die("Connection to database failed");
   
    $name = $_POST['name'];
    $phone_no = $_POST['phone'];
    $email = $_POST['email_addr'];
   
    $result=mysql_query("INSERT INTO userdetails(Name,PhoneNo,Email) VALUES ('$name',$phone_no,'$email')",$connect);
    echo $result;
    if($result){
        echo "Successfully inserted";       

    }
    mysql_close($connect);
?>

#main
{
    background-color:#00FF00;
    width:300;
    height:100;
    position:absolute;           
    margin-left:400;
    margin-top:200;
    padding-left:10;
    padding-top:10;
}
#mainbody
{
    background-color:#00FF00;
    width:350;
    height:250;
    position:absolute;           
    margin-left:300;
    margin-top:200;
    padding-left:10;
    padding-top:10;
}
   


<html>
    <head>
        <title>HTML Form</title>
        <link rel="stylesheet" type="text/css" href="style.css" >
        <script src="validation.js" type="text/javascript" xml:space="preserve"></script>
    </head>
    <body bgcolor="#9999CC">
    <div id="mainbody">
        <form name="details" action="insert.php"  onsubmit="return validateFormOnSubmit(details)" method="POST" >
            <p>
            Enter your name:
            <br />
            <input type="text" size=50 name="name" />
            <p>
            Enter your phone:
            <br />
            <input type="text" size=50 name="phone" />
            <p>
            Enter your email address:
            <br />
            <input type="text" size=50 name="email_addr" />
            <p>
            <input type=submit value="submit" />
            <input type=reset value="clear" />
        </form>
    </div>
    </body>
</html>


      

No comments:

Post a Comment