Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using Value from Dropbox to retrieve data from database table

    I got two pages with the following code (database is local):

    page 1:

    <html>
    <head></head>
    <body>
    <p>
    Select State:
    <form action="sales_rep_result.php" method="get">
    <select name="varname">
    <option value="select">Select...</option>
    <option value="Alabama">Alabama</option>
    <option value="Alaska">Alaska</option>
    <option value="Arizona">Arizona</option>
    <option value="Arkansas">Arkansas</option>
    <option value="California Northern">California Northern</option>
    <option value="California Southern">California Southern</option>
    <option value="Canada Eastern">Canada Eastern</option>
    <option value="Canada Western">Canada Western</option>
    <option value="Colorado">Colorado</option>
    <option value="Connecticut">Connecticut</option>
    <option value="Delaware">Delaware</option>
    <option value="Florida">Florida</option>
    <option value="Florida Panhandle">Florida Panhandle</option>
    <option value="Georgia">Georgia</option>
    <option value="Georgia Savannah">Georgia Savannah</option>
    <option value="Hawaii">Hawaii</option>
    <option value="Idaho">Idaho</option>
    <option value="Illinois Northern">Illinois Northern</option>
    <option value="Illinois Southern">Illinois Southern</option>
    <option value="Indiana">Indiana</option>
    <option value="Indiana Evansville">Indiana Evansville</option>
    <option value="Iowa">Iowa</option>
    <option value="Iowa Davenport">Iowa Davenport</option>
    <option value="Kansas">Kansas</option>
    <option value="Kentucky">Kentucky</option>
    <option value="Kentucky Northern">Kentucky Northern</option>
    <option value="Louisiana">Louisiana</option>
    <option value="Maine">Maine</option>
    <option value="Maryland">Maryland</option>
    <option value="Massachusetts Eastern">Massachusetts Eastern</option>
    <option value="Massachusetts">Massachusetts</option>
    <option value="Michigan">Michigan</option>
    <option value="Minnesota">Minnesota</option>
    <option value="Mississippi">Mississippi</option>
    <option value="Missouri">Missouri</option>
    <option value="Missouri St Louis">Missouri St Louis</option>
    <option value="Montanna">Montanna</option>
    <option value="Nebraska">Nebraska</option>
    <option value="Nevada Southern">Nevada Southern</option>
    <option value="Nevada northern">Nevada northern</option>
    <option value="New Hampshire">New Hampshire</option>
    <option value="New Jersey">New Jersey</option>
    <option value="New Mexico">New Mexico</option>
    <option value="New York">New York</option>
    <option value="North Carolina">North Carolina</option>
    <option value="North Dakota">North Dakota</option>
    <option value="Ohio">Ohio</option>
    <option value="Ohio Toledo">Ohio Toledo</option>
    <option value="Oklahoma">Oklahoma</option>
    <option value="Oregon">Oregon</option>
    <option value="Pennsylvania Eastern - Central">Pennsylvania Eastern - Central</option>
    <option value="Pennsylvania Western">Pennsylvania Western</option>
    <option value="Rhode Island">Rhode Island</option>
    <option value="South Carolina">South Carolina</option>
    <option value="South Dakota">South Dakota</option>
    <option value="Tennessee">Tennessee</option>
    <option value="Tennessee Tri-Cities">Tennessee Tri-Cities</option>
    <option value="Texas">Texas</option>
    <option value="Texas El Paso">Texas El Paso</option>
    <option value="Utah">Utah</option>
    <option value="Vermont">Vermont</option>
    <option value="Virginia Northern">Virginia Northern</option>
    <option value="Virginia Southern">Virginia Southern</option>
    <option value="Washington">Washington</option>
    <option value="Washington,DC">Washington,DC</option>
    <option value="West Virginia">West Virginia</option>
    <option value="Wisconsin">Wisconsin</option>
    <option value="Wyoming">Wyoming</option>
    </select>
    <input type="submit" />

    </form>
    </p>
    </body>
    </html>

    /////////////////////////////////////

    Page 2 code:

    <?php
    //if(isset($_GET['Selected'])){

    //$selectState = $_GET['Selected'];

    $var_value = $_GET['varname'];
    echo $var_value;

    ?>

    <?php
    $username="root";
    $password="root";
    $database="sales_rep";

    mysql_connect(localhost,$username,$password);

    @mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM sales_rep WHERE state= $var_value";$result=mysql_query($query);

    $num=mysql_numrows($result);mysql_close();

    ?>

    <table border="0" cellspacing="2" cellpadding="2">
    <tr>
    <td>
    <font face="Arial, Helvetica, sans-serif">State</font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif">Sales Rep Name</font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif">Phone</font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif">Email</font>
    </td>
    </tr>
    <?php
    $i=0;while ($i < $num) {$f1=mysql_result($result,$i,"state");
    $f2=mysql_result($result,$i,"sales_rep");$f3=mysql_result($result,$i,"phone");
    $f4=mysql_result($result,$i,"email");
    ?>

    <tr>
    <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
    </td>
    <td>
    <font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font>
    </td>
    </tr>
    <?php
    $i++;}
    ?>

    ///////////////////////////////

    this line of code does not return anything:

    $query="SELECT * FROM sales_rep WHERE state= $var_value";$result=mysql_query($query);

    BUT

    when I take off the WHERE clause, it retrieve everything, but that is not what i want.

    However, the $var_value does print the selected state from previous page.

    Can someone please help?

    Thank you

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    In the future please wrap your code in [php][/php] or [code][/code] tags. It retains the formatting.
    State is a string. You'll need to identify it as such: $query="SELECT * FROM sales_rep WHERE state='$var_value'";
    Also, try to avoid using mysql_result. It is very very slow. Opt for mysql_fetch_assoc, mysql_fetch_row, or mysql_fetch_array which all return arrays that you can offset intead. Assoc and Array (although you pretty much never need the _array) will give you an associative offset, so you can simply dereference the $row['PropertyName'].

    On a side note, the mysql library is officially deprecated, and I would expect it removed in the next major release. You should use the mysqli library or the PDO libraries as a replacement.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And on a completely different note...

    The use of a value= property in an <option> is a waste of time and code *IF* the value is the same as the text.

    That is, from PHP's standpoint (and from any server-side coding's standpoint)
    Code:
    <option value="Arkansas">Arkansas</option>
    and
    Code:
    <option>Arkansas</option>
    are identical.

    You only need to use value= when you want the value to be *different* than what is shown to the user.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And also, by the by, if you had *correctly* done
    Code:
    $query="SELECT * FROM sales_rep WHERE state= $var_value" or die(mysql_error());
    you would have seen that MySQL was giving you an error from your query.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all for your input. I am new to PHP.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •