After clicking button submit, only select option of "type" is posted in the database, but select option of "Supplier" did not posted and only shows "0" values in phpMyAdmin.
Whenever I try to add a product, all the details are inputted to the database, but only "supplier" field is input with "0" Example of image is as below
Example of output: https://i.imgur.com/mKyZyLj.jpg
Below is my products.php
<?php
include_once 'products_crud.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Paw Empire : Products</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php include_once 'nav_bar.php'; ?>
<div >
<div >
<div >
<div >
<h2>Create New Product</h2>
</div>
<form action="products.php" method="post" >
<div >
<label for="productid" >ID</label>
<div >
<input name="pid" type="text" id="productid" placeholder="Product ID" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_num']; ?>" required>
</div>
</div>
<div >
<label for="productname" >Name</label>
<div >
<input name="name" type="text" id="productname" placeholder="Product Name" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_name']; ?>" required>
</div>
</div>
<div >
<label for="productprice" >Price (RM)</label>
<div >
<input name="price" type="number" id="productprice" placeholder="Product Price" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_price']; ?>" min="0.0" step="0.01" required>
</div>
</div>
<div >
<label for="productq" >Quantity</label>
<div >
<input name="quantity" type="number" id="productq" placeholder="Product Quantity" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_quantity']; ?>" min="0" required>
</div>
</div>
<div >
<label for="producttype" >Type</label>
<div >
<select name="type" id="producttype" required>
<option value="">Please select</option>
<option value="Cat" <?php if(isset($_GET['edit'])) if($editrow['fld_product_type']=="Cat") echo "selected"; ?>>Cat</option>
<option value="Cat Food" <?php if(isset($_GET['edit'])) if($editrow['fld_product_type']=="Cat Food") echo "selected"; ?>>Cat Food</option>
<option value="Cat Toys" <?php if(isset($_GET['edit'])) if($editrow['fld_product_type']=="Cat Toys") echo "selected"; ?>>Cat Toys</option>
</select>
</div>
</div>
<div >
<label for="supplier" >Supplier</label>
<div >
<select name="supplier" id="supplier" required>
<option value="">Please select</option>
<option value="My Pets Library" <?php if(isset($_GET['edit'])) if($editrow['fld_product_supplier']=="My Pets Library") echo "selected"; ?>>My Pets Library</option>
<option value="Pet Lovers Centre" <?php if(isset($_GET['edit'])) if($editrow['fld_product_supplier']=="Pet Lovers Centre") echo "selected"; ?>>Pet Lovers Centre</option>
<option value="Pet Smart" <?php if(isset($_GET['edit'])) if($editrow['fld_product_supplier']=="Pet Smart") echo "selected"; ?>>Pet Smart</option>
</select>
</div>
</div>
<div >
<label for="productshipping" >Shipping Price (RM)</label>
<div >
<input name="shipping" type="number" id="productshipping" placeholder="Shipping Price" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_price']; ?>" min="0.0" step="0.01" required>
</div>
</div>
<div >
<div >
<?php if (isset($_GET['edit'])) { ?>
<input type="hidden" name="oldpid" value="<?php echo $editrow['fld_product_num']; ?>">
<button type="submit" name="update"><span aria-hidden="true"></span> Update</button>
<?php } else { ?>
<button type="submit" name="create"><span aria-hidden="true"></span> Create</button>
<?php } ?>
<button type="reset"><span aria-hidden="true"></span> Clear</button>
</div>
</div>
</form>
</div>
</div>
<div >
<div >
<div >
<h2>Products List</h2>
</div>
<table >
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Price (RM)</th>
<th>Quantity</th>
<th>Type</th>
<th>Supplier</th>
<th>Shipping Price (RM)</th>
<th></th>
</tr>
<?php
// Read
$per_page = 5;
if (isset($_GET["page"]))
$page = $_GET["page"];
else
$page = 1;
$start_from = ($page-1) * $per_page;
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("select * from tbl_products_a180834_pt2 LIMIT $start_from, $per_page");
$stmt->execute();
$result = $stmt->fetchAll();
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
foreach($result as $readrow) {
?>
<tr>
<td><?php echo $readrow['fld_product_num']; ?></td>
<td><?php echo $readrow['fld_product_name']; ?></td>
<td><?php echo $readrow['fld_product_price']; ?></td>
<td><?php echo $readrow['fld_product_quantity']; ?></td>
<td><?php echo $readrow['fld_product_type']; ?></td>
<td><?php echo $readrow['fld_product_supplier']; ?></td>
<td><?php echo $readrow['fld_product_shipping']; ?></td>
<td>
<a href="products_details.php?pid=<?php echo $readrow['fld_product_num']; ?>" role="button">Details</a>
<a href="products.php?edit=<?php echo $readrow['fld_product_num']; ?>" role="button"> Edit </a>
<a href="products.php?delete=<?php echo $readrow['fld_product_num']; ?>" onclick="return confirm('Are you sure to delete?');" role="button">Delete</a>
</td>
</tr>
<?php } ?>
</table>
</div>
</div>
<div >
<div >
<nav>
<ul >
<?php
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM tbl_products_a180834_pt2");
$stmt->execute();
$result = $stmt->fetchAll();
$total_records = count($result);
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
$total_pages = ceil($total_records / $per_page);
?>
<?php if ($page==1) { ?>
<li ><span aria-hidden="true">«</span></li>
<?php } else { ?>
<li><a href="products.php?page=<?php echo $page-1 ?>" aria-label="Previous"><span aria-hidden="true">«</span></a></li>
<?php
}
for ($i=1; $i<=$total_pages; $i )
if ($i == $page)
echo "<li class=\"active\"><a href=\"products.php?page=$i\">$i</a></li>";
else
echo "<li><a href=\"products.php?page=$i\">$i</a></li>";
?>
<?php if ($page==$total_pages) { ?>
<li ><span aria-hidden="true">»</span></li>
<?php } else { ?>
<li><a href="products.php?page=<?php echo $page 1 ?>" aria-label="Previous"><span aria-hidden="true">»</span></a></li>
<?php } ?>
</ul>
</nav>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
And below is my products_crud.php (which sends the data to database)
<?php
include_once 'database.php';
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Create
if (isset($_POST['create'])) {
try {
$stmt = $conn->prepare("INSERT INTO tbl_products_a180834_pt2 (fld_product_num,
fld_product_name, fld_product_price, fld_product_quantity, fld_product_type,
fld_product_supplier, fld_product_shipping) VALUES(:pid, :name, :price, :quantity,
:type, :supplier, :shipping)");
$stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':price', $price, PDO::PARAM_INT);
$stmt->bindParam(':quantity', $quantity, PDO::PARAM_STR);
$stmt->bindParam(':type', $type, PDO::PARAM_STR);
$stmt->bindParam(':supplier', $supplier, PDO::PARAM_INT);
$stmt->bindParam(':shipping', $shipping, PDO::PARAM_INT);
$pid = $_POST['pid'];
$name = $_POST['name'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$type = $_POST['type'];
$supplier = $_POST['supplier'];
$shipping = $_POST['shipping'];
$stmt->execute();
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
}
//Update
if (isset($_POST['update'])) {
try {
$stmt = $conn->prepare("UPDATE tbl_products_a180834_pt2 SET fld_product_num = :pid,
fld_product_name = :name, fld_product_price = :price, fld_product_quantity = :quantity,
fld_product_type = :type, fld_product_supplier = :supplier, fld_product_shipping = :shipping
WHERE fld_product_num = :oldpid");
$stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':price', $price, PDO::PARAM_INT);
$stmt->bindParam(':quantity', $quantity, PDO::PARAM_STR);
$stmt->bindParam(':type', $type, PDO::PARAM_STR);
$stmt->bindParam(':supplier', $supplier, PDO::PARAM_INT);
$stmt->bindParam(':shipping', $shipping, PDO::PARAM_INT);
$stmt->bindParam(':oldpid', $oldpid, PDO::PARAM_STR);
$pid = $_POST['pid'];
$name = $_POST['name'];
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$type = $_POST['type'];
$supplier = $_POST['supplier'];
$shipping = $_POST['shipping'];
$oldpid = $_POST['oldpid'];
$stmt->execute();
header("Location: products.php");
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
}
//Delete
if (isset($_GET['delete'])) {
try {
$stmt = $conn->prepare("DELETE FROM tbl_products_a180834_pt2 WHERE fld_product_num = :pid");
$stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
$pid = $_GET['delete'];
$stmt->execute();
header("Location: products.php");
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
}
//Edit
if (isset($_GET['edit'])) {
try {
$stmt = $conn->prepare("SELECT * FROM tbl_products_a180834_pt2 WHERE fld_product_num = :pid");
$stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
$pid = $_GET['edit'];
$stmt->execute();
$editrow = $stmt->fetch(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
}
$conn = null;
?>
CodePudding user response:
This
$stmt->bindParam(':supplier', $supplier, PDO::PARAM_INT);
an integer parameter binding, is not compatible with
<option value="My Pets Library"
which is of course a string
Additional note:
You dont have to do
$stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
// and then
$pid = $_POST['pid'];
// you can just do
$stmt->bindParam(':pid', $_POST['pid'], PDO::PARAM_STR);
Saves a bit of memory and a few CPU cycles