C.R.U.D. Charles Severance

Slides:



Advertisements
Similar presentations
Introduction to PHP Dr. Charles Severance
Advertisements

Keys, Referential Integrity and PHP One to Many on the Web.
PHP Functions / Modularity
NMD202 Web Scripting Week5. What we will cover today PHPmyAdmin Debugging – using print_r Modifying Data PHP (cont.) 4D Methodology File and IO operations.
Database-driven Web Pages from Access Databases Stephen Rondeau TINST May 2009.
Accessing MySQL Using PDO
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
Forms and PHP Dr. Charles Severance
Cookies, Sessions, and Authentication Dr. Charles Severance
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
MS Access Database Connection. Database? A database is a program that stores data and records in a structured and queryable format. The tools that are.
PHP Part 2.
Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.
PHP Arrays Dr. Charles Severance
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Document Management System for Rhodes University Supervisor: John Ebden Presenter: Bijal Rana.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
PHP Functions Dr. Charles Severance
Accessing MySQL Using PDO Charles Severance
Accessing MySQL Using PDO Charles Severance
Forms and PHP Dr. Charles Severance
Relational Databases and MySQL Charles Severance
PHP Arrays Dr. Charles Severance
PHP Functions / Modularity Dr. Charles Severance
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Using Handlebars Dr. Charles Severance
PHP and MySQL Session 4: Advanced PHP Izzy
Dr. Charles Severance Using Handlebars Dr. Charles Severance
Accessing MySQL Using PDO
Introduction to Dynamic Web Content
PHP Arrays Dr. Charles Severance
CHAPTER 5 SERVER SIDE SCRIPTING
Our Technologies Dr. Charles Severance
Reading Files Chapter 7 Python for Everybody
HTML Charles Severance
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
PHP Functions / Modularity
Transactions Dr. Charles Severance
Cascading Style Sheets
Getting PHP Hosting Dr. Charles Severance
Using JSON Dr. Charles Severance
PHP: Login FdSc Module 109 Server side scripting and Database design
Redirect, Routing, and Authentication
Relational Databases and MySQL
Loops and Iteration Chapter 5 Python for Everybody
HTTP Parameters and Arrays
Functions Chapter 4 Python for Everybody
Accessing MySQL Using PDO
PHP Arrays Dr. Charles Severance
Passing variables between pages
Object Oriented Programming in JavaScript
BASIC PHP and MYSQL Edward S. Flores.
Expressions and Control Flow in PHP
Basic Contact Form user sends an
Tuples Chapter 10 Python for Everybody
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
MS Access Database Connection
PHP Namespaces (in progress)
Introduction to Dynamic Web Content
CIS 388 Internet Programming
Web Development & Design Foundations with H T M L 5
Using jQuery Dr. Charles Severance
Web Programming Week 4 Old Dominion University
Charles Severance Single Table SQL.
Data Modelling Many to Many
Web Programming Week 4 Old Dominion University
Model View Controller (MVC)
Presentation transcript:

C.R.U.D. Charles Severance www.wa4e.com http://www.wa4e.com/code/crud.zip

CRUD Pattern When we store things in database tables we generally need Create - Insert a new row Read - Read existing row(s) Update - Change some values of a record Delete - Delete a record So far we have done most of CRUD

Our Program is a Little Ugly Usually we create several screens Add new row View all rows (paging) View single row Edit single row Delete a row

Five Separate Files index.php - Main list and links to other files add.php - Add a new entry delete.php - Delete an entry edit.php - Edit existing view.php (if index.php needs a detail view)

index.php <?php require_once "pdo.php"; session_start(); ?> <html><head></head> <body> if ( isset($_SESSION['error']) ) {     echo '<p style="color:red">'.$_SESSION['error']."</p>\n";     unset($_SESSION['error']); } if ( isset($_SESSION['success']) ) {     echo '<p style="color:green">'.$_SESSION['success']."</p>\n";     unset($_SESSION['success']); echo('<table border="1">'."\n"); index.php

index.php <?php echo('<table border="1">'."\n"); $stmt = $pdo->query("SELECT name, email, password, user_id FROM users"); while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {     echo "<tr><td>";     echo(htmlentities($row['name']));     echo("</td><td>");     echo(htmlentities($row['email']));     echo(htmlentities($row['password']));     echo('<a href="edit.php?user_id='.$row['user_id'].'">Edit</a> / ');     echo('<a href="delete.php?user_id='. $row['user_id'].'">Delete</a>');     echo("\n</form>\n");     echo("</td></tr>\n"); } ?> </table> <a href="add.php">Add New</a>

<tr><td>Chuck</td><td>csev@umich <tr><td>Chuck</td><td>csev@umich.edu</td><td>123</td><td> <a href="edit.php?user_id=1">Edit</a> / <a href="delete.php?user_id=1">Delete</a></td></tr> <tr><td>Glenn</td><td>gg@umich.edu</td><td>456</td><td> <a href="edit.php?user_id=2">Edit</a> / <a href="delete.php?user_id=2">Delete</a></td></tr>

add.php <?php require_once "pdo.php"; session_start(); if ( isset($_POST['name']) && isset($_POST['email'])       && isset($_POST['password'])) {     $sql = "INSERT INTO users (name, email, password)                VALUES (:name, :email, :password)";     $stmt = $pdo->prepare($sql);     $stmt->execute(array(         ':name' => $_POST['name'],         ':email' => $_POST['email'],         ':password' => $_POST['password']));    $_SESSION['success'] = 'Record Added';    header( 'Location: index.php' ) ;    return; } ?> <p>Add A New User</p> <form method="post"> <p>Name:<input type="text" name="name"></p> <p>Email:<input type="text" name="email"></p> <p>Password:<input type="password" name="password"></p> <p><input type="submit" value="Add New"/> <a href="index.php">Cancel</a></p> </form> add.php

if ( isset($_POST['name']) && isset($_POST['email']) <?php if ( isset($_POST['name']) && isset($_POST['email'])       && isset($_POST['password'])) {     $sql = "INSERT INTO users (name, email, password)                VALUES (:name, :email, :password)";     $stmt = $pdo->prepare($sql);     $stmt->execute(array(         ':name' => $_POST['name'],        ':email' => $_POST['email'],        ':password' => $_POST['password']));    $_SESSION['success'] = 'Record Added';    header( 'Location: index.php' ) ;    return; if ( isset($_SESSION['success']) ) { echo '<p style="color:green">'.$_SESSION['success']."</p>\n"; unset($_SESSION['success']); }

delete.php Don't alter data in a GET. <?php require_once "pdo.php"; session_start(); if ( isset($_POST['delete']) && isset($_POST['user_id']) ) {     $sql = "DELETE FROM users WHERE user_id = :zip";     $stmt = $pdo->prepare($sql);     $stmt->execute(array(':zip' => $_POST['user_id']));     $_SESSION['success'] = 'Record deleted';     header( 'Location: index.php' ) ;     return; } $stmt = $pdo->prepare("SELECT name, user_id FROM users where user_id = :xyz"); $stmt->execute(array(":xyz" => $_GET['user_id'])); $row = $stmt->fetch(PDO::FETCH_ASSOC); if ( $row === false ) {     $_SESSION['error'] = 'Bad value for user_id'; ?> <p>Confirm: Deleting <?= htmlentities($row['name']) ?></p> <form method="post"><input type="hidden" name="user_id" value="<?= $row['user_id'] ?>"> <input type="submit" value="Delete" name="delete"> <a href="index.php">Cancel</a> </form> delete.php Don't alter data in a GET.

if ( isset($_SESSION['success']) ) { <?php if ( isset($_POST['delete']) && isset($_POST['user_id']) ) {     $sql = "DELETE FROM users WHERE user_id = :zip";     $stmt = $pdo->prepare($sql);     $stmt->execute(array(':zip' => $_POST['user_id']));     $_SESSION['success'] = 'Record deleted';     header( 'Location: index.php' ) ;     return; } ?> if ( isset($_SESSION['success']) ) { echo '<p style="color:green">'.$_SESSION['success']."</p>\n"; unset($_SESSION['success']);}

edit.php <?php require_once "pdo.php"; session_start(); if ( isset($_POST['name']) && isset($_POST['email'])       && isset($_POST['password']) && isset($_POST['user_id']) ) {     $sql = "UPDATE users SET name = :name,              email = :email, password = :password             WHERE user_id = :user_id";     $stmt = $pdo->prepare($sql);     $stmt->execute(array(         ':name' => $_POST['name'],         ':email' => $_POST['email'],         ':password' => $_POST['password'],         ':user_id' => $_POST['user_id']));     $_SESSION['success'] = 'Record updated';     header( 'Location: index.php' ) ;     return; } $stmt = $pdo->prepare("SELECT * FROM users where user_id = :xyz"); $stmt->execute(array(":xyz" => $_GET['user_id'])); $row = $stmt->fetch(PDO::FETCH_ASSOC); if ( $row === false ) {     $_SESSION['error'] = 'Bad value for user_id'; edit.php

edit.php $n = htmlentities($row['name']); $e = htmlentities($row['email']); $p = htmlentities($row['password']); $user_id = $row['user_id']; ?> <p>Edit User</p> <form method="post"> <p>Name: <input type="text" name="name" value="<?= $n ?>"></p> <p>Email: <input type="text" name="email" value="<?= $e ?>"></p> <p>Password: <input type="text" name="password" value="<?= $p ?>"></p> <input type="hidden" name="user_id" value="<?= $user_id ?>"> <p><input type="submit" value="Update"/> <a href="index.php">Cancel</a></p> </form> edit.php

edit.php if ( isset($_POST['name']) && isset($_POST['email'])      && isset($_POST['password']) && isset($_POST['user_id']) ) {     $sql = "UPDATE users SET name = :name,              email = :email, password = :password             WHERE user_id = :user_id";     $stmt = $pdo->prepare($sql);     $stmt->execute(array(         ':name' => $_POST['name'],         ':email' => $_POST['email'],         ':password' => $_POST['password'],         ':user_id' => $_POST['user_id']));     $_SESSION['success'] = 'Record updated';     header( 'Location: index.php' ) ;     return; } edit.php

Summary Making database connections Doing database operations SQL security (a.k.a. we love PDO prepared statements) Exploring errors... A multi-file CRUD application with redirect

Acknowledgements / Contributions Continue new Contributors and Translators here These slides are Copyright 2010- Charles R. Severance (www.dr-chuck.com) as part of www.wa4e.com and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Note from Chuck. Please retain and maintain this page as you remix and republish these materials. Please add any of your own improvements or contributions.