?
Solved

Php List box populated from MSsql query

Posted on 2007-07-24
8
Medium Priority
?
286 Views
Last Modified: 2013-12-12
I am trying to create a list box from a php query but I am getting an error what am I doing wrong?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Lab Support Service Request Page</title>
</head>
<body>
<?php
$dbc = mssql_connect ('cacapppro01.cup.com','nskinfo','webres');
$query = mssql_query("
SELECT     *
FROM         dbo.NSKSystemInfo
WHERE     (SystemState_ID = 1)
ORDER BY systemName");
?>
<?
echo "<select  size="10" multiple="yes" value="System_ID" display="SystemName" name=\"SYSTEM_ID\"></select>";
mssql_close($dbc);

?>
</body>
</html>
0
Comment
Question by:lhaluska
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 19558693
i think you are trying to populate select box with the DB values?
0
 
LVL 1

Author Comment

by:lhaluska
ID: 19558730
Yes, The table I want to show in the list is "SystemName" and the value I want is "System_ID".
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 19558762
just an idea


<?php
      include "canada_hotels/library/connection.php";
      
      $query  = "SELECT * FROM tblcategory";
      $result = mysql_query($query);
      ?>
      <select name='category'>
      <option></option>
      <?
      while($row = mysql_fetch_array($result, MYSQL_ASSOC))
      {
            echo "<option value='{$row['categoryID']}'";         
            echo ">{$row['categoryname']}</option>";      
      }
?>
      </select>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
Aamir Saeed earned 2000 total points
ID: 19558796
i did post that code in MYSQL as i just wanted to give you an idea. now, i would like to let you know about your errors.

1; you need to use mssql_select_db() to select DB
2; you need to use mssql_fetch_array() to get data and then loop through it and display data
3; you can't use outer and inner single and double quotes at the same time for example,

echo "<select  size="10" multiple="yes" value="System_ID" display="SystemName" name=\"SYSTEM_ID\"></select>";

whether you 'll use outer single and inner double or vice versa and if you want to use the same then you can escape them by using \ before the inner quotes.

hope this helps
0
 
LVL 1

Author Comment

by:lhaluska
ID: 19559662
So I have a couple of questions. Why is it that I need a "mssql_select_db" if I have the "$query" and the "$dbc" variable? The code is now working with your help from the example, But why am I getting a white space at the top of my list box?

<?php
$dbc = mssql_connect ('cacapppro01.cup.com','nskinfo','webres');
$query = mssql_query("
SELECT     *
FROM         dbo.NSKSystemInfo
WHERE     (SystemState_ID = 1)
ORDER BY systemName");

?>

<select  size="10" multiple="yes" value="System_ID" display="SystemName" name="SYSTEM">

      <option></option>
      <?
      while($row = mssql_fetch_array($query, MSSQL_ASSOC))
      {
            echo "<option value='{$row['System_ID']}'";        
            echo ">{$row['systemName']}</option>";      
      }
?>
      </select>


<?
mssql_close($dbc);

?>
0
 
LVL 1

Author Comment

by:lhaluska
ID: 19559688
I answered my second question in the last post it was the option above the loop. Oh I have one more question how do I make it so users can not see the php code if they click view source?
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 19563079
people can't see php code rather they can only see client side code for example HTML, CSS, JAVASCRIPT etc and PHP is server side language
0
 
LVL 1

Author Comment

by:lhaluska
ID: 19569897
oh ok thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question