Page 1 of 1

About PHP Generated Combo Boxes

Posted: Fri Jun 16, 2006 5:13 am
by Lordrye
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello ,im a newbie here. I just have some questions regarding how to pass selected values from a PHP generated combo box. I have 3 PHP-generated combo boxes, the values of which come from the database, This is my code :

Code: Select all

<form id="form1" name="form1" method="post" action="saveMix.php">
  <table width="279" border="0">
    <tr>
      <th width="51" scope="row">Copy</th>
      <td width="218"><label>
        <?php  
	  connectDB(); 
	  getCopies(); 	  ?>
      </label></td>
    </tr>
    <tr>
      <th scope="row">Volume</th>
      <td><label>
        <?php 
		connectDB();
		getVolumes(); ?>
      </label></td>
    </tr>
    <tr>
      <th scope="row">Issue</th>
      <td><label>
        <?php 
		connectDB();
		getIssues(); ?>
      </label></td>
    </tr>
.....


function getIssues(){
       $query = "select distinct issue from magazines order by issue";
    	$result = mysql_query($query);
       echo " <select name='issues'> ";
       while ($row = mysql_fetch_assoc($result))
        { extract($row);
          echo "<option value=>$issue</option>";
        }
       mysql_close();
	   echo "</select>";
}

function getCopies(){

        $query = "select distinct copy from magazines order by copy";
    	$result = mysql_query($query);
       echo " <select name='copies'> ";
       while ($row = mysql_fetch_assoc($result))
        { extract($row);
          echo "<option value=>$copy</option>";
        }
       mysql_close();
	   echo "</select>";

 }



function getVolumes(){
    $query = "select distinct vol from magazines order by vol";
    	$result = mysql_query($query);
       echo " <select name='volumes'> ";
       while ($row = mysql_fetch_assoc($result))
        { extract($row);
          echo "<option value=>$vol</option>";
        }
       mysql_close();
	   echo "</select>";


}
When I try to select items from the 3 combo boxes, the selected items are not saved in the database. Here's my code for saveMix.php

Code: Select all

<?php
 $host = 'localhost';
 $user = 'root';
 $pass = '';

   $conn = mysql_connect($host, $user, $pass) or die ('Error connecting to mysql');-
    print mysql_error();
	$dbname = 'archives';
	mysql_select_db($dbname);
	
	$copy=$_GET['copies'];
	$type=$_POST['types'];
	$issue=$_POST['issues'];
	$volume=$_POST['volumes'];
	$type=$_POST['types'];
	$author=$_POST['authors'];
	$topic=$_POST['topic'];
	$details=$_POST['details'];
						
    
  

			
  $query = "insert into mix values('" . $copy .  "','" . $volume . "','" .  $issue . "','"  .  $type . "','"  .  $author . "','"  .  $topic . "','"   .  $details . "')" ;
	
	$result = mysql_query($query);   //blah blah blah..




Need help pls ASAP. Thank you and more power.


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jun 16, 2006 7:40 am
by printf
In your functions (each one)

// function getIssues(){

change this....

Code: Select all

echo "<option value=>$issue</option>";

to this...

Code: Select all

echo "<option value='" . htmlspecialchars ( $issue, ENT_QUOTES ) . "'>" . $issue . "</option>";

//function getCopies(){

change this...

Code: Select all

echo "<option value=>$copy</option>";

to this...

Code: Select all

echo "<option value='" . htmlspecialchars ( $copy, ENT_QUOTES ) . "'>" . $copy . "</option>";

//function getVolumes(){

change this...

Code: Select all

echo "<option value=>$vol</option>";

to this...

Code: Select all

echo "<option value='" . htmlspecialchars ( $vol, ENT_QUOTES ) . "'>" . $vol . "</option>";


pif!

Posted: Fri Jun 16, 2006 7:47 am
by tecktalkcm0391
This:

$query = "insert into mix values('" . $copy . "','" . $volume . "','" . $issue . "','" . $type . "','" . $author . "','" . $topic . "','" . $details . "')" ;

should be this:

Code: Select all

$query = "insert into mix (COLUMN1, COLUMN2,  COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7) values('" . $copy .  "','" . $volume . "','" .  $issue . "','"  .  $type . "','"  .  $author . "','"  .  $topic . "','"   .  $details . "')" ;

Posted: Fri Jun 16, 2006 8:01 am
by printf
tecktalkcm0391 wrote:This:

$query = "insert into mix values('" . $copy . "','" . $volume . "','" . $issue . "','" . $type . "','" . $author . "','" . $topic . "','" . $details . "')" ;

should be this:

Code: Select all

$query = "insert into mix (COLUMN1, COLUMN2,  COLUMN3, COLUMN4, COLUMN5, COLUMN6, COLUMN7) values('" . $copy .  "','" . $volume . "','" .  $issue . "','"  .  $type . "','"  .  $author . "','"  .  $topic . "','"   .  $details . "')" ;

You really don't need to use column naming if your inserting a full row of values, the only time it is really needed, is when you want to insert (1) or more values, but not all the values that table has, so that the column matches the value, allowing the database to understand exactly where to place the value for that less than full insert!

pif!

Posted: Fri Jun 16, 2006 9:27 am
by Lordrye
to printf and tecktalkcm0391:

thanks for the valuable examples. My page now works.Thank you very much and more power.