473,387 Members | 1,515 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,387 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 1716
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.