html - PHP; PDO; MySQL INSERT query with drop down -
i struggling 2 input insert query. not insert db. first, supposed accept user input , dropdown menu selection id table hence select query. once code fixed set relationship between 2 tables involved via 'pubid'.
<?php error_reporting(e_all); ini_set('display_errors', 1); require_once("dbconn.php"); try { $conn = new pdo("mysql:host=$dbhost;dbname=$dbname;charset=utf8",$dbuser,$dbpass,$dbo); } catch (pdoexception $e) { echo $e->getmessage(); exit; } if(isset($_post['addbtn'])){ $q = $conn->prepare("insert series (title, pubid) values (:title, :pubid)"); $q->bindparam(':title',$title,pdo::param_str); $q->bindparam(':pubid',$pubid,pdo::param_int); $q->execute(); } $sql = 'select pubid, name publisher order name'; $q = $conn->query($sql); $q->setfetchmode(pdo::fetch_assoc); ?> <!doctype html> <html lang="eng"> <head> <meta charset="utf-8"> <title> comics db > add series </title> <link rel="stylesheet" type="text/css" href="comicsdb.css"> </head> <body> <div class="main"> <menu> <ul> <li><a href="index.php">home</a></li> <li><a href="add.php">add</a></li> <li><a href="edit.php">edit</a></li> <li><a href="delete.php">delete</a></li> <li><a href="list.php">list</a></li> <li><a href="search.php">search</a></li> </ul> </menu> <div class="pub_menu"> <form action="addseries.php" method="post"> <p> series: <input type="text" name="title" id="title" size="40" /><br /> <select> <option>select</option> <?php while ($row = $q->fetch()){ ?> <option name="pubid" id="pubid" value="<?php echo $row['pubid']; ?>"><?php echo $row['name']; ?></option> <?php } ?> </select> <input type="submit" value="add series" name="addbtn" /> </p> </form> </div> </body> </html> dbconn.php
<?php $dbhost = '127.0.0.1'; $dbname = 'comicsdb'; $dbuser = 'root'; $dbpass = 'fuckyou'; $dbport = '3306'; $charset = 'utf8'; $dbo = array( // important! use actual prepared statements (default: emulate prepared statements) pdo::attr_emulate_prepares => false // throw exceptions in case of errors (default: stay silent) , pdo::attr_errmode => pdo::errmode_exception // fetch associative arrays (default: mixed arrays) , pdo::attr_default_fetch_mode => pdo::fetch_assoc ); [edit]
<?php error_reporting(e_all); ini_set('display_errors', 1); require_once("dbconn.php"); try { $conn = new pdo("mysql:host=$dbhost;dbname=$dbname;charset=utf8",$dbuser,$dbpass,$dbo); } catch (pdoexception $e) { echo $e->getmessage(); exit; } if(isset($_post['addbtn'])){ $title = $_post['title']; $pubid = $_post['pubid']; $q = $conn->prepare("insert series (title, pubid) values (:title, :pubid)"); $q->bindparam(':title',$title,pdo::param_str); $q->bindparam(':pubid',$pubid,pdo::param_int); $q->execute(); } $sql = 'select pubid, name publisher order name'; $q = $conn->query($sql); $q->setfetchmode(pdo::fetch_assoc); ?> <!doctype html> <html lang="eng"> <head> <meta charset="utf-8"> <title> comics db > add series </title> <link rel="stylesheet" type="text/css" href="comicsdb.css"> </head> <body> <div class="main"> <menu> <ul> <li><a href="index.php">home</a></li> <li><a href="add.php">add</a></li> <li><a href="edit.php">edit</a></li> <li><a href="delete.php">delete</a></li> <li><a href="list.php">list</a></li> <li><a href="search.php">search</a></li> </ul> </menu> <div class="pub_menu"> <form action="addseries.php" method="post"> <p> series: <input type="text" name="title" id="title" size="40" /><br /> <select> <option name="pubid" id="pubid">select</option> <?php while ($row = $q->fetch()){ ?> <option value="<?php echo $row['pubid']; ?>"><?php echo $row['name']; ?></option> <?php } ?> </select> <input type="submit" value="add series" name="addbtn" /> </p> </form> </div> </body> </html>
you didn't assign variables.
assign them post arrays:
if(isset($_post['title'],$_post['pubid'])){ $title = $_post['title']; $pubid = $_post['pubid']; $q = $conn->prepare("insert publisher (title, pubid) values (:title, :pubid)"); $q->bindparam(':title',$title,pdo::param_str); $q->bindparam(':pubid',$pubid,pdo::param_int); $q->execute(); } - kudos ghost catching this.
<select> bears name attribute, not <option>.
change <select> read <select name="pubid"> , remove name="pubid" <option>.
edit:
as per edit, still have unnamed attribute select:
<select> <option name="pubid" id="pubid">select</option> that needs read as:
<select name="pubid"> <option>select</option> that's why you're still getting undefined index notice.
plus, can move id inside it:
<select name="pubid" id="pubid">
Comments
Post a Comment