Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('HOST', 'localhost');
- define('USER', 'root');
- define('PASS', '');
- define('NAME', 'radi');
- define('TABLE', 'books');
- $result = null;
- try {
- $conn = new PDO('mysql:host='. HOST .';dbname='. NAME, USER, PASS);
- // errmode enabled
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch(PDOException $e) {
- echo "Connection failed: " . $e->getMessage();
- }
- if(isset($_POST['update'])) {
- $title = htmlentities($_POST['title'], ENT_QUOTES, 'UTF-8');
- $author = htmlentities($_POST['author'], ENT_QUOTES, 'UTF-8');
- $sql = 'UPDATE ' . TABLE . ' SET ' . (!empty($title) ? 'title = \'' . $title . '\'': '') . ' ' . (!empty($author) ? ', author = \'' . $author . '\'' : '') . ' WHERE id = ' . htmlentities($_GET['book'], ENT_QUOTES, 'UTF-8');
- $stmt = $conn->prepare($sql);
- $stmt->execute();
- echo 'Successfully updated ' . $stmt->rowCount() .' rows!';
- } else if(isset($_GET['book'])) {
- $sql = 'SELECT * FROM books WHERE id = ' . htmlentities($_GET['book'], ENT_QUOTES, 'UTF-8');
- $stmt = $conn->prepare($sql);
- $stmt->execute();
- $result = $stmt->fetchAll(PDO::FETCH_OBJ);
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Document</title>
- </head>
- <body>
- <form method="post">
- <label for="title">Title:</label><br>
- <input type="text" name="title" id="title" value="<?= isset($result) ? $result[0]->title : "" ?>">
- <br><br>
- <label for="author">Author:</label><br>
- <input type="text" name="author" id="author" value="<?= isset($result) ? $result[0]->author : "" ?>">
- <br><br>
- <button type="submit" name="update">Update Data</button>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement