PDO Form value inserting into Database - Mostlikers

13 February, 2014

PDO Form value inserting into Database

previously  i posted on PDO basic tutorials so now i create small form using insert and update query in PDO. it's best helpful for Beginners to learn something about  PDO.



View code             Download

Database

Try to connect the database in PDO Query
CREATE TABLE `datas`
(
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `age` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
)

PHP

<?php
$hostname='localhost';$username='root';$password='';
$dbname='design';// Change Db name
try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname",$username,$password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
    if(isset($_POST['submit']))
    {
        $name=$_POST['name'];
        $email=$_POST['email'];
        $age=$_POST['age'];
        $insert =$dbh->prepare("INSERT INTO datas (name,age,email) values ('$name','$age','$email')");
        $insert->execute();
        echo "<script>alert('Add sucessful')</script>";
    }
    
    if(isset($_GET['Del']))
    {
       $dele=$_GET['Del'];
       $dell = "delete from `datas` where id='".$dele."'";
       $dells = $dbh->prepare($dell);
       $dells->execute();
       echo "<script>alert('Delete sucessful')</script>";
    }
    
    if(isset($_POST['update']))
    {
      $name=$_POST['name'];
      $id=$_POST['num'];
      $email=$_POST['email'];
      $age=$_POST['age'];
      $sql = "UPDATE `datas` SET name='".$name."', `age`='".$age."', email='".$email."' where id='".$id."'";
      $statement = $dbh->prepare($sql);
      $statement->execute();
    echo "<script>alert('update sucessful');window.location='pdo.php'</script>";
    }
     }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>U<body>
<form name="form" method="post">
<table width="400" border="1" cellpadding="1" cellspacing="0">
  <tr>
    <td colspan="2"><div align="center"><h3>Add Client Information</h3></div></td>
  </tr>
  <tr>
    <td width="192"><div align="center">Name</div></td>
    <td width="198"><input type="text" name="name" /></td>
  </tr>
  <tr>
    <td><div align="center">Age</div></td>
    <td><input type="text" name="email" /></td>
  </tr>
  <tr>
    <td><div align="center">Email</div></td>
    <td><input type="text" value="" name="age" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input type="submit" name="submit" value="submit" /></td>
  </tr>
</table>

</form>
<br /><br />
<?php 
if(isset($_GET['id']))
{
    $ids=$_GET['id'];
    $sql = "SELECT * FROM `datas` where id='".$ids."'";
foreach ($dbh->query($sql) as $row)
    {
?>
<h3>Edit User Details</h3>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" value="<?php  echo $row["id"]; ?>" name="num" />
<label>Name </label>
<input type="text" value="<?php  echo $row["name"]; ?>" name="name" /><br /><br />
<label>Email</label>
<input value="<?php  echo $row["email"]; ?>" type="text" name="email" /><br /><br />
<label>Age &nbsp;</label>
<input type="text" value="<?php  echo $row["age"]; ?>" name="age" /><br /><br />
<input type="submit" name="update" value="update" />
</form>
<?php } } else { } ?>
<table border="1" cellpadding="1" cellspacing="0" >
<tr>
<th>Name</th><th>Age</th><th>Email</th><th>Edit</th><th>Delete</th>
</tr>

<?php
$sql = "SELECT * FROM `datas` order by id Desc";
foreach ($dbh->query($sql) as $row)
    {
 ?>
<tr>
<td><?php  echo $row["name"]; ?></td>
<td><?php  echo $row["age"]; ?></td>
<td><?php  echo $row["email"];?></td>
<td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $row['id']; ?>" id="<?php echo $row['id']; ?>">EDIT</a></td>
<td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?Del=<?php echo $row['id']; ?>">Delete</a></td>
<?php } ?>
</table>
</body>
</html>

1 comment:

  1. Thank you for beautiful script. New to PDO. As the add and edit forms are basically identical, how do I get the add form to populate when edit is selected rather than a new form. Thanks for help in advance.

    ReplyDelete