<?php 
error_reporting(0);\
    /*-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: May 09, 2019 at 05:00 AM
-- Server version: 10.1.36-MariaDB
-- PHP Version: 7.2.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 

--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `contacts`
--

CREATE TABLE `contacts` (
  `id` int(5) NOT NULL,
  `fullname` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `contacts`
--



--
-- Indexes for dumped tables
--

--
-- Indexes for table `contacts`
--
ALTER TABLE `contacts`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `contacts`
--
ALTER TABLE `contacts`
  MODIFY `id` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

$dbh = new PDO("mysql:host=localhost;dbname=test", 'root', '');
$status = 'Insert';
//insert
$submit = $_POST['submit'];
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$editid = $_GET['editid'];
$deleteid = $_GET['deleteid'];


if ($submit == 'Insert'){
    
    $insql= $dbh->prepare("insert into contacts (fullname,email,phone) values (?,?,?)");
    $insql->bindParam(1,$fullname,PDO::PARAM_STR); 
    $insql->bindParam(2,$email,PDO::PARAM_STR); 
    $insql->bindParam(3,$phone,PDO::PARAM_STR); 
    $insql->execute();
    //print_r($insql->errorInfo());
    echo "Record Inserted<br>";

    
}



//delete

if (isset($deleteid)){
    $delsql = $dbh->prepare("delete from contacts where id = ?");
    $delsql->bindParam(1,$deleteid,PDO::PARAM_INT);
    $delsql->execute();
   // print_r($delsql->errorInfo());
    echo "record deleted!<br>";
    
}


//update
if ($submit == 'Update'){
    
    $upsql = $dbh->prepare("update contacts set fullname = ?,email = ?, phone = ? where id = ?");
    $upsql->bindParam(1,$fullname,PDO::PARAM_STR); 
    $upsql->bindParam(2,$email,PDO::PARAM_STR); 
    $upsql->bindParam(3,$phone,PDO::PARAM_STR); 
    $upsql->bindParam(4,$editid,PDO::PARAM_INT); 
    $upsql->execute();
    //print_r($upsql->errorInfo());
    echo 'Record Updated';    
    
}





if (isset($editid)){
    $status =  'Update';
   $editsql = $dbh->prepare("select fullname,email,phone from contacts where id = ?");
   $editsql->bindParam(1,$editid,PDO::PARAM_INT);
   $editsql->execute();
   $editrow = $editsql->fetch();
   $upfullname = $editrow['fullname'];
   $upemail = $editrow['email'];
   $upphone = $editrow['phone'];   
}

//read
$sql = $dbh->prepare("select id,fullname from contacts");
$sql->execute();
while ($row = $sql->fetch()){
    $id = $row['id'];
    $fullname = $row['fullname'];
    echo $fullname.' <a href ="contacts.php?deleteid='.$id.'" onclick="return confirm(\'are you sure you want to delete '.$fullname.'?\');">Delete</a> <a href ="contacts.php?editid='.$id.'">Edit</a><br>';
}
if (isset($editid)){
 echo '<a href="contacts.php">Return to Add Mode</a><br>';
}

?>


<form action="contacts.php?<?php echo $_SERVER['QUERY_STRING'];?>" method="post">
    <input type="hidden" name="idtoedit" value="<?php echo $editid;?>">
Name: <input type="text" name="fullname" value="<?php echo $upfullname;?>"><br> 
Email: <input type="email" name="email" value="<?php echo $upemail;?>"><br> 
Phone: <input type="tel" name="phone" value="<?php echo $upphone;?>"><br> 
<input type="submit" name="submit" value="<?php echo $status;?>">
</form>