PHP; PDO; MySQL; INSERT query not taking input

mcdz89

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">&nbsp;<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
);

?>
Dwza

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP; PDO; MySQL INSERT query with drop down

From Dev

php mysql (INSERT QUERY)

From Dev

Mysql - conditional insert query with select and PDO

From Dev

PDO insert into MySQL Database not working with another PDO query

From Dev

PDO Insert operation using PHP and MYSQL not working

From Dev

PHP, PDO, MySQL - Multiple INSERT vulnerable to injection?

From Dev

php and mysql insert input array

From Dev

PHP MySQL Insert Into query not working

From Dev

insert php mysql query in hyperlink

From Dev

PHP mysql query insert where

From Dev

PHP PDO Correct use of JOINS for mySQL query

From Dev

How to use async Mysql query with PHP PDO

From Dev

Dynamically Building PHP PDO MySQL query

From Dev

INSERT INTO SELECT PDO query

From Dev

INSERT INTO SELECT PDO query

From Dev

MySQL php INSERT INTO taking very very long time

From Dev

PHP PDO, Return value object after insert query

From Dev

run insert query once only when the database is created php pdo

From Dev

PHP PDO can't run query INSERT INTO with SELECT

From Dev

How to process input value from HTML form in PHP to insert into MySQL query?

From Dev

Converting MySql Insert To PDO

From Dev

INSERT INTO array with MySQL and PDO

From Dev

PDO insert BindParam mysql

From Dev

What is the best way to insert multiple rows in PHP PDO MYSQL?

From Dev

insert into using same row(same primary key) mysql php pdo

From Dev

Insert multiple values via PHP PDO into MySQL database

From Dev

insert into using same row(same primary key) mysql php pdo

From Dev

How to insert unknown amount of colums in mysql using PHP PDO

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

Related Related

HotTag

Archive