Wednesday, January 27, 2010

PHP -MYSQL Tutorial

First connect to Mysql Database
// define variables 
$dbhost = 'localhost';
$dbuser = 'root';
$dbpassword = 'jagdeep';
$con=mysql_connect($dbhost,$dbuser,$dbpassword) or die ("cannot connected") ;
$dbname = "ppl";
$tablename = "map";

?
save this file as "common.inc"  used for others files. 

 Create Database  and  table.
<?php
include "common.inc";
$create_db = "CREATE DATABASE $dbname ;"; //Create Query for Create database

mysql_query($create_db) or die ("can't create database"); //Create Database

mysql_select_db($dbname)or die("cant connect"); // Select database

$create_table = "CREATE TABLE $tablename ( latitude DOUBLE NOT NULL, longitude DOUBLE NOT NULL,title VARCHAR (50),description VARCHAR(50), icon VARCHAR (20),icon_size FLOAT(10) , icon_offset VARCHAR (20) , date DATE NOT NULL ,time TIME NOT NULL ,PRIMARY KEY (latitude));"; //Create Query for Create Table

mysql_query($create_table)or die(" can not create table" );//Create Table
?>

Create html page for insert the data in database.
 
<html>
<title>
ADD NEW RECORD
</title>
<link href="jsm.css" rel="stylesheet" type="text/css">
<form action="newrecord.php" method ="post">
<table>
<H2> ADD NEW RECORD </H2>
<tr>
<td>Latitude: </td>
<td><input type ="text" name="latitude"></td>
</tr>
<tr>
<td>Longitude:</td>
<td ><input type ="text" name="longitude"></td>
</tr>

<tr>
<td> Title:</td>
<td><input type ="text" name="title"></td>
</tr>

<tr>
<td> Description:</td>
<td ><input type ="text" name="description"></td>
</tr>

<tr>
<td> Icon:</td>
<td ><input type ="text" name="icon"></td>
</tr>

<tr>
<td >Icon Size:</td>
<td ><input type ="text" name="icon_size"></td>
</tr>

<tr>
<td> Icon Offset:</td>
<td ><input type ="text" name="icon_offset"></td>
</tr>

<TR>
   <TD>Date:</TD>
   <TD>
      <!-- You can use PHP functions to automatically get the value of date -->
      <INPUT TYPE='TEXT' NAME='date' VALUE='<? echo date("Y-m-j"); ?>'>
   </TD>
</TR>
<TR>
   <TD>Time:</TD>
   <TD>
      <!-- You can use PHP functions to automatically get the value of time -->
      <INPUT TYPE='TEXT' NAME='time' VALUE='<? echo date("g:i a"); ?>'>
   </TD>
</TR>

<tr>
<td align ="center">
<table >
<tr>
<td >
<td ><input type ="Submit" name="submit" value ="Add"></td>
<td><input type ="reset" name="reset" value ="Reset"></td>
</tr>

</table>

</tr>
</table>

</from>
</body>
</html>

Make the the newrecord.php  files which is used in HTML files.
<?php

include "common.inc";


mysql_select_db("$dbname") or die ("can not create");//select database

$latitude=stripslashes(trim($_POST['latitude'] ));
$select_record="select * from $tablename where latitude=' " .$latitude. " ' ";
$select_query= mysql_query($select_record) or die (mysql_error());

if (mysql_num_rows($select_query)==0)// take "select_record" pointer and returns the number rows
{
    $longitude=stripslashes(trim($_POST['longitude'] ));
    $title=stripslashes(trim($_POST['title'] ));
    $description=stripslashes(trim($_POST['description']));
    $icon=stripslashes(trim($_POST['icon'] ));
    $icon_size=stripslashes(trim($_POST['icon_size'] ));
    $icon_offset=stripslashes(trim($_POST['icon_offset'] ));
    $date=stripslashes(trim($_POST['date'] ));
    $time=stripslashes(trim($_POST['time'] ));
    $add_new= "insert into $tablename(latitude,longitude,title,description,icon,icon_size,icon_offset ,date,time) values ($latitude,
        '$longitude','$title','$description','$icon','$icon_size','$icon_offset','$date','$time')"; // create Qurey

    $add_query=mysql_query($add_new) or die (mysql_error());//select Qurey for add new record

   
    if($add_query) //Check for Record Added or Not
    {
        echo "new record Added!!";
    }
}

else
echo "duplicte rol not allowed";
mysql_close($con);
?>
 
  Retrieve data from database
<?php
include "common.inc";
mysql_select_db("$dbname") or die ("cant select");//select database
$select_query="SELECT * FROM $tablename ";//create query
$list = mysql_query($select_query);//apply
//Create table for list the data
echo "<table border = '1'> 
<tr>
<th>Latitude</TH>
<TH>Longitute</TH>
<TH>Title</TH>
<TH> Description</TH>
<TH> Icon</TH>
<TH> Icon_size</TH>
<TH> Icon_offset</TH>
<TH> Date</TH>
<TH> Time</TH>
</TR>";
echo "</table>";
while($record = mysql_fetch_array($list)) //fetch the data from database
{
echo $record ['latitude'] . "     |     " . $record ['longitude'] . "     |     " . $record['title'] . "     |     " . $record ['description'] . "     |     " . $record ['icon'] . "    |     " . $record ['icon_size'] . "    |     " . $record ['icon_offset'] . "    |    " .  $record ['date'] . "    |    " . $record ['time']  ;
echo "<br/> ";
}
mysql_close($con);
?>
Upate data from Table 
<?php
include "common.inc";
$dbname = "jsm";
$tablename = "mapinformation";
mysql_select_db("$dbname") or die ("cannot select");
$res=mysql_query("UPDATE $tablename SET title = 'parking'  where description = 'Venue II' ");
if ($res)
echo "record changed";
else
echo "record not changed ";
?>

 
Delete data from Table
<?php
include "common.inc";
$dbname = "jsm";
$tablename = "mapinformation";
mysql_select_db("$dbname") or die ("cannot select ");
$res =mysql_query("DELETE from $tablename WHERE title = 'parking' ");
if (!$res)
echo "record not delet";
else
echo "record delete";
?>