PDO dropdown boxes post

That looks ok, could it really be the extra space after :courseId?

[php]$stmt->bindParam(’:courseId ', $_POST[‘Course_Name’], PDO::PARAM_INT); // here[/php]

If that doesn’t help you could always fall back to using unnamed placeholders

[php]$stmt = $db->prepare(“SELECT Module_Name,ModuleID, Module_Code, Module_Year FROM coursemodule WHERE CourseID = ? ORDER BY ModuleID ASC”);
$stmt->execute(array($_POST[‘Course_Name’]));[/php]

But still, the named placeholders error would haunt me…

[php]echo ‘<option name=“courseID” value"{$row[‘CourseID’]}">{$row[‘Course_Name’]}’;[/php]

This will set a value for what is selected.

Then , Module_Name is not a method of output, so you would do, $output[‘Module_Name’] to get the name of the module stored.

If the named value is null, because there is no value for all the post returns, there is nothing to pull from the database. The is nothing in the where clause.

Your right that got rid of the error; now the dropdown box is shown and populated but doesn’t do anything when I send it.

I tried and got the error on line 34 "Parse error: syntax error, unexpected ‘CourseID’ (T_STRING), expecting ‘,’ or ‘;’ in N:…

[code]<?php
require_once(‘connect.php’);

if ( isset( $_POST[‘send’]))

{

    $sql = "SELECT Module_Name,ModuleID, Module_Code, Module_Year  FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC";
   $stmt = $db->prepare($sql);
   $stmt->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT);
   $stmt->execute();
   while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
      echo "<p>" . $output->Module_Name  . " " . $output->Module_Code  . "</p>\n";
	  




 
 }

}

?>

<?php
   $stmt = $db->prepare('Select Course_Name, CourseID from coursename');
   $stmt->execute();

      while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<option name="courseID" value"{$row['CourseID']}">{$row['Course_Name']}</option>';
      }
	 

   ?>
[/code]

[php] echo “<option name=‘courseID’ value=’{$row[‘CourseID’]}’>{$row[‘Course_Name’]}”;[/php]

The interpreter is confused with the quotes.

Error gone with that solution and its displaying the dropdown box but a selection doesn’t print anything.

[php]

<?php require_once('connect.php'); if ( isset( $_POST['send'])) { echo "
Course ID is: $_POST['Course_Name']

"; $sql = "SELECT Module_Name,ModuleID, Module_Code, Module_Year FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC"; $stmt = $db->prepare($sql); $stmt->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT); $stmt->execute(); while ($output = $stmt->fetch(PDO::FETCH_OBJ)) { echo "

" . $output['Module_Name'] . " " . $output['Module_Code'] . "

\n"; } } ?> <?php
    $stmt = $db->prepare('Select Course_Name, CourseID from coursename');
    $stmt->execute();

       while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
     echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
       }
	 

    ?>
</select> 
[/php]

Just tried that and got the following error
"Parse error: syntax error, unexpected ‘’ (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in line "

Sorry, enclose the post in the first echo in brackets {}

Right I did that by adding them as shown"echo “
CourseID is: {$_POST}[‘Course_Name’]

”;"
Now im getting the error "Notice: Array to string conversion " on line 10.
Though it is displaying “CourseID is: Array[‘Course_Name’]” on the page.

Did you do a direct copy and paste? That should not be an array if you removed the names from the options and just left the values in place.

Sorted thanks for you help.
Just one quick question say I want to create a duplicate dropdown box that siits beside the first one how to do I go about adding this without interfering with each other?

I have to add a second dropdown box that is triggered using the same send button.
So heres the code for one dropdown box:

[code]<?php
require_once(‘connect.php’);

  if ( isset( $_POST['send']))
  
  {
  
      echo "<br>Course ID is: $_POST['Course_Name']<br><br>";

       $sql = "SELECT Module_Name,ModuleID, Module_Code, Module_Year  FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC";
     $stmt = $db->prepare($sql);
     $stmt->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT);
     $stmt->execute();
     while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
        echo "<p>" . $output['Module_Name']  . " " . $output['Module_Code']  . "</p>\n";
   }

}

?>

<?php
     $stmt = $db->prepare('Select Course_Name, CourseID from coursename');
     $stmt->execute();





        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
        }
    ?>
 </select> 
[/code] And heres my attempt at adding a second dropdown box. [code]<?php require_once('connect.php');
  if ( isset( $_POST['send']))
  
  {
  
      echo "<br>Course ID is: $_POST['Course_Name']<br><br>";

       $sql = "SELECT Module_Name,ModuleID, Module_Code, Module_Year  FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC";
	   
     $stmt = $db->prepare($sql);
	 $sql1 = "SELECT Module_Name,ModuleID, Module_Code, Module_Year  FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC";
	 $stmt1 = $db->prepare($sql1);
     $stmt->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT);
	 $stmt1->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT);
	 
     $stmt->execute();
	 $stmt1->execute();
	 
     while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
        echo "<p>" . $output['Module_Name']  . " " . $output['Module_Code']  . "</p>\n";
		
		while ($output = $stmt1->fetch(PDO::FETCH_OBJ)) {
        echo "<p>" . $output['Module_Name']  . " " . $output['Module_Code']  . "</p>\n";
   }

}

?>

<?php
     $stmt = $db->prepare('Select Course_Name, CourseID from coursename');
     $stmt->execute();
	
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
	  
	  $stmt1 = $db->prepare('Select Course_Name, CourseID from coursename');
     $stmt1->execute();
	  
	  while($row = $stmt1->fetch(PDO::FETCH_ASSOC)) {
      echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
        } 
     ?>
 </select> 
[/code]

Is this the right idea or am I going about this the wrong way?

Currently I have managed to create another dropdown box and from the last post I learned I needed to have them in one form as I only want one submit/send button.
There’s no error messages though only the results from one dropdown box is shown.
Current attempt-

[code]<?php
require_once(‘connect.php’);

    if ( isset( $_POST['send']))
    
    {
    
        echo "<br>Course ID is: $_POST['Course_Name']<br><br>";

        $sql = "SELECT Module_Name,ModuleID, Module_Code, Module_Year  FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC";
	   
      $stmt = $db->prepare($sql);
	
      $stmt->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT);
	 
	 
      $stmt->execute();
	 
	 
      while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
         echo "<p>" . $output['Module_Name']  . " " . $output['Module_Code']  . "</p>\n";
		
		
    }

}

?>
<?php if ( isset( $_POST['send1'])) { echo "
Course ID is: $_POST['Course_Name']

"; $sql1 = "SELECT Module_Name,ModuleID, Module_Code, Module_Year FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC"; $stmt1 = $db->prepare($sql); $stmt1->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT); $stmt1->execute(); while ($output = $stmt->fetch(PDO::FETCH_OBJ)) { echo "

" . $output['Module_Name'] . " " . $output['Module_Code'] . "

\n"; } } ?>
<form method='post'>
   <select id="Course_Name" name="Course_Name">
   <?php
         
  
      $stmt = $db->prepare('Select Course_Name, CourseID from coursename');
      $stmt->execute();
	
         while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
       echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
	  
	  
         } 
      ?>
  </select> 
  
  
  
   <select id="Course_Name1" name="Course_Name1">
   <?php
         
  
      $stmt1 = $db->prepare('Select Course_Name, CourseID from coursename');
      $stmt1->execute();
	
         while($row = $stmt1->fetch(PDO::FETCH_ASSOC)) {
       echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
	  
	  
         } 
      ?>
  </select> 
  
  
  
<input type="submit" value="Send" name="send">
</form>[/code]

The form is only submitting one dropdown box (“send”)so how do I go about getting the other one working too?

That doesn’t make any sense, honestly. You would have two dropdowns on a successful form submission, Course_Name and Course_Name1. send is just the name of the submit button.

And I don’t understand why you are running the same query, for the same information, using different names. IF that was necessary, you would use a function for it, but in your case it is just wasted space.

Ok in reference to the submit button I will attempt to change my code. I’m running the same query so users can view selection results beside each other (in this case modules).
When I run the same query without changing the names the selection of one dropdown causes the results to be printed twice in the results i.e.only one drop down box works at a time though results are printed twice.

Hope that explains why; should I approach implementing two queries differently because they are the same?

Heres my latest attempt where the second dropdown box is not working.

[code]<?php
require_once(‘connect.php’);

       if ( isset( $_POST['send']))
       
       {
       
           echo "<br>Course ID is: $_POST['Course_Name']<br><br>";


          $sql = "SELECT Module_Name,ModuleID, Module_Code, Module_Year  FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC";
          $sql1 = "SELECT Module_Name,ModuleID, Module_Code, Module_Year  FROM coursemodule WHERE CourseID = :courseId ORDER BY ModuleID ASC";
	   
        $stmt = $db->prepare($sql);
        $stmt1 = $db->prepare($sql);
	
        $stmt->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT);
        $stmt1->bindParam(':courseId', $_POST['Course_Name'], PDO::PARAM_INT);
	 
	 
        $stmt->execute();
	 
	 
        while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
           echo "<p>" . $output['Module_Name']  . " " . $output['Module_Code']  . "</p>\n";
		
		
      }
   $stmt1->execute();
	 
	 
        while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
           echo "<p>" . $output['Module_Name']  . " " . $output['Module_Code']  . "</p>\n";
		
		
      }
  }
  
  ?>





  <form method='post'>
     <select id="Course_Name" name="Course_Name">
     <?php
           
    
        $stmt = $db->prepare('Select Course_Name, CourseID from coursename');
        $stmt->execute();
	
           while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
	  
	  
           } 
        ?>
    </select> 
  
  
  
   <select id="Course_Name1" name="Course_Name1">
     <?php
           
    
        $stmt1 = $db->prepare('Select Course_Name, CourseID from coursename');
        $stmt1->execute();
	
           while($row = $stmt1->fetch(PDO::FETCH_ASSOC)) {
         echo "<option value='{$row['CourseID']}'>{$row['Course_Name']}</option>";
	  
	  
           } 
        ?>
    </select> 
  
  
  
  <input type="submit" value="Send" name="send">
 </form>[/code]

I have been trying to put it in a function but honestly don’t know how to.
Any help would be appreciated thanks.

Mikra, what is not working on the second drop-down?

Just for you information. A form can have an unlimited number of drop-downs as it can other input fields.
Each must have a unique name. Therefore, the Course_Name and Course_Name1. BUT, in your code when
you use these two drop-downs, you must remember to use the two different names. Therefore, you will
need to use $_POST[‘Course_Name’] and $_POST[‘Course_Name1’] in your code where you need to access
the data when it is posted. You can create an array of the drop-downs, but it is a little confusing unless you
have done it in the past.

A few more comments. If the $sql and $sql1 are exactly the same, why duplicate them? You only need to
pull the courses once. Just use the first one. If the two (or more) drop-downs ALWAYS have exactly the
same options, it is easier to just create a string that holds the tags. So, one string
would contain all the courses. Then, just display that string between the two different
tags. Same content… If the two queries to pull the courses are different, then keep it as two different
ones. Did that make sense?

Now, what exactly is not working for you? Just for your information, you can review the page’s code which
is the OUTPUT from PHP and see if the two selects are well-formed and where the errors are by vieing it
live. So, bring your page up online where the two select’s should be. RIGHT-CLICK on the page and select
VIEW-SOURCE option, VIEW-PAGE-SOURCE on Firefox. This will show you the live code after PHP has done
it’s job. Look down to where the two selects are and review the layout. It should show you where the
code is not correct. You might be able to see something that jumps out as incorrect.

Let us know…

Got the issue solved yesterday; there was a mistake in my code as you said I didn’t post the second selection.
Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service