473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Mysql column populating drop down menu issue

38
Hi there,

I've essentially got a form with several drop down, each populated by columns in various tables.

The populating bit works fine - the column rows appear as they should in the menu.

Ideally the user needs to make their selections, and enter the form into a new database table.

The problem is, when the selection is entered into the new table, only the first word of the string is entered. For example, the drop down may have...

"Category One"
"Category Two"
"Category Three"

When the form is submitted only the word "Category" is entered into the database.

Here is the code, it takes the column 'courseTitle' from one table, displays it in a menu, and then saves the selection into a new table as 'eventName'.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. include("connect.php"); 
  3. $query="SELECT courseTitle FROM course";
  4. $result = mysql_query ($query);
  5. echo "<select name='eventName'=''></option>";
  6. while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  7. echo "<option value=$nt[courseTitle]>$nt[courseTitle]</option>";
  8. }
  9. echo "</select>";// Closing of list box 
  10. ?>
  11.  

Apologies if I've made it slightly long winded.
Aug 4 '09 #1
14 4296
dlite922
1,584 Expert 1GB
I don't know why you have an equal sign and extra quotes here:

Expand|Select|Wrap|Line Numbers
  1. "<select name='eventName'=''></option>";
  2.  
but debug your post vars like this
Expand|Select|Wrap|Line Numbers
  1.  die(print_r($_POST))
and see if the data gets there fine.

If that's fine, look in your insert SQL (not shown in your code). Just print it and see if it looks right.

If you don't find anything, post that code back here and any other notes/comments.

Apologies if I've made it slightly long winded.
Nope, it's perfect. I wish every user provided this concise amount of info.



Dan
Aug 4 '09 #2
Philth
38
Many thanks for the reply Dan,

I didn't notice the extra quotes. They made no difference either way.

The insert SQL is nothing special, I can't spot anything wrong with it.

Expand|Select|Wrap|Line Numbers
  1. <? 
  2.  
  3. if($_POST['submit'])  
  4. {
  5. include("connect.php"); 
  6.  
  7.    $eventName = $_POST['eventName'];
  8.    $eventCategory1 = $_POST['eventCategory1'];
  9.    $eventCategory2 = $_POST['eventCategory2'];
  10.    $eventCategory3 = $_POST['eventCategory3'];   
  11.    $eventLocation = $_POST['eventLocation'];
  12.    $eventDate = $_POST['eventDate'];   
  13.  
  14.    $result=MYSQL_QUERY("INSERT INTO events (eventName, eventCategory1, eventCategory2, eventCategory3, eventLocation, eventDate)".
  15.       "VALUES ('$eventName', '$eventCategory1', '$eventCategory2', '$eventCategory3', '$eventLocation', '$eventDate')"); 
  16.  
  17.    echo "New Course Event Added"; 
  18. }
  19. ?>
  20.  
Aug 4 '09 #3
dlite922
1,584 Expert 1GB
echo $eventName right before the mysql_query() call, and print it's output. I don't know if you know this, but here's how I check my variables when I debug:

Expand|Select|Wrap|Line Numbers
  1. die(var_dump($eventname));  
Another thing I can think of is your MySQL table definition. perhaps the field name is not long enough. ALTER TABLE and change that field to VARCHAR(100).

FYI: You can do "desc events" to give you a definition of the table from a mysql command prompt, if you have access to one.

Let me know,



Dan
Aug 4 '09 #4
Philth
38
I added the debug line on line 13 of the second script I posted - it returned the value "Null". The field is set to VARCHAR(100).

Was line 13 the right place to put it?
Aug 5 '09 #5
Markus
6,050 Expert 4TB
@Philth
That's because you probably did $eventname where it should be $eventName.

Anyway, var_dump() your POST array, to check it's being populated correctly by your form.

Expand|Select|Wrap|Line Numbers
  1. var_dump($_POST);
  2. // or
  3. print_r($_POST);
  4.  
Also, with your mysql_query()ies, while debugging, you're provided the function mysql_error() which is populated with a description of the latest mysql error (duh). So, as is popular, we append 'or die(mysql_error());' to any mysql queries - some people don't like the 'or die()' statements, but it's purely preferential.

Expand|Select|Wrap|Line Numbers
  1. mysql_query($query) or die(mysql_error());
  2.  
Mark.
Aug 5 '09 #6
Philth
38
Mark,

Thanks for the reply.

The var_dump gave the following info...

array(7) { ["eventName"]=> string(6) "Master" ["eventCategory1"]=> string(4) "VHDL" ["eventCategory2"]=> string(4) "VHDL" ["eventCategory3"]=> string(4) "VHDL" ["eventLocation"]=> string(7) "Germany" ["eventDate"]=> string(0) "" ["submit"]=> string(13) "Add New Event" }

Event name should read "Master VHDL Effectively".

I'm seriously confused.
Aug 5 '09 #7
Markus
6,050 Expert 4TB
Ah, I see the problem: If you do not use quotes around an HTML attribute's value, it'll only read up to the first space as the value. Consider the following:

Expand|Select|Wrap|Line Numbers
  1. <input value=my name is mark name=name />
  2. // $_POST['name'] will hold the value 'my'.
  3. <input value="my name is mark" name="name" />
  4. // $_PSOT['name'] will hold "my name is mark"
  5.  
Always surround attribute values with quotes.

Mark.
Aug 5 '09 #8
Philth
38
Hi Mark,

Apologies for not understanding. If the issue is to do with the values before being posted, then the error must be with the first script that I posted?

Expand|Select|Wrap|Line Numbers
  1. <?
  2. include("connect.php"); 
  3. $query="SELECT courseTitle FROM course";
  4.  
  5.  
  6. $result = mysql_query ($query);
  7. echo "<select name='eventName'></option>";
  8.  
  9. while($nt=mysql_fetch_array($result)){
  10. echo "<option value=$nt[courseTitle]>$nt[courseTitle]</option>";
  11.  
  12. }
  13. echo "</select>";
  14. ?>
  15.  
All HTML values are quoted.
Aug 5 '09 #9
Dormilich
8,658 Expert Mod 8TB
@Philth
quoted?

..................
Aug 5 '09 #10
Markus
6,050 Expert 4TB
@Dormilich
Yeah, what Dorm said. ;)
Aug 5 '09 #11
Philth
38
Yeah, sorry for being dim today.

Thanks for everyones help.
Aug 5 '09 #12
Dormilich
8,658 Expert Mod 8TB
and while we're quoting......... you should always quote the array's keys (unless they're variables or indeed constants (which is btw rather rare))
Aug 5 '09 #13
dlite922
1,584 Expert 1GB
@Philth
Me too, I knew it has something to do with the values not posting correctly, thus form issue.

I always quote my html attributes. It's best to always ensure they're valid also:

http://validator.w3.org/

preferably with XHTML 1.0

Cheers,



Dan
Aug 5 '09 #14
Dormilich
8,658 Expert Mod 8TB
@dlite922
sadly, there is no XHTML support in IE.......
Aug 5 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jason | last post by:
I have a client that would like to have drop down menus added to a nav bar that is generated from MySQL. Is it possible to have a dynamically driven DHTML menu from MySQL? example link...
4
by: Mike Wilcox | last post by:
I use the following code snippet to build a drop down menu with the results of a query. How can I set the initial value of this input based on the result of another query? What I am trying to do...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
9
by: elyob | last post by:
Hi, I'm looking at storing snippets of details in MySQL about what credit cards a business excepts. Rather than have a whole column for Visa, another for Amex etc ... I am looking at having a...
3
by: Angelos | last post by:
Hello again, I have this dynamic menu and I want to change the order of the menu items... I added a column in the database wich has an integer value for ordering the menuitems. But the only way...
0
by: tusaar | last post by:
Hi all I am in big need for a drop down menu created with php, mysql and ajax. Exactly, I need three drop down menu (Category, Subcategory and Item). The data of each drop down will come from...
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
7
by: dongletran06 | last post by:
Hi, Please help me find out what wrong with my codes in inputting from my form to mysql database using drop down menu. Below is the codes I used. Only the drop down is not working but the "input...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.