Database Connectivity Through PHP

Before running this you have to install "PhpMyAdmin"or "wamp"or "xampp" in your pc. then open any browser and write in Url bar "Localhost/phpmyadmin" and here we make our Database,
Now open one more tab in browser and open "localhost/Filename". we all know the ip address of localhost is 127.0.0.1

In Php for printing anything we use  echo"statement". in thiscode i also perform the validation.
  
Code
 
  1. <?php  
  2.    mysql_connect("localhost","root","");  
  3.    mysql_select_db("DATABASE NAME");  
  4. ?>  
  5. <html>  
  6. <head>  
  7.    <title>insert</title>  
  8. </head>  
  9. <body>  
  10.    <form name="frm" method="post">  
  11.       <table border="1" bgcolor="#9999CC" cellpadding="5" cellspacing="5" align="center" bordercolor="#9966FF";>  
  12.       <caption style="font-size:50px">SIGN UP</caption>  
  13.       <b>  
  14.       <tr>  
  15.             <td style="color:#FFF">Name</td>  
  16.             < td><input type="text" name="name" value="<?php echo $_POST['name']?>"></td>  
  17.       </tr>  
  18.         <tr>  
  19.             <td style="color:#FFF">Father's name</td>  
  20.             <td><input type="text" name="fname" value="<?php echo $_POST['fname']?>"></td>  
  21.       </tr>  
  22.       <tr>  
  23.             <td colspan="2" align="center"><input type="submit" name="submit"></td>  
  24.       </tr>  
  25.       </b>  
  26.       </table>  
  27. <?php  
  28. if(isset($_POST['submit']))  
  29. {  
  30. if($_POST['name']=="")              // for validation  
  31. {  
  32.    echo "please enter your name";  
  33. }  
  34. elseif($_POST['fname']=="")         //for validation  
  35. {  
  36.    echo "please enter father name";  
  37. }  
  38. else  
  39. {  
  40.    $strQuery ="insert into TABLE NAME(name,father_name) values('".$_POST['name']."','".$_POST['fname']."')";  
  41.    $strResult=mysql_query($strQueryor die("here some error".mysql_error());  
  42. }  
  43. }  
  44. if($strResult)  
  45. {  
  46.    echo "thanks for register";  
  47. }  
  48. ?>  
  49. </form>  
  50. </body>  
  51. </html> 
 
Ebook Download
View all
Learn
View all