Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

How to Import and Export CSV Files Using PHP and MySQL

How to Import and Export CSV Files Using PHP and MySQL


Hi Today discussed PHP Server side Scripting Language How to Import and Export CSV Files using PHP and mysql. Database normally insert and update and delete queries and bulk insert it's means csv file same column file data csv file data insert mysql. code follows.


How to Import and Export CSV Files Using PHP and MySQL
How to Import and Export CSV Files Using PHP and MySQL



First Create database and then create table.

CREATE DATABASE employee
Create table

CREATE TABLE employeedetils(
eid VARCHAR(50) UNSIGNED PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
create_date VARCHAR(50)
)
dbconnect

<?php
function getdb(){
$servername = "localhost";
$username = "root";
$password = "";
$db = "employee";

try {
   
    $conn = mysqli_connect($servername, $username, $password, $db);
     //echo "Connected successfully"; 
    }
catch(exception $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
    return $conn;
}
?>
PHP File Import function phpfiles.php

<?php


 if(isset($_POST["Import"])){
  
  $filename=$_FILES["file"]["tmp_name"];  


   if($_FILES["file"]["size"] > 0)
   {
     $file = fopen($filename, "r");
         while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
          {


            $sql = "INSERT into employeedetils(eid,firstname,lastname,email,create_date)                    values ('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."')";
                   $result = mysqli_query($con, $sql);
    if(!isset($result))
    {
     echo "<script type=\"text/javascript\">
       alert(\"Invalid File:Please Upload CSV File.\");
       window.location = \"index.php\"
        </script>";  
    }
    else {
       echo "<script type=\"text/javascript\">
      alert(\"CSV File has been successfully Imported.\");
      window.location = \"index.php\"
     </script>";
    }
          }
   
          fclose($file); 
   }
 }  


 ?>

View Page design bootstrap lib include

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>
View Page


<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>

</head>

<body>
    <div id="wrap">
        <div class="container">
            <div class="row">

                <form class="form-horizontal" action="phpfiles.php" method="post" name="upload_excel" enctype="multipart/form-data">
                    <fieldset>

                        <!-- Form Name -->
                        <legend>Form Name</legend>

                        <!-- File Button -->
                        <div class="form-group">
                            <label class="col-md-4 control-label" for="filebutton">Select File</label>
                            <div class="col-md-4">
                                <input type="file" name="file" id="file" class="input-large">
                            </div>
                        </div>

                        <!-- Button -->
                        <div class="form-group">
                            <label class="col-md-4 control-label" for="singlebutton">Import data</label>
                            <div class="col-md-4">
                                <button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Import</button>
                            </div>
                        </div>

                    </fieldset>
                </form>

            </div>
            <?php
               getdbrecords();
            ?>
        </div>
    </div>
</body>

</html>
view php records

function getdbrecords(){
    $con = getdb();
    $Sql = "SELECT * FROM employeedetils";
    $result = mysqli_query($con, $Sql);  


    if (mysqli_num_rows($result) > 0) {
     echo "<div class='table-responsive'><table id='myTable' class='table table-striped table-bordered'>
             <thead><tr><th>EMP ID</th>
                          <th>First Name</th>
                          <th>Last Name</th>
                          <th>Email</th>
                          <th>Registration Date</th>
                        </tr></thead><tbody>";


     while($row = mysqli_fetch_assoc($result)) {

         echo "<tr><td>" . $row['eid']."</td>
                   <td>" . $row['firstname']."</td>
                   <td>" . $row['lastname']."</td>
                   <td>" . $row['email']."</td>
                   <td>" . $row['create_date']."</td></tr>";        
     }
    
     echo "</tbody></table></div>";
     
} else {
     echo "you have no records";
}
}

Export MySQL to CSV With PHP

<div>
            <form class="form-horizontal" action="phpfiles.php" method="post" name="upload_excel"   
                      enctype="multipart/form-data">
                  <div class="form-group">
                            <div class="col-md-4 col-md-offset-4">
                                <input type="submit" name="Export" class="btn btn-success" value="export to excel"/>
                            </div>
                   </div>                    
            </form>           
 </div>
Export Functions

if(isset($_POST["Export"])){
   
      header('Content-Type: text/csv; charset=utf-8');  
      header('Content-Disposition: attachment; filename=data.csv');  
      $output = fopen("php://output", "w");  
      fputcsv($output, array('ID', 'First Name', 'Last Name', 'Email', 'Joining Date'));  
      $query = "SELECT * from employeedetils ORDER BY emp_id DESC";  
      $result = mysqli_query($con, $query);  
      while($row = mysqli_fetch_assoc($result))  
      {  
           fputcsv($output, $row);  
      }  
      fclose($output);  
 }  

List of Timezone GMT Database Query Mysql

List of Timezone GMT Database Query Mysql

Hi Today Discussed List of Timezone GMT Database Query Mysql normally php mysql basic database country timezone and state city this basic normally database stored so timezone database followes queries.

List of Timezone GMT Database Query Mysql
List of Timezone GMT Database Query Mysql

CREATE TABLE `zone` (
  `id` int(11) NOT NULL,
  `country_code` varchar(255) NOT NULL,
  `zone_name` text NOT NULL,
  `status` int(11) NOT NULL COMMENT '0-inactive 1-active'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Database Value


INSERT INTO `zone` (`id`, `country_code`, `zone_name`, `status`) VALUES
(1, 'Pacific/Kwajalein', '(GMT -12:00) Eniwetok, Kwajalein', 0),
(2, 'Pacific/Samoa', '(GMT -11:00) Midway Island, Samoa', 0),
(3, 'Pacific/Honolulu', '(GMT -10:00) Hawaii', 0),
(4, 'America/Anchorage', '(GMT -9:00) Alaska', 0),
(5, 'America/Los_Angeles', '(GMT -8:00) Pacific Time (US & Canada) Los Angeles, Seattle', 0),
(6, 'America/Denver', '(GMT -7:00) Mountain Time (US & Canada) Denver', 0),
(7, 'America/Chicago', '(GMT -6:00) Central Time (US & Canada), Chicago, Mexico City', 0),
(8, 'America/New_York', '(GMT -5:00) Eastern Time (US & Canada), New York, Bogota, Lima', 0),
(9, 'Atlantic/Bermuda', '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz', 0),
(10, 'Canada/Newfoundland', '(GMT -3:30) Newfoundland', 0),
(11, 'Brazil/East', '(GMT -3:00) Brazil, Buenos Aires, Georgetown', 0),
(12, 'Atlantic/Azores', '(GMT -2:00) Mid-Atlantic', 0),
(13, 'Atlantic/Cape_Verde', '(GMT -1:00 hour) Azores, Cape Verde Islands', 0),
(14, 'Europe/London', '(GMT) Western Europe Time, London, Lisbon, Casablanca', 0),
(15, 'Europe/Brussels', '(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris', 0),
(16, 'Europe/Helsinki', '(GMT +2:00) Kaliningrad, South Africa', 0),
(17, 'Asia/Baghdad', '(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg', 0),
(18, 'Asia/Tehran', '(GMT +3:30) Tehran', 0),
(19, 'Asia/Baku', '(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi', 0),
(20, 'Asia/Kabul', '(GMT +4:30) Kabul', 0),
(21, 'Asia/Karachi', '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent', 1),
(22, 'Asia/Calcutta', '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi', 0),
(23, 'Asia/Dhaka', '(GMT +6:00) Almaty, Dhaka, Colombo', 0),
(24, 'Asia/Bangkok', '(GMT +7:00) Bangkok, Hanoi, Jakarta', 0),
(25, 'Asia/Hong_Kong', '(GMT +8:00) Beijing, Perth, Singapore, Hong Kong', 0),
(26, 'Asia/Tokyo', '(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk', 0),
(27, 'Australia/Adelaide', '(GMT +9:30) Adelaide, Darwin', 0),
(28, 'Pacific/Guam', '(GMT +10:00) Eastern Australia, Guam, Vladivostok', 0),
(29, 'Asia/Magadan', '(GMT +11:00) Magadan, Solomon Islands, New Caledonia', 0),
(30, 'Pacific/Fiji', '(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka', 0);

Country And Currency Database

Currency Database

Hi Today discussed Country And Currency Database mysql database basic database country currency symbol and separator database followus code.

Country And Currency Database
Country And Currency Database


CREATE TABLE IF NOT EXISTS `currencies` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `country` varchar(100) DEFAULT NULL,
  `currency` varchar(100) DEFAULT NULL,
  `code` varchar(25) DEFAULT NULL,
  `symbol` varchar(25) DEFAULT NULL,
  `thousand_separator` varchar(10) DEFAULT NULL,
  `decimal_separator` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=132 DEFAULT CHARSET=utf8;

-- Dumping data for table invoice_new.currencies: 131 rows
/*!40000 ALTER TABLE `currencies` DISABLE KEYS */;
INSERT INTO `currencies` (`id`, `country`, `currency`, `code`, `symbol`, 
`thousand_separator`, `decimal_separator`) VALUES
 (1, 'Albania', 'Leke', 'ALL', 'Lek', ',', '.'),
 (2, 'America', 'Dollars', 'USD', '$', ',', '.'),
 (3, 'Afghanistan', 'Afghanis', 'AF', '؋', ',', '.'),
 (4, 'Argentina', 'Pesos', 'ARS', '$', ',', '.'),
 (5, 'Aruba', 'Guilders', 'AWG', 'ƒ', ',', '.'),
 (6, 'Australia', 'Dollars', 'AUD', '$', ',', '.'),
 (7, 'Azerbaijan', 'New Manats', 'AZ', 'ман', ',', '.'),
 (8, 'Bahamas', 'Dollars', 'BSD', '$', ',', '.'),
 (9, 'Barbados', 'Dollars', 'BBD', '$', ',', '.'),
 (10, 'Belarus', 'Rubles', 'BYR', 'p.', ',', '.'),
 (11, 'Belgium', 'Euro', 'EUR', '€', ',', '.'),
 (12, 'Beliz', 'Dollars', 'BZD', 'BZ$', ',', '.'),
 (13, 'Bermuda', 'Dollars', 'BMD', '$', ',', '.'),
 (14, 'Bolivia', 'Bolivianos', 'BOB', '$b', ',', '.'),
 (15, 'Bosnia and Herzegovina', 'Convertible Marka', 'BAM', 'KM', ',', '.'),
 (16, 'Botswana', 'Pula\'s', 'BWP', 'P', ',', '.'),
 (17, 'Bulgaria', 'Leva', 'BG', 'лв', ',', '.'),
 (18, 'Brazil', 'Reais', 'BRL', 'R$', ',', '.'),
 (19, 'Britain (United Kingdom)', 'Pounds', 'GBP', '£', ',', '.'),
 (20, 'Brunei Darussalam', 'Dollars', 'BND', '$', ',', '.'),
 (21, 'Cambodia', 'Riels', 'KHR', '៛', ',', '.'),
 (22, 'Canada', 'Dollars', 'CAD', '$', ',', '.'),
 (23, 'Cayman Islands', 'Dollars', 'KYD', '$', ',', '.'),
 (24, 'Chile', 'Pesos', 'CLP', '$', ',', '.'),
 (25, 'China', 'Yuan Renminbi', 'CNY', '¥', ',', '.'),
 (26, 'Colombia', 'Pesos', 'COP', '$', ',', '.'),
 (27, 'Costa Rica', 'Colón', 'CRC', '₡', ',', '.'),
 (28, 'Croatia', 'Kuna', 'HRK', 'kn', ',', '.'),
 (29, 'Cuba', 'Pesos', 'CUP', '₱', ',', '.'),
 (30, 'Cyprus', 'Euro', 'EUR', '€', ',', '.'),
 (31, 'Czech Republic', 'Koruny', 'CZK', 'Kč', ',', '.'),
 (32, 'Denmark', 'Kroner', 'DKK', 'kr', ',', '.'),
 (33, 'Dominican Republic', 'Pesos', 'DOP ', 'RD$', ',', '.'),
 (34, 'East Caribbean', 'Dollars', 'XCD', '$', ',', '.'),
 (35, 'Egypt', 'Pounds', 'EGP', '£', ',', '.'),
 (36, 'El Salvador', 'Colones', 'SVC', '$', ',', '.'),
 (37, 'England (United Kingdom)', 'Pounds', 'GBP', '£', ',', '.'),
 (38, 'Euro', 'Euro', 'EUR', '€', ',', '.'),
 (39, 'Falkland Islands', 'Pounds', 'FKP', '£', ',', '.'),
 (40, 'Fiji', 'Dollars', 'FJD', '$', ',', '.'),
 (41, 'France', 'Euro', 'EUR', '€', ',', '.'),
 (42, 'Ghana', 'Cedis', 'GHC', '¢', ',', '.'),
 (43, 'Gibraltar', 'Pounds', 'GIP', '£', ',', '.'),
 (44, 'Greece', 'Euro', 'EUR', '€', ',', '.'),
 (45, 'Guatemala', 'Quetzales', 'GTQ', 'Q', ',', '.'),
 (46, 'Guernsey', 'Pounds', 'GGP', '£', ',', '.'),
 (47, 'Guyana', 'Dollars', 'GYD', '$', ',', '.'),
 (48, 'Holland (Netherlands)', 'Euro', 'EUR', '€', ',', '.'),
 (49, 'Honduras', 'Lempiras', 'HNL', 'L', ',', '.'),
 (50, 'Hong Kong', 'Dollars', 'HKD', '$', ',', '.'),
 (51, 'Hungary', 'Forint', 'HUF', 'Ft', ',', '.'),
 (52, 'Iceland', 'Kronur', 'ISK', 'kr', ',', '.'),
 (53, 'India', 'Rupees', 'INR', 'Rp', ',', '.'),
 (54, 'Indonesia', 'Rupiahs', 'IDR', 'Rp', ',', '.'),
 (55, 'Iran', 'Rials', 'IRR', '﷼', ',', '.'),
 (56, 'Ireland', 'Euro', 'EUR', '€', ',', '.'),
 (57, 'Isle of Man', 'Pounds', 'IMP', '£', ',', '.'),
 (58, 'Israel', 'New Shekels', 'ILS', '₪', ',', '.'),
 (59, 'Italy', 'Euro', 'EUR', '€', ',', '.'),
 (60, 'Jamaica', 'Dollars', 'JMD', 'J$', ',', '.'),
 (61, 'Japan', 'Yen', 'JPY', '¥', ',', '.'),
 (62, 'Jersey', 'Pounds', 'JEP', '£', ',', '.'),
 (63, 'Kazakhstan', 'Tenge', 'KZT', 'лв', ',', '.'),
 (64, 'Korea (North)', 'Won', 'KPW', '₩', ',', '.'),
 (65, 'Korea (South)', 'Won', 'KRW', '₩', ',', '.'),
 (66, 'Kyrgyzstan', 'Soms', 'KGS', 'лв', ',', '.'),
 (67, 'Laos', 'Kips', 'LAK', '₭', ',', '.'),
 (68, 'Latvia', 'Lati', 'LVL', 'Ls', ',', '.'),
 (69, 'Lebanon', 'Pounds', 'LBP', '£', ',', '.'),
 (70, 'Liberia', 'Dollars', 'LRD', '$', ',', '.'),
 (71, 'Liechtenstein', 'Switzerland Francs', 'CHF', 'CHF', ',', '.'),
 (72, 'Lithuania', 'Litai', 'LTL', 'Lt', ',', '.'),
 (73, 'Luxembourg', 'Euro', 'EUR', '€', ',', '.'),
 (74, 'Macedonia', 'Denars', 'MKD', 'ден', ',', '.'),
 (75, 'Malaysia', 'Ringgits', 'MYR', 'RM', ',', '.'),
 (76, 'Malta', 'Euro', 'EUR', '€', ',', '.'),
 (77, 'Mauritius', 'Rupees', 'MUR', '₨', ',', '.'),
 (78, 'Mexico', 'Pesos', 'MX', '$', ',', '.'),
 (79, 'Mongolia', 'Tugriks', 'MNT', '₮', ',', '.'),
 (80, 'Mozambique', 'Meticais', 'MZ', 'MT', ',', '.'),
 (81, 'Namibia', 'Dollars', 'NAD', '$', ',', '.'),
 (82, 'Nepal', 'Rupees', 'NPR', '₨', ',', '.'),
 (83, 'Netherlands Antilles', 'Guilders', 'ANG', 'ƒ', ',', '.'),
 (84, 'Netherlands', 'Euro', 'EUR', '€', ',', '.'),
 (85, 'New Zealand', 'Dollars', 'NZD', '$', ',', '.'),
 (86, 'Nicaragua', 'Cordobas', 'NIO', 'C$', ',', '.'),
 (87, 'Nigeria', 'Nairas', 'NG', '₦', ',', '.'),
 (88, 'North Korea', 'Won', 'KPW', '₩', ',', '.'),
 (89, 'Norway', 'Krone', 'NOK', 'kr', ',', '.'),
 (90, 'Oman', 'Rials', 'OMR', '﷼', ',', '.'),
 (91, 'Pakistan', 'Rupees', 'PKR', '₨', ',', '.'),
 (92, 'Panama', 'Balboa', 'PAB', 'B/.', ',', '.'),
 (93, 'Paraguay', 'Guarani', 'PYG', 'Gs', ',', '.'),
 (94, 'Peru', 'Nuevos Soles', 'PE', 'S/.', ',', '.'),
 (95, 'Philippines', 'Pesos', 'PHP', 'Php', ',', '.'),
 (96, 'Poland', 'Zlotych', 'PL', 'zł', ',', '.'),
 (97, 'Qatar', 'Rials', 'QAR', '﷼', ',', '.'),
 (98, 'Romania', 'New Lei', 'RO', 'lei', ',', '.'),
 (99, 'Russia', 'Rubles', 'RUB', 'руб', ',', '.'),
 (100, 'Saint Helena', 'Pounds', 'SHP', '£', ',', '.'),
 (101, 'Saudi Arabia', 'Riyals', 'SAR', '﷼', ',', '.'),
 (102, 'Serbia', 'Dinars', 'RSD', 'Дин.', ',', '.'),
 (103, 'Seychelles', 'Rupees', 'SCR', '₨', ',', '.'),
 (104, 'Singapore', 'Dollars', 'SGD', '$', ',', '.'),
 (105, 'Slovenia', 'Euro', 'EUR', '€', ',', '.'),
 (106, 'Solomon Islands', 'Dollars', 'SBD', '$', ',', '.'),
 (107, 'Somalia', 'Shillings', 'SOS', 'S', ',', '.'),
 (108, 'South Africa', 'Rand', 'ZAR', 'R', ',', '.'),
 (109, 'South Korea', 'Won', 'KRW', '₩', ',', '.'),
 (110, 'Spain', 'Euro', 'EUR', '€', ',', '.'),
 (111, 'Sri Lanka', 'Rupees', 'LKR', '₨', ',', '.'),
 (112, 'Sweden', 'Kronor', 'SEK', 'kr', ',', '.'),
 (113, 'Switzerland', 'Francs', 'CHF', 'CHF', ',', '.'),
 (114, 'Suriname', 'Dollars', 'SRD', '$', ',', '.'),
 (115, 'Syria', 'Pounds', 'SYP', '£', ',', '.'),
 (116, 'Taiwan', 'New Dollars', 'TWD', 'NT$', ',', '.'),
 (117, 'Thailand', 'Baht', 'THB', '฿', ',', '.'),
 (118, 'Trinidad and Tobago', 'Dollars', 'TTD', 'TT$', ',', '.'),
 (119, 'Turkey', 'Lira', 'TRY', 'TL', ',', '.'),
 (120, 'Turkey', 'Liras', 'TRL', '£', ',', '.'),
 (121, 'Tuvalu', 'Dollars', 'TVD', '$', ',', '.'),
 (122, 'Ukraine', 'Hryvnia', 'UAH', '₴', ',', '.'),
 (123, 'United Kingdom', 'Pounds', 'GBP', '£', ',', '.'),
 (124, 'United States of America', 'Dollars', 'USD', '$', ',', '.'),
 (125, 'Uruguay', 'Pesos', 'UYU', '$U', ',', '.'),
 (126, 'Uzbekistan', 'Sums', 'UZS', 'лв', ',', '.'),
 (127, 'Vatican City', 'Euro', 'EUR', '€', ',', '.'),
 (128, 'Venezuela', 'Bolivares Fuertes', 'VEF', 'Bs', ',', '.'),
 (129, 'Vietnam', 'Dong', 'VND', '₫', ',', '.'),
 (130, 'Yemen', 'Rials', 'YER', '﷼', ',', '.'),
 (131, 'Zimbabwe', 'Zimbabwe Dollars', 'ZWD', 'Z$', ',', '.');

Insert,Update,Delete PHP Using Ajax

Insert,Update,Delete PHP Using Ajax 


Hi Today Discussed Core PHP Insert,Update,Delete php using Ajax, Php insert view update delete without loading browser so data pasing ajax follows code.

Insert,Update,Delete PHP Using Ajax
Insert,Update,Delete PHP Using Ajax 

sql

CREATE TABLE IF NOT EXISTS `user_detail` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

list.php

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript" src="modify.js"></script>
</head>
<body>
<div id="wrapper">

<?php
$host="localhost";
$username="root";
$password="";
$databasename="ajaxphp";
$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

$select =mysql_query("SELECT * FROM user_detail");
?>

<table align="center" cellpadding="10" border="1" id="user_table">
<tr>
<th>NAME</th>
<th>AGE</th>
<th></th>
</tr>
<?php
while ($row=mysql_fetch_array($select)) 
{
 ?>
 <tr id="row<?php echo $row['id'];?>">
  <td id="name_val<?php echo $row['id'];?>"><?php echo $row['name'];?></td>
  <td id="age_val<?php echo $row['id'];?>"><?php echo $row['age'];?></td>
  <td>
   <input type='button' class="edit_button" id="edit_button<?php echo $row['id'];?>" value="edit" onclick="edit_row('<?php echo $row['id'];?>');">
   <input type='button' class="save_button" id="save_button<?php echo $row['id'];?>" value="save" onclick="save_row('<?php echo $row['id'];?>');">
   <input type='button' class="delete_button" id="delete_button<?php echo $row['id'];?>" value="delete" onclick="delete_row('<?php echo $row['id'];?>');">
  </td>
 </tr>
 <?php
}
?>

<tr id="new_row">
 <td><input type="text" id="new_name"></td>
 <td><input type="text" id="new_age"></td>
 <td><input type="button" value="Insert Row" onclick="insert_row();"></td>
</tr>
</table>

</div>
</body>
</html>
modify.js

function edit_row(id)
{
 var name=document.getElementById("name_val"+id).innerHTML;
 var age=document.getElementById("age_val"+id).innerHTML;

 document.getElementById("name_val"+id).innerHTML="<input type='text' id='name_text"+id+"' value='"+name+"'>";
 document.getElementById("age_val"+id).innerHTML="<input type='text' id='age_text"+id+"' value='"+age+"'>";
 
 document.getElementById("edit_button"+id).style.display="none";
 document.getElementById("save_button"+id).style.display="block";
}

function save_row(id)
{
 var name=document.getElementById("name_text"+id).value;
 var age=document.getElementById("age_text"+id).value;
 
 $.ajax
 ({
  type:'post',
  url:'modify_records.php',
  data:{
   edit_row:'edit_row',
   row_id:id,
   name_val:name,
   age_val:age
  },
  success:function(response) {
   if(response=="success")
   {
    document.getElementById("name_val"+id).innerHTML=name;
    document.getElementById("age_val"+id).innerHTML=age;
    document.getElementById("edit_button"+id).style.display="block";
    document.getElementById("save_button"+id).style.display="none";
   }
  }
 });
}

function delete_row(id)
{
 $.ajax
 ({
  type:'post',
  url:'modify_records.php',
  data:{
   delete_row:'delete_row',
   row_id:id,
  },
  success:function(response) {
   if(response=="success")
   {
    var row=document.getElementById("row"+id);
    row.parentNode.removeChild(row);
   }
  }
 });
}

function insert_row()
{
 var name=document.getElementById("new_name").value;
 var age=document.getElementById("new_age").value;

 $.ajax
 ({
  type:'post',
  url:'modify_records.php',
  data:{
   insert_row:'insert_row',
   name_val:name,
   age_val:age
  },
  success:function(response) {
   if(response!="")
   {
    var id=response;
    var table=document.getElementById("user_table");
    var table_len=(table.rows.length)-1;
    var row = table.insertRow(table_len).outerHTML="<tr id='row"+id+"'><td id='name_val"+id+"'>"+name+"</td><td id='age_val"+id+"'>"+age+"</td><td><input type='button' class='edit_button' id='edit_button"+id+"' value='edit' onclick='edit_row("+id+");'/><input type='button' class='save_button' id='save_button"+id+"' value='save' onclick='save_row("+id+");'/><input type='button' class='delete_button' id='delete_button"+id+"' value='delete' onclick='delete_row("+id+");'/></td></tr>";

    document.getElementById("new_name").value="";
    document.getElementById("new_age").value="";
   }
  }
 });
}
modify_record.php

<?php
$host="localhost";
$username="root";
$password="";
$databasename="ajaxphp";

$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

if(isset($_POST['edit_row']))
{
 $row=$_POST['row_id'];
 $name=$_POST['name_val'];
 $age=$_POST['age_val'];

 mysql_query("update user_detail set name='$name',age='$age' where id='$row'");
 echo "success";
 exit();
}

if(isset($_POST['delete_row']))
{
 $row_no=$_POST['row_id'];
 mysql_query("delete from user_detail where id='$row_no'");
 echo "success";
 exit();
}

if(isset($_POST['insert_row']))
{
 $name=$_POST['name_val'];
 $age=$_POST['age_val'];
 mysql_query("insert into user_detail values('','$name','$age')");
 echo mysql_insert_id();
 exit();
}
?>

Automatically create cache file with php And Quickly Loaded in PHP Files

Automatically create cache file with php And Quickly Loaded in PHP Files


Hi Today Discussed Automatically create cache file with php and Quickly loaded in php files large number of data in mysql database is once run this file the data automatically create cache file and then quickly loaded this file follows briefly discussed.

Automatically create cache file with php And Quickly Loaded in PHP Files
Automatically create cache file with php And Quickly Loaded in PHP Files

Your php file quick load speed  increased your web site each every page loaded auto created cache file follows code.

<?php
function getUrl () {
    if (!isset($_SERVER['REQUEST_URI'])) {
    $url = $_SERVER['REQUEST_URI'];
    } else {
    $url = $_SERVER['SCRIPT_NAME'];
    $url .= (!empty($_SERVER['QUERY_STRING']))? '?' . $_SERVER[ 'QUERY_STRING' ] : '';

    }
    return $url;
}

//getUrl gets the queried page with query string
function cache ($buffer) { //page's content is $buffer
    $url = getUrl();
    $filename = md5($url) . '.cache';
    $data = time() . '¦' . $buffer;
    $filew = fopen("cache/" . $filename, 'w');
    fwrite($filew, $data);
    fclose($filew);
    return $buffer;
}

function display () {
    $url = getUrl();
    $filename = md5($url) . '.cache';
    if (!file_exists("/cache/" . $filename)) {
    return false;
    }
    $filer = fopen("cache/" . $filename, 'r');
    $data = fread($filer, filesize("cache/" . $filename));
    fclose($filer);
    $content = explode('¦', $data, 2);
    if (count($content)!= 2 OR !is_numeric($content['0'])) {
        return false;
    }
    if (time()-(100) > $content['0']) { // 100 is the cache time here!!!
        return false;
    }
        echo $content['1'];
        die();
}

// Display cache (if any)
display();  // if it is displayed, die function will end the program here.

// if no cache, callback cache
ob_start ('cache');
?>

PHP Execute a HTTP POST Using PHP CURL

PHP Execute a HTTP POST Using PHP CURL

Today Discussed PHP Curl Post Means PHP Execute a HTTP POST Using PHP Curl.Well, there's a hurdle -- the information isn't going to be saved on the localhost database -- it needs to be stored in a remote database that I cannot connect directly to.

PHP Execute a HTTP POST Using PHP CURL
PHP Execute a HTTP POST Using PHP CURL

  1. User will submit the form, as usual.
  2. In the form processing PHP, I use cURL to execute a POST transmission to a PHP script on the customer's server.
  3. The remote script would do a MySQL INSERT query into the customer's private database.
This solution worked quite well so I thought I'd share it with you. Here's how you execute a POST using the PHP CURL library.


Curl Post Code Follows

//extract data from the post
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
 'lname' => urlencode($_POST['last_name']),
 'fname' => urlencode($_POST['first_name']),
 'title' => urlencode($_POST['title']),
 
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);