Monday, June 1, 2015

PHP: simple CRUD (Create, Read, Update and Delete)



Folder Structure
add_item.php
connect.php
detail_item.php
edit_item.php
function.php
index.php

add_item.php
<?php
include('connect.php');
include('function.php');

if (isset($_POST['insert'])) {
     $title = $_POST['title'];
     $content = $_POST['content'];
     $date = date('Y-m-d H:i:s');
     $slug = strtolower(url_title($title));
     mysql_query("insert into tb_crud(title, content, date, slug) values('".$title."', '".$content."', '".$date."', '".$slug."')");
     header("location:index.php");
}
?>

<form method="post" action="">
     <table width="100%">
          <tr>
               <td>Title</td>
               <td><input type="text" name="title" size="60" /></td>
          </tr>
          <tr>
               <td valign="top">Content</td>
               <td><textarea type="text" style="width:500px;height:300px" name="content"></textarea></td>
          </tr>
          <tr>
               <td> </td>
               <td><input type="submit" value="Submit" name="insert" /></td>
          </tr>
     </table>
</form>

connect.php
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'db_php_series';

$db_conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db, $db_conn);

detail_item.php
<table width="100%" border="1">
     <tr>
          <th>Title</th>
          <th>Content</th>
          <th>Date</th>
     </tr>
     <?php
     include('connect.php');
     if (isset($_GET['slug'])) {
          $detail = mysql_fetch_array(mysql_query("SELECT * FROM `tb_crud` WHERE `slug` = '".$_GET['slug']."'"));
     }
     echo '<tr>';
     echo '<td>'.$detail['title'].'</td>';
     echo '<td>'.$detail['content'].'</td>';
     echo '<td>'.$detail['date'].'</td>';
     echo '</tr>';
     ?>
</table>

edit_item.php
<?php
include('connect.php');
include('function.php');

if (isset($_GET['edit'])) {
     $update = mysql_fetch_array(mysql_query("select * from tb_crud where ID_crud = '".$_GET['edit']."'"));
}

if (isset($_POST['update'])) {
     $title = $_POST['title'];
     $content = $_POST['content'];
     $date = date('Y-m-d H:i:s');
     $slug = strtolower(url_title($title));
     mysql_query("UPDATE `tb_crud` SET `title`='".$title."',`content`='".$content ."',`date`='".$date."',`slug`='".$slug."' WHERE ID_crud = '".$_GET['edit']."'");
     header("location:index.php");
}
?>

<form method="post" action="">
     <table width="100%">
          <tr>
               <td>Title</td>
               <td><input type="text" name="title" size="60" value="<?php echo $update['title'];?>" /></td>
          </tr>
          <tr>
               <td valign="top">Content</td>
               <td><textarea type="text" style="width:500px;height:300px" name="content"><?php echo $update['content'];?></textarea></td>
          </tr>
          <tr>
               <td> </td>
               <td><input type="submit" value="Submit" name="update" /></td>
          </tr>
     </table>
</form>

function.php
<?php
function url_title($str, $separator = '-', $lowercase = FALSE) {
     if ($separator == 'dash') {
          $separator = '-';
     } else if ($separator == 'underscore') {
          $separator = '_';
     }
     $q_separator = preg_quote($separator);

     $trans = array(
          '&.+?;' => '',
          '[^a-z0-9 _-]' => '',
          '\s+' => $separator,
          '('.$q_separator.')+' => $separator
     );

     $str = strip_tags($str);

     foreach ($trans as $key => $val) {
          $str = preg_replace("#".$key."#i", $val, $str);
     }

     if ($lowercase === TRUE) {
          $str = strtolower($str);
     }
     return trim($str, $separator);
}
?>

index.php
<table width="100%" border="1">
     <tr>
          <th>Title</th>
          <th>Content</th>
          <th>Date</th>
          <th>Slug</th>
          <th>Options</th>
     </tr>

     <?php
     include('connect.php');

     $query = mysql_query("select * from tb_crud order by date desc");
     while ($data = mysql_fetch_array($query)) {
          echo '<tr>';
          echo '<td><a href="detail_item.php?slug='.$data['slug'].'">'.$data['title'].'</a></td>';
          echo '<td>'.$data['content'].'</td>';
          echo '<td>'.$data['date'].'</td>';
          echo '<td>'.$data['slug'].'</td>';
          echo '<td><a href="edit_item.php?edit='.$data['ID_crud'].'">Edit</a> | <a href="index.php?delete='.$data['ID_crud'].'">Delete</a></td>';
          echo '</tr>';
     }

     // delete script
     if (isset($_GET['delete'])) {
          mysql_query("DELETE FROM `tb_crud` WHERE `ID_crud` = '".$_GET['delete']."'");
          header("location:index.php");
     }
     ?>
</table>
<br/>
<a href="add_item.php">Add Item</a>

Categories: ,

0 comments:

Post a Comment

luvne.com resepkuekeringku.com desainrumahnya.com yayasanbabysitterku.com

Copyright © Click | Powered by Blogger