Tuesday 7 August 2012

PHP My Works Example

EXAMPLE OF INSERT INTO DATABASE

if(isset($_POST['submit']))
{
               
    $name=$_POST['name'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $email=$_POST['email'];

$sql="insert into project1 values('$name','$age','$gender','$email')”;
$count=mysql_query($sql)or die(mysql_error());
if($count==1)
{
         header("location:index.php");
}
else
{   
        echo "Not Registered";
 }


ACCEPT ALL KEYBOARD VALUES IN DATABASE

$address=mysql_real_escape_string($address1);


DON’T INSERT DUPLICATE VALUES IN  DATABASE

if(isset($_POST['submit']))
{             
    $name=$_POST['name'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $email=$_POST['email'];
    mysql_connect("localhost","root","");
    mysql_select_db("hussain");
$sql="select * from project1 where name='$name'";
                $result=mysql_query("$sql");
                $data=mysql_fetch_row($result);
if(mysql_num_rows($result))
                {
                                echo "This name is already existes pls enter another name";
                }
                else
                {
 $sql="insert into project1 values('$name','$age','$gender','$email','$password','$hobbies','$address','$pincode','$country','$image','0')";
$count=mysql_query($sql)or die(mysql_error());
if($count==1)
        {
            header("location:index.php");
        }
    else
        {   
            echo "Not Registered";
        }

UPLOADING  IMAGE  AND SAVING IN SPECIFIC  LOCATION  

if(isset($_POST['submit']))
{             
    $name=$_POST['name'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $email=$_POST['email'];
    $image=$_FILES['image']['name'];
    mysql_connect("localhost","root","");
    mysql_select_db("hussain");
$sql="select * from project1 where name='$name'";
                $result=mysql_query("$sql");
                $data=mysql_fetch_row($result);
if(mysql_num_rows($result))
                {
                                echo "This name is already existes pls enter another name";
                }
                else
                {
 $sql="insert into project1 values('$name','$age','$gender','$email','$image')";
$count=mysql_query($sql)or die(mysql_error());
                if($count==1)
        {
            header("location:index.php");
        }
    else
        {   
            echo "Not Registered";
        }
                               
                                move_uploaded_file($_FILES["image"]["tmp_name"],
        "image/" . $_FILES["image"]["name"]);
        echo "Stored in: " . "image/" . $_FILES["image"]["name"];
                }
       
}



HOW TO MULTIPLE CHECK BOX VALUES  STORED INTO SINGLE VARIABLES  

if(isset($_POST['submit']))
{             
    $name=$_POST['name'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $email=$_POST['email'];
//inside a connection

    $hobbies = mysql_real_escape_string(implode(',', $_POST['hobbies']));

$sql="insert into project1 values('$name','$age','$gender','$email','$'hobbies')";
$count=mysql_query($sql)or die(mysql_error());
                if($count==1)
        {
            header("location:index.php");
        }
    else
        {   
            echo "Not Registered";
        }
   }
<tr>
    <td valign="bottom">Hobbies</td>
    <td valign="bottom">
    <input type="checkbox" name="hobbies[]" value="Cricket" id="hobbies[]">Cricket
   <input type="checkbox" name="hobbies[]" value="Football" id="hobbies[]">Football
   <input type="checkbox" name="hobbies[]" value="Hockey" id="hobbies[]">Hockey
   <input type="checkbox" name="hobbies[]" value="Tennis" id="hobbies[]">Tennis
   </td>
  </tr>

LOGIN FROM

if(isset($_POST['submit']))
{
                mysql_connect("localhost","root","");
    mysql_select_db("hussain");
               
                $name=$_POST['name'];
                $password=$_POST['password'];
                //$ustatus=$_POST['user_status'];

                $sql="SELECT * FROM project1 WHERE name='$name' and password='$password'";
               
                $count=mysql_query($sql);

                $row = mysql_fetch_assoc($count);
               
                if( $row['name'] == $name && $row['password'] == $password )
    {

                                $_SESSION['name']=$name;
                                //$_SESSION['user_status']=$ustatus;
                                header("location:admin.php");
   } else
                {
                echo "Worng User Name and Password";
                header('location:index.php');
                }
}

LOGOUT FROM

session_start();
session_unset($_POST['name']);
session_destroy();
header("location:index.php");

 
FORGET PASSWORD

if(isset($_POST['submit']))
{
                mysql_connect("localhost","root","");
    mysql_select_db("hussain");
               
                $name=$_POST['name'];
                $email=$_POST['email'];

                $sql="SELECT * FROM project1 WHERE name='$name' and email='$email'";
               
                $count=mysql_query($sql);

                $row = mysql_fetch_assoc($count);
               
    if( $row['name'] == $name && $row['email'] == $email )
    {
                                echo "Your Password is</br>" .$row['password'];
                }
                else
                {
                echo " Pls Enter a Valid User Name and Email ID";
                //header('location:Loginproject.php');
                }
}


PHP Advanced


PHP Date

<?php
echo date("Y/m/d") . "<br />";             output:2012/08/07
echo date("Y.m.d") . "<br />";                        2012.08.07
echo date("Y-m-d");                                            2012-08-07
?>

Syntax for mktime():

mktime(hour,minute,second,month,day,year,is_dst)

example:

<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?> 

output: Tomorrow is 2012/08/07

 

PHP Include  

<?php include 'header.php';

echo '<a href="/default.php">Home</a>
<a href="/tutorials.php">Tutorials</a>
<a href="/references.php">References</a>
<a href="/examples.php">Examples</a> 
<a href="/about.php">About Us</a> 
<a href="/contact.php">Contact Us</a>';

?>


PHP File Upload

index.php
<html>
<body>
 
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>
 
</body>
</html>

 upload_file.php

 <?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>

By using the global PHP $_FILES array you can upload files from a client computer to the remote server.
 
The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:
  • $_FILES["file"]["name"] - the name of the uploaded file
  • $_FILES["file"]["type"] - the type of the uploaded file
  • $_FILES["file"]["size"] - the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] - the error code resulting from the file upload
This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload.

Saving and Restriction File Upload:

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 

 

PHP Sessions

Starting and Storing Session Variables 

<?php
session_start();
// store session data
$_SESSION['views']=1;
?>

<html>
<body>

<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>

</body>
</html>

Destroying Session:

If you wish to delete some session data, you can use the unset() or the session_destroy() function.

The unset() function is used to free the specified session variable:

<?php
session_start();
if(isset($_SESSION['views']))
  unset($_SESSION['views']);
?>
 
You can also completely destroy the session by calling the session_destroy() function:

<?php
session_destroy();
?>

PHP E-mail 

Syntax : mail(to,subject,message,headers,parameters)

PHP SIMPLE MAIL

<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>