php - PDO Mysql data undisplayed when insert data is present -
problem when displayed data database see data in page. when i'm put insert data unviewable.
this code:
<?php include 'db.php'; $stmt = $db->prepare('select * ' . $table); $stmt->execute(); if(isset($_post['btn-insert'])){ $prodname = $_post['prod_name']; $prodsupp = $_post['prod_supplier']; $prodprice = $_post['prod_price']; } ?> <html> <head><title></title></head> <body> <table cellpadding="4" cellspacing="2" border='1'> <tr> <th>product id</th> <th>product name</th> <th>product supplier</th> <th>product price</th> </tr> <?php while($product = $stmt->fetch(pdo::fetch_obj)) { ?> <tr> <td><?php echo $product->prod_id; ?></td> <td><?php echo $product->prod_name; ?></td> <td><?php echo $product->prod_supplier; ?></td> <td><?php echo $product->prod_price; ?></td> </tr> <?php } ?> </table> <br> <br> <form action="" method="post"> <table cellspacing="2" cellpadding="2" border="1"> <tr> <td>product name:</td> <td><input type="text" name="prod_name"></td> </tr> <tr> <td>product supplier:</td> <td><input type="text" name="prod_supplier"></td> </tr> <tr> <td>product price:</td> <td><input type="text" name="prod_price"></td> </tr> <tr> <td colspan="2"><input type="submit" name="btn-insert" value="insert"></td> </tr> </table> </form> </body> </html>
and code when try insert change php script on top:
<?php include 'db.php'; $stmt = $db->prepare('select * ' . $table); $stmt->execute(); if(isset($_post['btn-insert'])){ $prodname = $_post['prod_name']; $prodsupp = $_post['prod_supplier']; $prodprice = $_post['prod_price']; $db = new pdo('mysql:host' . $host . ';dbname=' . $dbname, $dbusername, $dbpassword); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); $stmt = $db->prepare("insert tbl_product(prod_name, prod_supplier, prod_price) values('$prodname', '$prodsupp', '$prodprice')"); } ?>
thats problem have encountered right @ moment.
you created conflict in "stmt" variable... firstly
$stmt = $db->prepare('select * ' . $table); $stmt->execute();
then:
$stmt = $db->prepare("insert into...
so since prepared insert statement,
this not fetching anything.. check conflict , fine..
$product = $stmt->fetch(pdo::fetch_obj)
Comments
Post a Comment