Sunday, 8 June 2014

create/read/update/delete data using PHP & MYSQL.

Table of Content

  • Create Database
  • Insert Record with image upload
  • Show Record
  • Edit Record
  • Delete Record  
1. Creating a sample Database table 
CREATE TABLE IF NOT EXISTS `tbl_customer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `image` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
2.Connecting to Database   
$server = 'localhost';
$user = 'root';
$pass = '';
$connect = mysql_connect($server,$user,$pass)
or die(mysql_error());
$selectdb = mysql_select_db('mydb')
or die(mysql_error());
3. Insert & edit Record

ob_start();
include('connect.php');
if(isset($_GET['id']))
{
 $qry = "SELECT * FROM tbl_customer where id=".$_GET['id'];
 $result = mysql_query($qry);
 $row = mysql_fetch_array($result);
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
   $name = $_POST["name"];
   IF($_FILES['file']['name']!='')
   {
     $file='uploads/'.$row['image'];
     @unlink($file);
     $tmp_name = $_FILES["file"]["tmp_name"];
     $namefile = $_FILES["file"]["name"];
     $ext = end(explode(".", $namefile));
     $image_name=time().".".$ext;
     $fileUpload = move_uploaded_file($tmp_name,
     "uploads/".$image_name);
    }
    else
    {
     $image_name=$row['image'];
    }
     $sqlAdd ="update  tbl_customer set name='".$name."',
     image='".$image_name."'
     where  id=".$_GET['id'];
     mysql_query($sqlAdd);
     header("Location:add.php?id=".@$_GET['id']."&msg=success");
     exit;
    
}
else
{
  if($_SERVER["REQUEST_METHOD"] == "POST")
  {
    $name = $_POST["name"];
    IF($_FILES['file']['name']!='')
    {
     $tmp_name = $_FILES["file"]["tmp_name"];
     $namefile = $_FILES["file"]["name"];
     $ext = end(explode(".", $namefile));
     $image_name=time().".".$ext;
     $fileUpload = move_uploaded_file($tmp_name,
     "uploads/".$image_name);
    }
     $sqlAdd = mysql_query("insert into tbl_customer(name,image)
     VALUES('$name','$image_name')");
     header("Location:index.php?msg=success");
     exit;
   }
}
   ob_end_flush();
?>


 4.Show Record



  $allRecords = mysql_query('select * from tbl_customer ORDER BY
  id DESC limit 5');
  if(is_resource($allRecords))
  {
    while($row = mysql_fetch_assoc($allRecords))
    {
    ?>
    
     
    
class="checkbox">"checkbox"
    value=""
    name="ids[]" class="case"   />
    echo $row['name']; ?>
    echo $row['image'];
    ?>" height="30" width="30"
    />
                    
    
class="action"-->    echo $row['id']; ?>"-->
    Edit
    
    
    }
  }
?> 

5. Delete Record

if($_SERVER["REQUEST_METHOD"] == "POST")
 {
    if($_POST['action'] == "delete")
    {
          $ids = @implode(", ", $_POST['ids']);
          $qry = "SELECT * FROM  tbl_customer where id IN(".$ids.")";
          $result = mysql_query($qry);
          while($row = mysql_fetch_array($result))
          {
             $file='uploads/'.$row['image'];
             @unlink($file);
          }
          $sqlAdd ="delete from tbl_customer where id IN(".$ids.")";
          mysql_query($sqlAdd);
          header("Location:index.php?msg=success");
          exit;
    }
 }

Monday, 17 June 2013

AJAX FUNCTION

function checkcaptcha(val){
    $.ajax({
    type: "POST",
    url: "captcha.php?val="+val,
    }).done(function( msg ) {
    //alert(msg);
    document.getElementById("captchaval").innerHTML=(msg);
    return false;
    });
   
}

Sunday, 5 May 2013

Abstract Class Example


abstract class One{

public function disp(){

echo "Inside the parent class
";

}

}

class Two extends One{

public function disp(){

echo "Inside the child class
";

}

}

class Three extends One{

//no method is declared

}

$two=new Two();

echo "Calling from the child class Two:
";

$two->disp();

echo "Calling from the child class Three:
";

$three=new Three();

$three->disp();

?>

Monday, 1 April 2013

https://docs.google.com/forms/d/1zVa4KFmjzvYFhUhUGVeZFlGUmC9lxrtyvbUDg7_z-i8/viewform?pli=1



For Email the page help:-

most help full :- http://www.html-form-guide.com/contact-form/php-email-contact-form.html

http://thedemosite.co.uk/phpformmailer/source_code_php_form_mailer_more_secure_than_cgi_form_mailers.php


Saturday, 23 March 2013

how to get the last inserted id in sql query

retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
use the function mysql_insert_id()

Que.Warning: Cannot modify header information - headers already sent by.

Ans.use the function
ob_start() after where u start the session_start()

Thursday, 21 February 2013

str replace for replacing str

<?php
$arr = array("ram","shyam","sita","gita");
print_r(str_replace("shyam","ramesh",$arr,$i));
echo "Replacements: $i";
?>
The output of the code above will be:
Array
(
[0] =>ram
[1] =>ramesh
[2] =>sita
[3] =>gita
)
Replacements: 1