Add Item and Download of Images works
This commit is contained in:
56
add_item.php
Normal file
56
add_item.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
ini_set('display_errors', 1);
|
||||||
|
ini_set('display_startup_errors', 1);
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
include_once('config/config.php');
|
||||||
|
|
||||||
|
$ItemTitle = $_POST['ItemTitle'];
|
||||||
|
$ItemDescription = $_POST['ItemDescription'];
|
||||||
|
$ItemPrice = $_POST['ItemPrice'];
|
||||||
|
$ItemLink = $_POST['ItemLink'];
|
||||||
|
$ItemImage = $_POST['ItemImage'];
|
||||||
|
|
||||||
|
|
||||||
|
#--- check if the provided Image-Link is a real image:
|
||||||
|
|
||||||
|
$headers = get_headers($ItemImage, 1);
|
||||||
|
|
||||||
|
if (strpos($headers['Content-Type'], 'image/') !== false) {
|
||||||
|
$imageLocalLink = 'data/images/' . uniqid() . '.' . pathinfo($ItemImage, PATHINFO_EXTENSION);
|
||||||
|
echo "ImageLink: " . $imageLocalLink;
|
||||||
|
file_put_contents($imageLocalLink, fopen($ItemImage, 'r'));
|
||||||
|
} else {
|
||||||
|
echo "Link is Not an Image";
|
||||||
|
}
|
||||||
|
|
||||||
|
#---
|
||||||
|
|
||||||
|
$ItemPriceCents = $ItemPrice * 100;
|
||||||
|
|
||||||
|
$conn = new mysqli($servername, $username, $password, $db);
|
||||||
|
|
||||||
|
// Check connection
|
||||||
|
if ($conn->connect_error) {
|
||||||
|
die('Connection failed: ' . $conn->connect_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt = $conn->prepare('INSERT INTO whishes (title, description, link, image, price) VALUES (?, ?, ?, ?, ?)');
|
||||||
|
|
||||||
|
if (false === $stmt) {
|
||||||
|
die('prepare() failed: ' . htmlspecialchars($mysqli->error));
|
||||||
|
}
|
||||||
|
|
||||||
|
$rc = $stmt->bind_param('ssssi', $ItemTitle, $ItemDescription, $imageLocalLink, $ItemImage, $ItemPriceCents);
|
||||||
|
if (false === $rc) {
|
||||||
|
die('bind_param() failed: ' . htmlspecialchars($stmt->error));
|
||||||
|
}
|
||||||
|
|
||||||
|
$rc = $stmt->execute();
|
||||||
|
if (false === $rc) {
|
||||||
|
die('execute() failed: ' . htmlspecialchars($stmt->error));
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt->close();
|
||||||
|
$conn->close();
|
||||||
|
|
||||||
|
header('Location: ' . $_SERVER['HTTP_REFERER']);
|
@@ -1,10 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function generateListItem($ListItemID, $ItemImage, $ItemTitle, $ItemPrice, $ItemComment)
|
function generateListItem($ListItemID, $ItemImage, $ItemTitle, $ItemLink, $ItemPrice, $ItemComment, $ItemReserved)
|
||||||
{
|
{
|
||||||
$formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
|
$formatter = new NumberFormatter('de_DE', NumberFormatter::CURRENCY);
|
||||||
|
|
||||||
echo ('
|
if (strlen($ItemComment) == 0) {
|
||||||
|
$ItemComment = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
echo ('
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="card shadow-sm">
|
<div class="card shadow-sm">
|
||||||
<img src="' . $ItemImage . '" class="card-img-top">
|
<img src="' . $ItemImage . '" class="card-img-top">
|
||||||
@@ -13,8 +17,8 @@ function generateListItem($ListItemID, $ItemImage, $ItemTitle, $ItemPrice, $Item
|
|||||||
<p class="card-text">' . $ItemComment . '</p>
|
<p class="card-text">' . $ItemComment . '</p>
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
<div class="d-flex justify-content-between align-items-center">
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<button type="button" class="btn btn-sm btn-outline-secondary">zum Anbieter</button>
|
<a href="' . $ItemLink . '" class="btn btn-sm btn-outline-secondary" role="button" target="_blank">zum Anbieter</a>
|
||||||
<button type="button" class="btn btn-sm btn-outline-secondary">Reservieren</button>
|
<button type="button" class="btn btn-sm ' . ($ItemReserved == true ? 'btn-outline-info' : 'btn-outline-secondary') . ' "data-bs-toggle="modal" data-bs-target="#reservationModal">Reservieren</button>
|
||||||
</div>
|
</div>
|
||||||
<small class="text-muted">' . $formatter->formatCurrency($ItemPrice / 100, 'EUR') . '</small>
|
<small class="text-muted">' . $formatter->formatCurrency($ItemPrice / 100, 'EUR') . '</small>
|
||||||
</div>
|
</div>
|
||||||
|
57
index.php
57
index.php
@@ -60,14 +60,14 @@ include_once('config/config.php');
|
|||||||
|
|
||||||
// Check connection
|
// Check connection
|
||||||
if ($conn->connect_error) {
|
if ($conn->connect_error) {
|
||||||
die("Connection failed: " . $conn->connect_error);
|
die('Connection failed: ' . $conn->connect_error);
|
||||||
}
|
}
|
||||||
$sql = "SELECT ID, title, description, image, reserved, price FROM whishes";
|
$sql = 'SELECT ID, title, description, link, image, reserved, price FROM whishes';
|
||||||
$result = $conn->query($sql);
|
$result = $conn->query($sql);
|
||||||
|
|
||||||
if ($result !== false && $result->num_rows > 0) {
|
if ($result !== false && $result->num_rows > 0) {
|
||||||
while ($row = $result->fetch_assoc()) {
|
while ($row = $result->fetch_assoc()) {
|
||||||
generateListItem($row['ID'], $row['image'], $row['title'], $row['price'], $row['description']);
|
generateListItem($row['ID'], $row['image'], $row['title'], $row['link'], $row['price'], $row['description'], $row['reserved']);
|
||||||
}
|
}
|
||||||
$conn->close();
|
$conn->close();
|
||||||
}
|
}
|
||||||
@@ -108,7 +108,7 @@ include_once('config/config.php');
|
|||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
<!-- Modal -->
|
<!-- Modal addItem-->
|
||||||
<div class="modal fade" id="addItemModal" tabindex="-1" aria-labelledby="addItemModalLabel" aria-hidden="true">
|
<div class="modal fade" id="addItemModal" tabindex="-1" aria-labelledby="addItemModalLabel" aria-hidden="true">
|
||||||
<div class="modal-dialog">
|
<div class="modal-dialog">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
@@ -116,12 +116,59 @@ include_once('config/config.php');
|
|||||||
<h5 class="modal-title" id="addItemModalLabel">Add new Item</h5>
|
<h5 class="modal-title" id="addItemModalLabel">Add new Item</h5>
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
</div>
|
</div>
|
||||||
|
<form action="add_item.php" method="POST">
|
||||||
|
<div class="modal-body">
|
||||||
|
|
||||||
|
<label for="ItemTitle" class="form-label">Titel</label>
|
||||||
|
<div class="input-group mb-3">
|
||||||
|
<input type="text" class="form-control" id="ItemTitle" name="ItemTitle" rows="3" required>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label for="ItemDescription" class="form-label">Beschreibung</label>
|
||||||
|
<div class="input-group mb-3">
|
||||||
|
<textarea class="form-control" id="ItemDescription" name="ItemDescription" rows="3"></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label for="ItemPrice" class="form-label">Preis</label>
|
||||||
|
<div class="input-group mb-3">
|
||||||
|
<input type="text" class="form-control" id="ItemPrice" name="ItemPrice" pattern="^\d*(\.\d{2}$)?" value="" data-type="currency" placeholder="0.00€" />
|
||||||
|
<span class="input-group-text">€</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label for="ItemLink" class="form-label">Link zum Angebot</label>
|
||||||
|
<div class="input-group mb-3">
|
||||||
|
<input type="url" class="form-control" id="ItemLink" name="ItemLink" pattern="https?://.+" title="Include http://" rows="3">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label for="ItemImage" class="form-label">Link zum Bild</label>
|
||||||
|
<div class="input-group mb-3">
|
||||||
|
<input type="url" class="form-control" id="ItemImage" name="ItemImage" pattern="https?://.+" title="Include http://" rows="3">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||||
|
<button type="submit" class="btn btn-primary">Add new Item</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Modal Reservation-->
|
||||||
|
<div class="modal fade" id="reservationModal" tabindex="-1" aria-labelledby="reservationModalLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="reservationModalLabel">Wunsch reservieren</h5>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
...
|
...
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||||
<button type="button" class="btn btn-primary">Add new Item</button>
|
<button type="button" class="btn btn-primary">Reservieren</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user