note 127637 added to book.pdo

From: Date: Thu, 15 Sep 2022 21:41:26 +0000
Subject: note 127637 added to book.pdo
Groups: php.notes 
Request: Send a blank email to php-notes+get-219581@lists.php.net to get a copy of this message
// Some simple script snippets for processing database queries // ---------- Page with databese queries - queries.php---------- // ... other HTML Code <?php $userId = ''; $userData = DatabaseClass::getUsersById($userId); if (empty($userData)) { echo "<tr><td colspan='3'> <p class='alert alert-danger align-content-center'>No data found</p></td></tr>"; } ?> // ... other HTML Code <?php echo $userData["name"]; echo $userData["email"]; echo $userData["password"]; ?> // ... other HTML Code <table> <thead> <tr> <th>Name</th> <th>Email</th> <th>Password</th> </tr> </thead> <tbody> <?php $userId = ''; $userName = ''; $userEmail = ''; $userPassword = ''; $dataAllUsers = DatabaseClass::getAllUsers(); if (empty($dataAllUsers)) { echo "No data found"; } foreach ($dataAllUsers as $d) { $userId = preg_replace('()', '', $d["id"]); $userName = $d["name"]; $userEmail = $d["email"]; $password = $d["password"]; echo "<tr>"; echo "<td>" . $userId . "</td>"; echo "<td>" . $userName . "</td>"; echo "<td>" . $userEmail . "</td>"; echo "<td>" . $userPassword . "</td>"; echo "<td>" . "<a href='userdetails.php?id=$userId'>user details</a>" . "</td>"; echo "</tr>"; } ?> </tbody> </table> // ... other HTML Code <?php $name = ''; $email = ''; $password = ''; if (isset($_POST['submit'])) { $result = DatabaseClass::writeNewUser($name, $email, $password); } if ($result == true) { echo "One row written"; } else { echo "No Data written"; } ?> // ... other HTML Code <form action="queries.php" method="post"> <div class="mb-3"> <label for="name" class="form-label">Name</label> <input type="text" name="name" class="form-control <?= isset($errors['name']) ? 'is-invalid' : '' ?>" id="name" placeholder="Your Name" min="1" max="32" value="<?= htmlspecialchars($name) ?>" required > </div> <div class="mb-3 mt-3"> <label for="email" class="form-label">Email</label> <input type="email" name="email" class="form-control <?= isset($errors['email']) ? 'is-invalid' : '' ?>" id="email" placeholder="Your Email" value="<?= htmlspecialchars($email) ?>" required > </div> <div class="mb-3"> <label for="password" class="form-label">Passwortd</label> <input type="password" name="password" class="form-control <?= isset($errors['password']) ? 'is-invalid' : '' ?>" id="password" placeholder="Your Password" min="8" max="32" value="<?= htmlspecialchars($password) ?>" required > </div> <button type="submit" name="submit" class="btn btn-primary mb-3">Submit</button> <a href="queries.php" class="btn btn-secondary mb-3">Reset</a> // ... other HTML Code // ---------- DatabaseClass.php ---------- <?php class DatabaseClass { private $emailInput = ''; public static function connect() { $dbName = 'MyDatabase'; $dbHost = 'localhost'; $dbUsername = 'root'; $dbUserPassword = ''; try { $conn = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUsername, $dbUserPassword); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } return $conn; } public static function getUsersById($userId) { $db = DatabaseClass::connect(); $sql = "SELECT * FROM Users WHERE Id = ?"; $stmt = $db->prepare($sql); $stmt->execute([$userId]); $data = $stmt->fetch(PDO::FETCH_ASSOC); $db = null; return $data; } public static function ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.173 (proxied: 185.17.14.59) ---- Manual Page -- https://php.net/manual/en/book.pdo.php Edit -- https://main.php.net/note/edit/127637 Del: integrated -- https://main.php.net/note/delete/127637/integrated Del: useless -- https://main.php.net/note/delete/127637/useless Del: bad code -- https://main.php.net/note/delete/127637/bad+code Del: spam -- https://main.php.net/note/delete/127637/spam Del: non-english -- https://main.php.net/note/delete/127637/non-english Del: in docs -- https://main.php.net/note/delete/127637/in+docs Del: other reasons-- https://main.php.net/note/delete/127637 Reject -- https://main.php.net/note/reject/127637 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#219581) next »