1

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>
8
  • You are preparing your query but never actually executing it. Commented Mar 31, 2015 at 23:21
  • You're not executing anything. Here php.net/manual/en/pdostatement.execute.php Commented Mar 31, 2015 at 23:21
  • @HoboSapiens I'm in a timezone where it's 1 second later than where you are ;-) Commented Mar 31, 2015 at 23:21
  • Ok I just added an 'execute()' to it and it still did not do anything Commented Mar 31, 2015 at 23:25
  • The SELECT statement? that works, the INSERT does not. Commented Mar 31, 2015 at 23:28

1 Answer 1

1

You didn't assign anything to your variables.

Assign them to your POST arrays:

if(isset($_POST['title'],$_POST['pubid'])){

$title = $_POST['title'];
$pubid = $_POST['pubid'];

      $q = $conn->prepare("INSERT INTO publisher (title, pubid) VALUES (:title, :pubid)");
      $q->bindParam(':title',$title,PDO::PARAM_STR);
      $q->bindParam(':pubid',$pubid,PDO::PARAM_INT);
      $q->execute();
   }

  • Kudos to Ghost for catching this.

<select> bears the name attribute, not the <option>.

Change your <select> to read as <select name="pubid"> and remove the name="pubid" from the <option>.


Edit:

As per your edit, you still have an unnamed attribute for the select:

<select>
<option name="pubid" id="pubid">Select</option>

that needs to read as:

<select name="pubid">
<option>Select</option>

that's why you're still getting an undefined index notice.

Plus, you can move your id inside it:

<select name="pubid" id="pubid">
Sign up to request clarification or add additional context in comments.

2 Comments

Comments are not for extended discussion; this conversation has been moved to chat.
@bluefeet Thank you. The chat button never showed up for me when trying to help out the OP at the time. Slight bug?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.