I am struggling with this two input INSERT query. It will not insert anything to the db. First, it is supposed to accept user input and the dropdown menu selection which is the id from another table hence the SELECT query. Once this code is fixed I will set up a relationship between the two tables involved via the '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 INTO 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 FROM publisher ORDER BY 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 INTO 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 FROM publisher ORDER BY 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>