I am working on a personal project trying to make a PDO and MySQL query. I have been fighting with this off and on for several weeks. I have done extensive research and found many good tutorials and questions on other forums. However I am absolutely stumped at this issue. The code I have currently only accepts one value to pass into the db. However I am always getting a Undefined variable notice. When I initialize an empty variable for the variable used, it just adds an empty record to the db. What do I do about this. I have held off on asking for as long as I can.
addpub.php
<?php
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;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Comics DB > Add Publisher
</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="addpub.php" method="POST" enctype="text/plain" id="form">
<p>
Publisher: <input type="text" name="name" id="name"> <input type="submit" value="Add Publisher" name="addPubBtn" id="addPubBtn">
</p>
</form>
<?php
$q = $conn->prepare("INSERT INTO publisher (name) VALUES (:name)");
$q->bindValue(':name', $name, PDO::PARAM_STR);
if(isset($name))
$q->execute();
else
echo "FAIL!";
?>
</div>
</body>
</html>
dbconn.php
<?php
$dbhost = '127.0.0.1';
$dbname = 'comicsdb';
$dbuser = '****';
$dbpass = '****';
$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
);
?>
Your source should look more like this (its a sample if you have only one file)
content of addpub.php print_r($_POST);
if(isset($_POST['name'])){
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;
}
$q = $conn->prepare("INSERT INTO publisher (name) VALUES (:name)");
$q->execute(array(':name'=>$_POST['name']));
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
Comics DB > Add Publisher
</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="addpub.php" method="POST">
<p>
Publisher:
<input type="text" name="name" />
<input type="submit" value="Add Publisher" name="addPubBtn" />
</p>
</form>
</div>
</body>
</html>
in this case you insert the data from the form to your db. If the target of your form is an other page you have to add your "pdo insert part" to the POST-Target. Otherwise you will never have the data there!
Also you should not mix output source with logic source. So may take a look at some template systems like Smarty.