PHP , PDO , Foreign Key -
i new php , doing project. question have:
in above interface, have extracted category name , id 'category name' combo box category table. , after form, should enter 'category id' 'item table', because category id foreign key of item table
this code have add
require_once '../../config/config.php'; $query = 'select `category_id`, `category_name` `tbl_category`'; $tbl_category_category_id = $query; $item_name = $_post['item_name']; $price = $_post['price']; $tbl_distributor_distributor_id= $_post['tbl_distributor_distributor_id']; $item_lowlevel = $_post['item_lowlevel']; $status = $_post['status']; try { $sql = "insert `tbl_item`(`tbl_category_category_id`, `item_name`, `price`, `tbl_distributor_distributor_id`, `item_lowlevel`,`status`) values (:tbl_category_category_id, :item_name, :price, :tbl_distributor_distributor_id, :item_lowlevel ,:status)"; $qry = $conn->prepare($sql); $qry->execute(array(':tbl_category_category_id' => $tbl_category_category_id, ':item_name' => $item_name, ':price' => $price, ':tbl_distributor_distributor_id' => $tbl_distributor_distributor_id, ':item_lowlevel' => $item_lowlevel, ':status' => $status)); $conn = null; } catch (pdoexception $e) { echo $e->getmessage(); }
but master table of 'item table'. has added query. how correct this. how should take category id combo box , enter item table.
first need execute select statement
change this...
$query = 'select `category_id`, `category_name` `tbl_category`';
to this...
$sql = "select category_id, category_name tbl_category order category_name asc"; $query = $conn->prepare($sql); $query->execute(); $row = $query->fetch(pdo::fetch_assoc);
next create select box using data select statement...
<select name="category_id" id="category_id"> <option value="">select category</option> <?php { ?> <option value="<?php echo $row['category_id'] ?>"><?php echo $row['category_name'] ?></option> <?php } while ($row = $query->fetch(pdo::fetch_assoc)); ?> </select>
finally category id
change this...
$tbl_category_category_id = $query;
to this...
$tbl_category_category_id = $_post['category_id'];
happy coding !
Comments
Post a Comment