Select Value Did Not Enter Database after clicking post button

Issue

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 class="container-fluid">
        <div class="row">
          <div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
            <div class="page-header">
              <h2>Create New Product</h2>
            </div>
            <form action="products.php" method="post" class="form-horizontal">
              <div class="form-group">
                <label for="productid" class="col-sm-3 control-label">ID</label>
                <div class="col-sm-9">
                  <input name="pid" type="text" class="form-control" id="productid" placeholder="Product ID" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_num']; ?>" required>
                </div>
              </div>
              <div class="form-group">
                <label for="productname" class="col-sm-3 control-label">Name</label>
                <div class="col-sm-9">
                  <input name="name" type="text" class="form-control" id="productname" placeholder="Product Name" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_name']; ?>" required>
                </div>
              </div>
              <div class="form-group">
                <label for="productprice" class="col-sm-3 control-label">Price (RM)</label>
                <div class="col-sm-9">
                  <input name="price" type="number" class="form-control" 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 class="form-group">
                <label for="productq" class="col-sm-3 control-label">Quantity</label>
                <div class="col-sm-9">
                  <input name="quantity" type="number" class="form-control" id="productq" placeholder="Product Quantity" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_quantity']; ?>"  min="0" required>
                </div>
              </div>


              <div class="form-group">
                <label for="producttype" class="col-sm-3 control-label">Type</label>
                <div class="col-sm-9">
                  <select name="type" class="form-control" 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 class="form-group">
                <label for="supplier" class="col-sm-3 control-label">Supplier</label>
                <div class="col-sm-9">
                  <select name="supplier" class="form-control" 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 class="form-group">
                <label for="productshipping" class="col-sm-3 control-label">Shipping Price (RM)</label>
                <div class="col-sm-9">
                  <input name="shipping" type="number" class="form-control" 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 class="form-group">
                <div class="col-sm-offset-3 col-sm-9">
                  <?php if (isset($_GET['edit'])) { ?>
                    <input type="hidden" name="oldpid" value="<?php echo $editrow['fld_product_num']; ?>">
                    <button class="btn btn-default" type="submit" name="update"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Update</button>
                  <?php } else { ?>
                    <button class="btn btn-default" type="submit" name="create"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Create</button>
                  <?php } ?>
                  <button class="btn btn-default" type="reset"><span class="glyphicon glyphicon-erase" aria-hidden="true"></span> Clear</button>
                </div>
              </div>
            </form>
          </div>
        </div>

        <div class="row">
          <div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
            <div class="page-header">
              <h2>Products List</h2>
            </div>
            <table class="table table-striped table-bordered">
              <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']; ?>" class="btn btn-warning btn-xs" role="button">Details</a>
                    <a href="products.php?edit=<?php echo $readrow['fld_product_num']; ?>" class="btn btn-success btn-xs" role="button"> Edit </a>
                    <a href="products.php?delete=<?php echo $readrow['fld_product_num']; ?>" onclick="return confirm('Are you sure to delete?');" class="btn btn-danger btn-xs" role="button">Delete</a>
                  </td>
                </tr>
              <?php } ?>

            </table>
          </div>
        </div>
        <div class="row">
          <div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
            <nav>
              <ul class="pagination">
                <?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 class="disabled"><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 class="disabled"><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;
?>

Solution

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

Answered By – RiggsFolly

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published