Sie sind auf Seite 1von 6

PHP PDO CRUD - Connect - Insert - Select -

Update - Delete
Create Db of Name "pdourdu"
After that you need to create table:

CREATE TABLE `users` (


`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`email` VARCHAR( 25 ) NOT NULL ,
`pass` VARCHAR( 25 ) NOT NULL
)

Create connection file name write as "connect.php"

<?php

try{
$con = new PDO ("mysql:host=localhost;dbname=pdourdu","root","");
echo "connected";
}
catch(PDOException $e)
{
echo "error:".$e->getMessage();
}

?>
Create Insert.php file

<?php

try{
$con = new PDO ("mysql:host=localhost;dbname=pdourdu","root","");

if(isset($_POST['done']))
{

$email = $_POST['email'];
$pass = $_POST['pass'];

$insert = $con->prepare("INSERT INTO users (email,pass)


VALUES(:email,:pass)");
$insert->bindParam(':email',$email);
$insert->bindParam(':pass',$pass);
$insert->execute();
echo "insert it";
}
}
catch(PDOException $e)
{
echo "error:".$e->getMessage();
}
?>
<form method="post">
<input type="text" name="email" placeholder="Email">
<input type="text" name="pass" placeholder="*****">
<input type="submit" name="done" >
</form>

Create select.php file

<table border="2">
<tr>
<th>ID</th>
<th>EMAIL</th>
<th>PASSWORD</th>
<th colspan="2">ACTION</th>

</tr>
<?php
try{
$con = new PDO ("mysql:host=localhost;dbname=pdourdu","root","");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$select = $con->prepare("SELECT * FROM users ");

$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
while($data=$select->fetch()){
?>

<tr>
<td><?php echo $data['id']; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $data['pass']; ?></td>
<td><a href="update.php?edit_id=<?php echo $data['id'];
?>">EDIT</a></td>
<td><a href="delete.php?del_id=<?php echo $data['id'];
?>">DELETE</a></td>

<?php
}
}
catch(PDOException $e)
{
echo "error:".$e->getMessage();
}

?>
</tr></table>

Create Update.php file

<?php
try{
$con = new PDO ("mysql:host=localhost;dbname=pdourdu","root","");

$edit_id = $_GET['edit_id'];

$select = $con->prepare("SELECT * FROM users where id='$edit_id' ");


$select->setFetchMode(PDO::FETCH_ASSOC);
$select->execute();
$data=$select->fetch();
if(isset($_POST['done']))
{

$email = $_POST['email'];
$pass = $_POST['pass'];

$update = $con->prepare("UPDATE users SET email=:email ,pass=:pass


where id='$edit_id'");
$update->bindParam(':email',$email);
$update->bindParam(':pass',$pass);
$update->execute();
header("location:select.php");
}
}
catch(PDOException $e)
{
echo "error:".$e->getMessage();
}
?>
<form method="post">
<input type="text" name="email" placeholder="Email" value="<?php echo
$data['email'] ?>">
<input type="text" name="pass" placeholder="*****" value="<?php echo
$data['pass'] ?>">
<input type="submit" name="done" >
</form>

Create Delete.php file


<?php

try{
$con = new PDO ("mysql:host=localhost;dbname=pdourdu","root","");

$del_id = $_GET['del_id'];

$DELETE = $con->prepare("DELETE FROM users where id='$del_id'");


$DELETE->execute();
header("location:select.php");

}
catch(PDOException $e)
{
echo "error:".$e->getMessage();
}

?>

Posted 16th November 2016 by Mujahid Khan

Labels: Notes Tutorials

Das könnte Ihnen auch gefallen