473,508 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Autofill fields in a form

12 New Member
Ok please forgive me, as I am really new to the whole PHP programming...

What I am attempting to do is on a form page create a drop down list that is populated from information in a database. For example a list of company names, then with this that would populate one more drop down list with the department they belong to if any.

Once both of those are chosen and the user hits submit it would populate text fields below. For example Name, company, phone number, email so on and so forth.

At this point the first drop down list box shows up but is empty. Here is the code I have for that:

Expand|Select|Wrap|Line Numbers
  1. <select name="comapny_list" size="1" id="company_list"> 
  2. <?php 
  3. include "config.php";
  4.  
  5. //connect to the mysql server
  6. $link = mysql_connect($server, $db_user, $db_pass) or die("Could not connect to MySQL");
  7.  
  8. //select the database
  9. mysql_select_db($database) or die ("Could not select database");
  10.  
  11. // Filling up the selection box
  12.  
  13. $query = mysql_query ("SELECT * FROM " . $table . " WHERE 'Active' = '1'", $link) or die("<center>There was an error and we could load the Client list</center>"); 
  14. $result = mysql_result($query); 
  15. while ($row = mysql_fetch_array($result)){ 
  16. $comp_name = $row2['company'];  
  17.  
  18. echo "<option value='$comp_name'>{$comp_name}</option>"; 
  19. ?> 
  20. <option value=""></option> 
  21. </select> 
Any suggestions would be much appreciated! Thanks!

*Edit here is the error form the error log sorry about that...
Wrong parameter count for mysql_result() in /htdocs/forms/orderform2.php on line 29
Expand|Select|Wrap|Line Numbers
  1. PHP Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /htdocs/forms/orderform2.php
  2.  on line 30
  3.  
Nov 2 '07 #1
12 2651
Atli
5,058 Recognized Expert Expert
Hi Merlin. Welcome to TSDN!

Try printing the query before you execute it, just to see that it looks as you expect it to. It's so easy to make a typo in there somewhere and it helps to see it all combined.

It is always a good idea to check whether your mysql result is valid to avoid the kind of warning you posted above. This can be done like so:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query(...);
  2. if($result) {
  3.   # Rest of the code.
  4. }
  5. else {
  6.   # Print error.
  7. }
  8.  
Note that your use of the mysql_result() function is not correct in your code. I would drop that and use the original result from the mysql_query() function.

I noticed that in your query you match a column, 'Active', against a number, '1'.
The field name itself should not be quoted, not unless you use back-ticks.

If the number is in fact the number 1, rather than the character 1, it should not be quoted either. I do suspect, however, that the 'Active' field is a CHAR(1) field, in which case your approach is the right one.

I'd recommend changing "SELECT * FROM ..." into "SELECT company FROM ..." because you are only using that one column. No need to fetch a bunch of columns you will never use. That only serves to slow down your server.

I'd also recommend changing from the mysql_fetch_array() function to the mysql_fetch_assoc() function to increase efficiency. The latter only creates an associative array, where the former creates a combination of both associative and indexed arrays.
Nov 2 '07 #2
Modern Merlin
12 New Member
I tried some of the things you suggested by starting with the printing the query to the screen. I also changed my query to company as per your suggestion.

What it returns is Resource id #4. This isnt anywhere in my database.

So currently all I am trying to do is select the company field and put it in a drop down list to select from, but I cant even get that to print to the screen.
Nov 3 '07 #3
Atli
5,058 Recognized Expert Expert
I tried some of the things you suggested by starting with the printing the query to the screen. I also changed my query to company as per your suggestion.

What it returns is Resource id #4. This isnt anywhere in my database.

So currently all I am trying to do is select the company field and put it in a drop down list to select from, but I cant even get that to print to the screen.
What you are printing is the result ID you get from your MySQL server. What I meant is, print the actual query and the error. Something like:
Expand|Select|Wrap|Line Numbers
  1. # Create and execute query
  2. $sql = "SELECT col from tbl"
  3. $result = mysql_query($sql);
  4.  
  5. # Print query for debugging purposes
  6. echo "<pre>$sql</pre>";
  7.  
  8. # Check result
  9. if($result) {
  10.   # Print all rows
  11.   $rowno = 0;
  12.   while($row = mysql_fetch_assoc($result) {
  13.     $rowno++;
  14.     echo "Row $rowno:<br />"
  15.     foreach($row as $_k => $_v) {
  16.       echo " - $_k = $_v<br />";
  17.     }
  18.   }
  19. else {
  20.   # Print error
  21.   echo "<b>Query failed:</b> ". mysql_error();
  22. }
  23. echo "</pre>";
  24.  
Nov 3 '07 #4
Modern Merlin
12 New Member
I feel like an idiot LOL

I tried what you have there and I get a parse error....

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. include "config.php";
  3.  
  4. //connect to the mysql server
  5. $link = mysql_connect($server, $db_user, $db_pass) or die("Could not connect to MySQL");
  6.  
  7. //select the database
  8. mysql_select_db($database) or die ("Could not select database");
  9.  
  10. // Selecting form the database
  11.  
  12. $query = mysql_query ("SELECT Company FROM Order_Form" ) or die("<center>There was an error and we could not load the Client list</center>"); 
  13.  
  14. //echo $query;
  15. $result = mysql_query ($query, $link);
  16.  
  17. echo "<pre>$query</pre>";
  18.  
  19. # Check result
  20. if($result) {
  21.   # Print all rows
  22.   $rowno = 0;
  23.   while($row = mysql_fetch_assoc($result)  {
  24.     $rowno++;
  25.     echo "Row $rowno:<br />"
  26.     foreach($row as $_k => $_v) {
  27.       echo " - $_k = $_v<br />";
  28.     }
  29.   }
  30. else {
  31.   # Print error
  32.   echo "<b>Query failed:</b> ". mysql_error();
  33. }
  34. echo "</pre>";
PHP Parse error: parse error, unexpected '{' in /htdocs/forms/orderform2.php on line 23

I changed the the line number to the correct one it is given me an error on. I am going over some of the lessons I took to see if I can figure it out, but any help would be much appreciated...

Modern Merlin
Nov 3 '07 #5
post
17 New Member
[php]
<?php

include "config.php";

//connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass) or die("Could not connect to MySQL");

//select the database
mysql_select_db($database) or die ("Could not select database");

// Selecting form the database
$query = mysql_query ("SELECT Company FROM Order_Form" ) or die("<center>There was an error and we could not load the Client list</center>");
// Put results in array
$result = mysql_fetch_array($query);

if ($result) {
$rowno = 0;

echo("<select name=\"comapny_list\" size=\"1\" id=\"company_list\">");

while ($row = mysql_fetch_assoc($query)) {

$rowno++;

foreach($row as $_k=>$_v) {

echo("<option value=\"" .$_v. "\">" .$_v. "");

}

}
echo("</select>");
}
else {
# Print error
echo "<b>Query failed:</b> ". mysql_error();
}
?>
[/php]

You forgot to add a ) at line 27.

But I fixed your code to do what I think you were asking take a look at it.
Nov 3 '07 #6
Modern Merlin
12 New Member
WOW that worked perfectly! THANK YOU!!!!

Now my question is, is there a way to select company as a field in one table if a field in that same table is active or in active? I know in MySQL I can select * from the table that are active by doing the statement: SELECT * FROM `Order_Form` WHERE `Active` = '1' This will pull up all active records.

What I am ultimately trying to do is to fill one drop down box with all the companies that are active and within that if the same company but different department places an order I need to pull up their record information. So I need a second drop down box to change the the dept if there is one. Then the text fields below will be filled in as per both of those selections. Then the rest of the form is filled up and written back to the database (which that part I have qworking fine.

Im thinking at this time I may need to create more than one table.. :(

Still working on this myself to see if I can figure it out but any nudge in the right direction would be awesome!

Modern Merlin
Nov 5 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, Merlin.

So what you're saying is that you have a table of companies and a table of departments, and you want the values in the second drop down to be all departments for whatever company is selected in the first drop down. Is that right?
Nov 5 '07 #8
Modern Merlin
12 New Member
No I have one table.. With fields..

Name
Company
Dept
Phone
Then some choices made by checkboxes

Which the customer will fill out. Then the sales associate will pull up another page that will have a drop down box for companies (which now populates) and some companies will have different departments that order from them. So I will also need a dept drop down the populates as per the company chosen. Once that is done the text boxes below with name company dept, phone plus the checkboxes already marked by the customer will populate as per the company and dept fields above and then the sales associate will be able to choose more check boxes and text fields and that will write back to the database (which I need to over-write what is already in there so that it doesnt create another row in the table but replaces whats already in there).

I already have the part for writing back to the datbase figured out for the customer portion so the table will populate. I can now pull the company names into the drop down box and if I want to list ALL the departments I can list those too. Whichever way I do it, the text fields and and the check boxes have to populate by the company name and dept chosen. If there isnt a dept then the dept field is listed as none, which Im not so sure is a good idea.

Should I have a seperate table to house the departments? I have no problems starting this over because as I explained in the beginning I am new to this and havent really touched any programming in almost 4+ years LOL. Believe me I have no problem admitting when I need help :)
Nov 5 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya, Merlin.

How does your script know that there are multiple departments for a given company? Does it pull this info from a separate table, or does it search for all departments in the one table that match the selected company name?
Nov 6 '07 #10
Modern Merlin
12 New Member
well currently it pulls all the companies and then all the dept and then the user selects the correct one as per the client. Then it searches through the table for all companies that match each one. So there wont be more than one.
Nov 6 '07 #11
Modern Merlin
12 New Member
So I solved my own issue going by a much more simple method. I rebuilt the db to house clients and orders and just used the one constant between the two which is the order_id. Thank you all fo rhelping me!

Modern Merlin
~been up for 40 hours now... time for some shut eye...
Nov 7 '07 #12
pbmods
5,821 Recognized Expert Expert
Heya, Merlin.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Nov 7 '07 #13

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

Similar topics

0
3466
by: Chris Sharman | last post by:
I'd like to design my pages to work cooperatively with browser autofill features. I've loked around, but can't find any good documentation on supported/unsupported field names...
1
18515
by: shortbackandsides.no | last post by:
I'm having a lot of difficulty trying to persuade the Google toolbar autofill to act consistently, for example ======================= <html><head> <title>autofill test</title> </head><body>...
0
4247
by: David Portabella | last post by:
Hello, I am a doing a survey about Autofill Web Forms softwares. Usually, they all collect information of the user once during the set-up phase (user knowledge base). Then, when the user...
0
2289
by: Ray Holtz | last post by:
Is it possible to autofill a field based on what is entered into another field in a form? My form has an employee field, and department field. In an Items Table, I have fields FldEmployee, and...
4
15828
by: HTS | last post by:
I have written a membership database application (PHP + mySQL) and need to prevent autofill from filling in fields in a member record edit form. I know I can turn off autoFill in my browsers, I...
6
9364
by: bloukopkoggelmander | last post by:
All I am new to this site and hopefully would have a good time here. I am new to Microsoft Access and VBA and am currently involved in a project to create a new database for one of our company...
3
1384
by: StuckAndNoClue | last post by:
I've searched through some of the answers on the forum regarding autofill but I can't work out how to do it for my database. I have two tables 1. Members info, containing the following fields: ...
8
2358
by: tess | last post by:
I have: table 1 - tblLeadInfo which includes a salesman ID field table 2 - tbllkpSalesman with all zips in the state and a Salesman assigned to that area. I have a form based on table #1 When...
13
6205
by: BASSPU03 | last post by:
Hello, folks. This is my first post and I only began to work extensively with Access about 3 weeks ago. I'm running Access 2003 on Windows XP. I'd like a textbox in subform2 to reflect the value...
1
3142
by: Nik Coughlin | last post by:
I'm doing some ajax validation on form fields, using the form change event. If I click Autofill in the Google Toolbar, fields are filled out but the change event is never fired, so the validation...
0
7229
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
7398
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
7502
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...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.