note 129970 added to class.pdo
| From: | mortyjorty@gmx.de | Date: | Fri, 10 Jan 2025 13:39:12 +0000 |
| Subject: | note 129970 added to class.pdo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221784@lists.php.net to get a copy of this message | ||
<?php
$servername = "mysql";
$username = "root";
$password = "htlkrems";
try {
$conn = new PDO("mysql:host=$servername;dbname=trackstar", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
if(isset($_GET['delete_id'])){
$delete_id = $_GET['delete_id'];
if(filter_var($delete_id, FILTER_VALIDATE_INT) !== false){
$stmnt = $conn->prepare("DELETE FROM projects WHERE project_id =
:project_id");
$stmnt->bindParam(':project_id', $delete_id);
if($stmnt->execute())
{
$delete_message = "Project with ID $delete_id deleted successfully";
} else {
$delete_message = "Project with ID $delete_id could not be deleted";
}
} else {
$delete_message = "Project ID $delete_id is invalid";
}
}
if(isset($_GET['add_project']))
{
$dummyTitle = "Demo APP XYZ";
$dummyDescription = "SOME TEXT ...";
$dummyUserID = rand(1,3);
$stmnt = $conn->prepare("INSERT INTO projects (title, description, created_at,
user_id) VALUES(:title, :description, NOW(), :user_id)");
$stmnt->bindParam(':title', $dummyTitle);
$stmnt->bindParam(':description', $dummyDescription);
$stmnt->bindParam(':user_id', $dummyUserID);
if($stmnt->execute()){
$add_message = "Project added successfully";
} else {
$add_message = "Project could not be added";
}
}
$sth = $conn->query("SELECT * FROM projects");
$projects = $sth->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Title</th>
<th scope="col">Description</th>
<th scope="col">Created at</th>
<th scope="col">User ID</th>
<th scope="col">Buttons</th>
</tr>
</thead>
<tbody>
<?php
if ($projects)
{
foreach ($projects as $project)
{
echo "<tr>";
foreach ($project as $item) {
echo "<td>" . $item . "</td>";
}
echo '
<th>
<a class="btn btn-primary" href="?delete_id=' .
$project['project_id'] .'" role="button">Remove Project</a>
</th>';
echo "</tr>";
}
}
?>
</tbody>
</table>
<a class="btn btn-primary" href="?add_project=true"
role="button">Add Project</a>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>
</html>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 193.170.70.27)
----
Manual Page -- https://php.net/manual/en/class.pdo.php
Edit -- https://main.php.net/note/edit/129970
Del: integrated -- https://main.php.net/note/delete/129970/integrated
Del: useless -- https://main.php.net/note/delete/129970/useless
Del: bad code -- https://main.php.net/note/delete/129970/bad+code
Del: spam -- https://main.php.net/note/delete/129970/spam
Del: non-english -- https://main.php.net/note/delete/129970/non-english
Del: in docs -- https://main.php.net/note/delete/129970/in+docs
Del: other reasons-- https://main.php.net/note/delete/129970
Reject -- https://main.php.net/note/reject/129970
Search -- https://main.php.net/manage/user-notes.php