Need assistance with selecting and displaying data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rickou812
    New Member
    • Oct 2006
    • 7

    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.cashsurveys now.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
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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(tr im($_POST['company']));
    $conn = mysql_connect(S QL_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($sq l)
    or die("Select error: ". mysql_error());
    if (mysql_num_rows ($res) > 0) {
    $row = mysql_fetch_ass oc($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:re d;'>No results found, try again</p>";
    }
    echo '<form name="compSel" action="'.$_SER VER['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:

    Comment

    • raji20
      New Member
      • Aug 2006
      • 28

      #3
      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(tr im($_POST['company']));
      $conn = mysql_connect(S QL_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($sq l)
      or die("Select error: ". mysql_error());
      if (mysql_num_rows ($res) > 0) {
      $row = mysql_fetch_ass oc($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:re d;'>No results found, try again</p>";
      }
      echo '<form name="compSel" action="'.$_SER VER['PHP_SELF'].'" method="post">' ;
      $sql = mysql_query("se lect * from tableName") or die (mysql_error()) ;
      echo "<select name='company'> ";
      while($res = mysql_fetch_arr ay($sql)){
      echo "<option value=".$res["companyNam e"]."";
      if ($res["companyNam e"] == $_POST["company"])
      echo " selected ";
      echo ">".$res["companyNam e"]."</option>";
      }
      echo "</select>";
      echo "<input type='submit' value='Get Info' />";
      echo "</form>";
      ?>[/php]

      Comment

      • rickou812
        New Member
        • Oct 2006
        • 7

        #4
        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_surve ydb"
        $tablename = "surveycomp any"
        $row = "compname
        Sorry for being such a idiot..but I am learning. :)

        Comment

        • rickou812
          New Member
          • Oct 2006
          • 7

          #5
          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
          Code:
          $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...

          Comment

          • rickou812
            New Member
            • Oct 2006
            • 7

            #6
            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(tr im($_POST['compname']));
            $conn = mysql_connect(" localhost", "XXXXX", "XXXXX")
            or die("Connect error: " . $msg_no_connect );
            mysql_select_db (rickou81_surve ydb)
            or die("Select db error: " . mysql_error());
            $sql = "SELECT * from surveycompany WHERE company = '$company' LIMIT 1";
            $res = mysql_query($sq l)
            or die("Select error: ". mysql_error());
            if (mysql_num_rows ($res) > 0) {
            $row = mysql_fetch_ass oc($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:re d;'>No results found, try again</p>";
            }
            echo '<form name="compSel" action="'.$_SER VER['PHP_SELF'].'" method="post">' ;
            $sql = mysql_query("se lect * from surveycompany", $conn) or die (mysql_error()) ;
            echo "<select name='compname' >";
            while($res = mysql_fetch_arr ay($sql)){
            echo "<option value=".$res["companyNam e"]."";
            if ($res["companyNam e"] == $_POST["company"])
            echo " selected ";
            echo ">".$res["companyNam e"]."</option>";
            }
            echo "</select>";
            echo "<input type='submit' value='Get Info' />";
            echo "</form>";
            ?> [/php]

            Comment

            • rickou812
              New Member
              • Oct 2006
              • 7

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

              Comment

              • raji20
                New Member
                • Aug 2006
                • 28

                #8
                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_surv eydb") or die("Select db error: " . mysql_error());
                if (isset($_POST['compname'])) {
                $company = htmlentities(tr im($_POST['compname']));
                $sql = "SELECT * from surveycompany WHERE company = '$company' LIMIT 1";
                $res = mysql_query($sq l)
                or die("Select error: ". mysql_error());
                if (mysql_num_rows ($res) > 0) {
                $row = mysql_fetch_ass oc($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:re d;'>No results found, try again</p>";
                }
                echo '<form name="compSel" action="'.$_SER VER['PHP_SELF'].'" method="post">' ;
                $sql = mysql_query("se lect * from surveycompany") or die (mysql_error()) ;
                echo "<select name='compname' >";
                while($res = mysql_fetch_arr ay($sql)){
                echo "<option value=".$res["companyNam e"]."";
                if ($res["companyNam e"] == $_POST["company"])
                echo " selected ";
                echo ">".$res["companyNam e"]."</option>";
                }
                echo "</select>";
                echo "<input type='submit' value='Get Info' />";
                echo "</form>";
                ?>

                Comment

                • rickou812
                  New Member
                  • Oct 2006
                  • 7

                  #9
                  Ok that took away all the errors and I think the script is working..but I am not getting the data from the database.

                  Comment

                  • rickou812
                    New Member
                    • Oct 2006
                    • 7

                    #10
                    what should be in the 'compname' spot
                    of this line :f (isset($_POST['compname'])) {
                    I put my table row name, but.....

                    Comment

                    Working...