473,224 Members | 1,441 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,224 software developers and data experts.

Need assistance with selecting and displaying data

What I am attempting to do is create a form field in which a company name can be selecting from a drop down box. When selecting I want to display the information from my database about the selected company.

I am releatively new to php and mysql, but I have learned how to create, input, and display data, but can not seem to figure out how to do the above.
I have a page at http://www.cashsurveysnow.com/company.php
with examples of a drop down list and a tabled list.
The second part of my question is how do I develope the backend script to fetch the data for the selected company form my data base?
Thanks
Oct 26 '06 #1
9 1707
ronverdonk
4,258 Expert 4TB
I am in a hurry, so you'll have to do with non tested code (MySQL part).
This is all you want in one form. You can continue on this frame and don't forget to fill in the database thing (host, user, psw, dbname, tablename, tablefields,). Try it out and we'll see what questions arise.
[php]<?php
$companyList = array ('Company A', 'Company B','Company C');

if (isset($_POST['company'])) {
$company = htmlentities(trim($_POST['company']));
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die("Connect error: " . $msg_no_connect);
mysql_select_db(SQL_DB)
or die("Select db error: " . mysql_error());
$sql = "SELECT * from company_table WHERE company = '$company' LIMIT 1";
$res = mysql_query($sql)
or die("Select error: ". mysql_error());
if (mysql_num_rows($res) > 0) {
$row = mysql_fetch_assoc($res);
echo "The selected company has the following info:<br>".
"<table>".
"<tr>Name</td><td>{$row['name']}</td></tr>".
"<tr>Background</td><td>{$row['background']}</td></tr>".
"</table>";
// whatever else you want to process
}
}
else {
echo "<p style='color:red;'>No results found, try again</p>";
}
echo '<form name="compSel" action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo "<select name='company'>";
foreach ($companyList as $comp) {
echo "<option value='$comp'";
if ($company == $comp)
echo " selected ";
echo ">$comp</option>";
}
echo "</select>";
echo "<input type='submit' value='Get Info' />";
echo "</form>";
?>[/php]

Ronald :cool:
Oct 26 '06 #2
raji20
28
I have modified the Ronald code in such a way that the company name comes from the database.here is the code, get back to me in case of any issues.

RAJI20: You should know better then displaying code without the tags! Before you do this the next time read the Posting Guidelines!
[php]
<?
if (isset($_POST['company'])) {
$company = htmlentities(trim($_POST['company']));
$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die("Connect error: " . $msg_no_connect);
mysql_select_db(SQL_DB)
or die("Select db error: " . mysql_error());
$sql = "SELECT * from company_table WHERE company = '$company' LIMIT 1";
$res = mysql_query($sql)
or die("Select error: ". mysql_error());
if (mysql_num_rows($res) > 0) {
$row = mysql_fetch_assoc($res);
echo "The selected company has the following info:<br>".
"<table>".
"<tr>Name</td><td>{$row['name']}</td></tr>".
"<tr>Background</td><td>{$row['background']}</td></tr>".
"</table>";
// whatever else you want to process
}
}
else {
echo "<p style='color:red;'>No results found, try again</p>";
}
echo '<form name="compSel" action="'.$_SERVER['PHP_SELF'].'" method="post">';
$sql = mysql_query("select * from tableName") or die (mysql_error());
echo "<select name='company'>";
while($res = mysql_fetch_array($sql)){
echo "<option value=".$res["companyName"]."";
if ($res["companyName"] == $_POST["company"])
echo " selected ";
echo ">".$res["companyName"]."</option>";
}
echo "</select>";
echo "<input type='submit' value='Get Info' />";
echo "</form>";
?>[/php]
Oct 26 '06 #3
Thanks for your help..But I guess I need it dumbed down a little for me...lol
What parts of the code do I need to change to reflect my table name.

MY datebase: rick_surveydb
The table name: surveycompany
the company name row: compname
Where I am getting hung up is where the table name and row name fall in at.
Can you change the code to use variable there like
$db_name = "rick_surveydb"
$tablename = "surveycompany"
$row = "compname
Sorry for being such a idiot..but I am learning. :)
Oct 26 '06 #4
I still have not got this figured out. I have tried, but I am just not getting something here.
I have played with both scripts and get the "nothing found" error with the first, which I know is my fault, because I am not putting the tablenames in the right places.
The secong script I am getting a connection error on line 25
Expand|Select|Wrap|Line Numbers
  1. $sql    =    mysql_query("select * from tableName") or die (mysql_error());
Please help, thanks

I think once I overcome this hurdle I may be on my way...
Oct 27 '06 #5
I fixed my connect problem on line 25, but now it states not a valid link on line 25, but like I mentioned before, I know I have the tablename and row names in the wrong places, here is a complete copy of the code with my modifications:
[php]<?
if (isset($_POST['compname'])) {
$company = htmlentities(trim($_POST['compname']));
$conn = mysql_connect("localhost", "XXXXX", "XXXXX")
or die("Connect error: " . $msg_no_connect);
mysql_select_db(rickou81_surveydb)
or die("Select db error: " . mysql_error());
$sql = "SELECT * from surveycompany WHERE company = '$company' LIMIT 1";
$res = mysql_query($sql)
or die("Select error: ". mysql_error());
if (mysql_num_rows($res) > 0) {
$row = mysql_fetch_assoc($res);
echo "The selected company has the following info:<br>".
"<table>".
"<tr>Name</td><td>{$row['compname']}</td></tr>".
"<tr>Background</td><td>{$row['description']}</td></tr>".
"</table>";
// whatever else you want to process
}
}
else {
echo "<p style='color:red;'>No results found, try again</p>";
}
echo '<form name="compSel" action="'.$_SERVER['PHP_SELF'].'" method="post">';
$sql = mysql_query("select * from surveycompany", $conn) or die (mysql_error());
echo "<select name='compname'>";
while($res = mysql_fetch_array($sql)){
echo "<option value=".$res["companyName"]."";
if ($res["companyName"] == $_POST["company"])
echo " selected ";
echo ">".$res["companyName"]."</option>";
}
echo "</select>";
echo "<input type='submit' value='Get Info' />";
echo "</form>";
?> [/php]
Oct 27 '06 #6
this is the error:
mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/rickou81/public_html/test2.php on line 25
Oct 27 '06 #7
raji20
28
Try this, the problem is you have established the db connection inside the if condition, so its pops up the db error, now i have made it availabe in the top, and also there is no need to pass the connection string in the mysql_query.
<?
$conn = mysql_connect("localhost", "XXXXX", "XXXXX") or die("Connect error: " . $msg_no_connect);
mysql_select_db("rickou81_surveydb") or die("Select db error: " . mysql_error());
if (isset($_POST['compname'])) {
$company = htmlentities(trim($_POST['compname']));
$sql = "SELECT * from surveycompany WHERE company = '$company' LIMIT 1";
$res = mysql_query($sql)
or die("Select error: ". mysql_error());
if (mysql_num_rows($res) > 0) {
$row = mysql_fetch_assoc($res);
echo "The selected company has the following info:<br>".
"<table>".
"<tr>Name</td><td>{$row['compname']}</td></tr>".
"<tr>Background</td><td>{$row['description']}</td></tr>".
"</table>";
// whatever else you want to process
}
}
else {
echo "<p style='color:red;'>No results found, try again</p>";
}
echo '<form name="compSel" action="'.$_SERVER['PHP_SELF'].'" method="post">';
$sql = mysql_query("select * from surveycompany") or die (mysql_error());
echo "<select name='compname'>";
while($res = mysql_fetch_array($sql)){
echo "<option value=".$res["companyName"]."";
if ($res["companyName"] == $_POST["company"])
echo " selected ";
echo ">".$res["companyName"]."</option>";
}
echo "</select>";
echo "<input type='submit' value='Get Info' />";
echo "</form>";
?>
Oct 27 '06 #8
Ok that took away all the errors and I think the script is working..but I am not getting the data from the database.
Oct 27 '06 #9
what should be in the 'compname' spot
of this line :f (isset($_POST['compname'])) {
I put my table row name, but.....
Oct 27 '06 #10

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

Similar topics

11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
5
by: Bill | last post by:
Good Day; I would appreciate assistance developing a query that I haven't been able to develop without using a second table. I wish to count the number of records that are still open on the...
2
by: yvan | last post by:
I was wondering if someone here could help me with modifying this PHP template so that it does something very specific for me. My knowledge of PHP is very limited (I'm more of a Cold Fusion...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
2
by: Mike Kelly | last post by:
Hi. I have a data table where rows are grouped according to a certain criteria and I want to be able to display all the rows that belong to the same group together on the screen. In addition, I...
5
by: Lucky | last post by:
hi guys, i'm looking for a RegEx which can find these type of string from the bunch of html lines. if any one can help me here, would be appriciated. <a...
2
by: yogeshtiwarijbp | last post by:
when i select data from the drop down list box and display data in the datagrid the first data in the dropdownlistbox not shown the requireddata in the grid. i have used 2 tables and after selecting...
6
by: martin DH | last post by:
**Urgent Need** I'll throw out the basics and any assistance is very, very, very much appreciated! Access 2003 on XP On a form (frmMain) is an option group of check boxes (ReportFrame) from...
1
by: anuragvatsa | last post by:
Hi, I have 4 columns workID, Description, work & Rework . On the basis these four columns i have to generate four more columns as total Work,%work, %Rework, %Total Work. we have some dummy data. So...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.