PHP form combobox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

PHP form combobox

Post by KevinCB »

I would like to apply a combo box to the form and allow the user to select from the drop down list. Now the book I'm working from tells me to get the selections from the ENUM field that I've already created in the DB, and then apply them to the combo box to create the list.

There's just one part of the code I'm struggling to get working it keeps coming up with a T_STRING error. This is what is in the book:

PHP Code:

Code: Select all

$get_list = "select id, concat_ws(', ', l_name, f_name) as display_name 
       from master_name order by l_name, f_name";
This is what I'm entering:

PHP Code:

Code: Select all

$get_list = "select 12_disk as display_name from store_12_singles";
The table is as follows:

item_id + 12_artist + 12_disk

Can anyone see what I'm doing wrong :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post the 2 lines before this line in question.
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

Post by KevinCB »

Just realised I think I've been doing a classic mistake. I've been entering this code in the middle of a form, and now I think of it, that's probably why it isn't working.

Code: Select all

if ($_POST[op] != "add") {
   //haven't seen the form, so show it
     $display_block = "<h1>Add a 12 Single</h1>
     <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
     <P><strong>Artist:</strong><br>
     <input type=\"text\" name=\"12_artist\" size=3 maxlength=100>

     <P><strong>Single Title:</strong><br>
     <input type=\"text\" name=\"item_name\" size=30 maxlength=75>

     <P><strong>Description:</strong><br>
     <textarea name=\"desc\" cols=35 rows=7 wrap=virtual></textarea>

     <P><strong>Price:</strong><br>
     <input type=\"text\" name=\"item_price\" size=10 maxlength=10>

     $get_list = "select 12_disk as display_name from store_12_singles";

     <P><strong>Image:</strong><br>
     <input type=\"text\" name=\"item_image\" size=30 maxlength=100>
      <input type=\"hidden\" name=\"op\" value=\"add\">

     <p><input type=\"submit\" name=\"submit\" value=\"Add 12" Single\"></p>
</FORM>";
So would I apply it before the form, and then add the combobox part to the existing form?

Stupid, stupid mistake :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well. it's kinda obvious if you look at the highlighted version.

If you don't have an editor that does highlighting, get one.
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

Post by KevinCB »

Ok, changed the script so that it is more correct, or so I thought, but it isn't working still. This is the script so far:

Code: Select all

<?php
//connect to database
  $conn = mysql_connect("localhost", "username", "password")
      or die(mysql_error());
  mysql_select_db("shoot_the_moon",$conn)  or die(mysql_error());
	
//get parts of record
	$get_list = "select 12_disk as disk_type from store_12_singles";
	$get_list_res = mysql_query($get_list)   or die(mysql_error());
	
	if(mysql_num_rows($get_list_res) < 1) {
	  //no records
	  $display_block .= "<p><em>Sorry, no records to select!</em></p>";
	
	} else if ($_POST['op'] != "add") {
   //haven't seen the form, so show it
	 $display_block .= "
	 <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
	 <P><strong>Artist:</strong><br>
	 <input type=\"text\" name=\"12_artist\" size=30 maxlength=100>
	 
	 <P><strong>Single Title:</strong><br>
	 <input type=\"text\" name=\"item_name\" size=30 maxlength=75>
	 
	 <P><strong>Description:</strong><br>
	 <textarea name=\"item_desc\" cols=35 rows=7 wrap=virtual></textarea>
	 
	 <P><strong>Select a Record to View:</strong><br>
	 <select name=\"sel_type">
	 <option value=\"\">-- Select One --</option>";
	 
	 while ($recs = mysql_fetch_array($get_list_res)) {
	     $disk_type = stripslashes($recs['disk_type']);
			 
			 $display_block .= "<option value=\"$disk_type\">
			 $disk_type = </option>";
	 }
	 $display_block .= "
	 </select>
	 
	 <P><strong>Price:</strong><br>
	 <input type=\"text\" name=\"item_price\" size=10 maxlength=10>
	 
	 <P><strong>Image:</strong><br>
	 <input type=\"text\" name=\"item_image\" size=30 maxlength=100>
	  <input type=\"hidden\" name=\"op\" value=\"add\">
		
	 <p><input type=\"submit\" name=\"submit\" value=\"Add Single\"></p>
	 </FORM>";
	 
} else if ($_POST['op'] == "add") {
   //time to add to tables, so check for required fields
	  if ($_POST['12_artist'] == "") {
		   header("Location: add12.php");
			 exit;
		}
	 
	 //add to store_items table
   $add_items = sprintf('
	 INSERT INTO store_items
   (cat_id, item_name, item_price, item_desc item_image)
   VALUES("5", "%s", "%s", "%s", "%s")
   ',
   mysql_real_escape_string($_POST['item_name']),
   mysql_real_escape_string($_POST['item_price']),
	 mysql_real_escape_string($_POST['item_desc']),
   mysql_real_escape_string($_POST['item_image'])
	 );
   mysql_query($add_items) or die(mysql_error());
	 
	 //get item id for use with other tables
	 $item_id = mysql_insert_id();
	 
	 if ($_POST['12_artist']) {
	    //something relevant so add to the book table
			$add_12 = sprintf('
			INSERT INTO store_12_singles
			(item_id, 12_artist, 12_disk)
			VALUES("%s", "%s", "%s")
			',
			$item_id,
			mysql_real_escape_string($_POST['12_artist']),
			mysql_real_escape_string($_POST['12_disk'])
			);
			mysql_query($add_12) or die(mysql_error());
	 }

   $display_block = "<h1>Record Added</h1>
	 <P>Your record has been added.  Would you like to
	  <a href=\"add12.php\">add another</a>?</p>
		<P>Go back to the
		<a href=\"adminmenu.php\">main menu</a></p>";
}
?>
Can anyone check this for me to see if I've done this correctly.

Also it comes up with an error:
Parse error: syntax error, unexpected '<' in add12.php on line 30
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

in line 29 and 30 you have to " that is missing an escape character

change it to:

Code: Select all

<select name=\&quote;sel_type\&quote;>
<option value=\&quote;\&quote;>-- Select One --</option>&quote;;
try to run it after that

/thallish
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

Post by KevinCB »

I've sorted the script and ran it again, and it doesn't list the values set by enum in the MySQL database.

Code: Select all

<?php
//connect to database
  $conn = mysql_connect("localhost", "username", "password")
      or die(mysql_error());
  mysql_select_db("shoot_the_moon",$conn)  or die(mysql_error());

    //get parts of record
    $get_list = "select 12_disk as disk_type from store_12_singles";
    $get_list_res = mysql_query($get_list)   or die(mysql_error());

if ($_POST['op'] != "add") {
   //haven't seen the form, so show it
     $display_block .= "
     <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">

     <P><strong>Artist:</strong><br>
     <input type=\"text\" name=\"12_artist\" size=30 maxlength=100>

     <P><strong>Single Title:</strong><br>
     <input type=\"text\" name=\"item_name\" size=30 maxlength=75>

     <P><strong>Description:</strong><br>
     <textarea name=\"item_desc\" cols=35 rows=7 wrap=virtual></textarea>

     <P><strong>Select a Record to View:</strong><br>
     <select name=\"sel_type\">
     <option value=\"\">-- Select One --</option>";

     while ($recs = mysql_fetch_array($get_list_res)) {
         $disk_type = stripslashes($recs['disk_type']);

             $display_block .= "<option value=\"$disk_type\">
             $disk_type = </option>";
     }
     $display_block .= "
     </select>
     <P><strong>Price:</strong><br>
     <input type=\"text\" name=\"item_price\" size=10 maxlength=10>

     <P><strong>Image:</strong><br>
     <input type=\"text\" name=\"item_image\" size=30 maxlength=100>
     <input type=\"hidden\" name=\"op\" value=\"add\">

     <p><input type=\"submit\" name=\"submit\" value=\"Add Single\"></p>
     </FORM>";

 } else if ($_POST['op'] == "add") {
   //time to add to tables, so check for required fields
      if ($_POST['12_artist'] == "") {
           header("Location: add12.php");
             exit;
        }
     
     //add to store_items table
   $add_items = sprintf('
     INSERT INTO store_items
   (cat_id, item_name, item_price, item_desc item_image)
   VALUES("5", "%s", "%s", "%s", "%s")
   ',
   mysql_real_escape_string($_POST['item_name']),
   mysql_real_escape_string($_POST['item_price']),
   mysql_real_escape_string($_POST['item_desc']),
   mysql_real_escape_string($_POST['item_image'])
     );
   mysql_query($add_items) or die(mysql_error());

          //get item id for use with other tables
            $item_id = mysql_insert_id();

          if ($_POST['12_artist']) {
            //something relevant so add to the book table
            $add_12 = sprintf('
            INSERT INTO store_12_singles
            (item_id, 12_artist, 12_disk)
            VALUES("%s", "%s", "%s")
            ',
            $item_id,
            mysql_real_escape_string($_POST['12_artist']),
            mysql_real_escape_string($_POST['12_disk']));
            mysql_query($add_12) or die(mysql_error());
     }
    $display_block = "<h1>Record Added</h1>
     <P>Your record has been added.  Would you like to
      <a href=\"add12.php\">add another</a>?</p>
        <P>Go back to the
        <a href=\"adminmenu.php\">main menu</a></p>";
}
?>
How come it isn't showing up the enum values that I have assigned to the field?
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

Post by KevinCB »

bump
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you remember to echo/print $display_block?
KevinCB
Forum Commoner
Posts: 32
Joined: Tue Mar 01, 2005 6:00 am

Post by KevinCB »

Yes I've done that, I think it might be the wrong code for what I'm trying to do. As I have no idea I will try and make it more understandable.

What I'm trying to do is bind an enum value to a combobox, I don't know if what I've done does this, as it was a snippet of a script that just selects from the combobox.
Post Reply