Here’s my code, the only thing possible that wouldn’t work is the isset
for posting additem
which name ive changed:
<?php
// Check if form is submitted
if (isset($_POST['additem']))
{
// Database connection parameters
$servername = "localhost";
$username = "root"; // Default username for XAMPP MySQL
$password = ""; // Default password for XAMPP MySQL
$dbname = "POS"; // Name of your database
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve form data
$itemId = $_POST['itemId'];
$itemName = $_POST['itemName'];
$itemPrice = $_POST['itemPrice'];
$itemTab = $_POST['itemTab'];
// Prepare and bind SQL statement
$query = mysqli_query($conn, "INSERT INTO items (ItemID, ItemName, ItemPrice, ItemTab) VALUES ('$itemId', '$itemName', '$itemPrice', '$itemTab')");
// Execute the statement
if ($query) {
echo "<script>alert('Item added successfully')</script>"; // Corrected syntax
} else {
echo "<script>alert('Error')</script>"; // Corrected syntax
}
// Close connection
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Item List</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Item List</h1>
<form method="post" action="" id="itemForm">
<label for="ID">ID:</label>
<input type="number" id="ID" name="itemId" required>
<label for="itemName">Item Name:</label>
<input type="text" id="itemName" name="itemName" required>
<label for="itemPrice">Price:</label>
<input type="number" id="itemPrice" name="itemPrice" required>
<label for="itemTab">Tab:</label>
<input type="text" id="itemTab" name="itemTab" required>
<button type="submit" name="additem" value="submit">submit</button>
</form>
<table id="itemTable">
<thead>
<tr>
<th>ID</th>
<th>Item Name</th>
<th>Price</th>
<th>Tab</th>
</tr>
</thead>
<tbody>
<!-- Items will be dynamically added here -->
</tbody>
</table>
<script src="script.js"></script>
</body>
</html>
I’ve looked at the whole code over and the only issue it wont pass the isset post. Help!
New contributor
Zell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.