Ubaid ur Rehman
Roll no: SP21-134
Connection:
<?php
$servername="localhost:3307"; #localhost:3307
$username="root";
$password="";
$dbname="inventory";
$conn=mysqli_connect($servername,$username,$password,$dbname);
if ($conn) {
echo "connection successful!";
}
else{
echo "connection failed!".mysqli_connect_error();
}
?>
Insert code:
<?php include("connection.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Entry</title>
</head>
<body>
<form action="",method="post">
<label for="">Product Name</label>
<input type="text" name="product" id="">
<br><br>
<label for="">Description</label>
<input type="text" name="description" id="">
<br><br>
<label for="">Price</label>
<input type="text" name="price" id="">
<br><br>
<label for="">Quantity</label>
<input type="text" name="quantity" id="">
<br><br>
<button name="insertd",value="insertd">Insert</button>
</form>
</body>
</html>
<?php
if(isset($_POST['insertd']))
{ $product=$_POST['product'];
$price=$_POST['price'];
$description=$_POST['description'];
$quantity=$_POST['quantity'];
$insert="INSERT INTO `inventory_m`(`product_name`,
`description`, `price`, `quantity`) VALUES
( '$product','$description','$price','$quantity')";
$result = mysqli_query($conn, $insert);
if ($result) {
echo "Product inserted successfully";
} else {
echo "not add";
}
?>
Show & delete idtem:
<form method="post">
<table >
<tr>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Action</th>
</tr>
<?php
$query = "SELECT * FROM `inventory_m`";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['product_name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td>
<form method="post">
<input type="hidden" name="id" value="<?php echo $row['id'];
?>">
<input type="submit" name="delete" value="Delete">
</form>
</td>
</tr>
<?php
}
?>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$query = "DELETE FROM `inventory_m` WHERE `id` = $id";
mysqli_query($conn, $query);
}
?>