Im trying to create a single page containing a shopping cart . Items will be added when barcode is scanned i only need 5 products but always failed you can see what i tried... i know how to connect db but very little knowledge in php the items shall be added in an html table form but nothing is working
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Barcode VARCHAR(50)
);
INSERT INTO Products (ProductID, ProductName, Barcode)
VALUES (1, 'Product A', '1234567890123'),
(2, 'Product B', '2345678901234'),
(3, 'Product C', '3456789012345'),
(4, 'Product D', '4567890123456'),
(5, 'Product E', '5678901234567');
php
<form method="post" action="scan.php">
<label>Scan Barcode:</label>
<input type="text" name="barcode">
<input type="submit" value="Submit">
</form>
scan.php
<?php
// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=your_database_name", "username", "password");
// Retrieve the barcode value from the form
$barcode = $_POST['barcode'];
// Prepare the SQL query to retrieve the product based on the barcode
$sql = "SELECT * FROM products WHERE barcode = :barcode";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':barcode', $barcode);
$stmt->execute();
// Check if there are any results
if ($stmt->rowCount() > 0) {
// Start the HTML table
echo "<table>";
echo "<tr><th>Product Name</th><th>Product Description</th><th>Product Price</th></tr>";
// Loop through the results and add each product as a row in the table
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['description'] . "</td>";
echo "<td>$" . $row['price'] . "</td>";
echo "</tr>";
}
// End the HTML table
echo "</table>";
} else {
// Display an error message if no results were found
echo "No products found for barcode: " . $barcode;
}
?>
source https://stackoverflow.com/questions/76176199/add-items-to-cart-using-barcode-scanner-php
No comments:
Post a Comment