468,290 Members | 2,024 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

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 1425
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

Post your reply

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

Similar topics

5 posts views Thread by Bill | last post: by
2 posts views Thread by yvan | last post: by
2 posts views Thread by Mike Kelly | last post: by
5 posts views Thread by Lucky | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.