8 Replies - 708 Views - Last Post: 19 October 2011 - 10:18 AM Rate Topic: -----

#1 dharitdesai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-August 09

Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 04:58 PM

<html>
<head>

</head>
<body>
<?php
echo '<form method="get" action="">';
function database_connect($select_database)
{
    $resource_link = mysql_connect('xxx', 'yyy', 'zzz') ;
    if (mysql_select_db($select_database, $resource_link)) {
        return $resource_link;
    } else {
        echo "Cannot connect to DB";
        return false;
    }
}
function print_dropdown($query, $link){
    $queried = mysql_query($query,$link);
    $menu = "<select name = Name>";
    $menu .= '<option selected>--Choose a Department--</option>';
    while ($result = mysql_fetch_array($queried)) {
        $menu .= '<option>' . $result['Name'] . '</option>';
    }
    $menu .= '</select>';
    return $menu;
}


echo print_dropdown("SELECT * FROM Dept", database_connect("yyy"));

echo '</form>';

?>


<?php

echo '<form method="get" action="">';

    function print_dropdown($query, $link){
    $queried = mysql_query("Select Name FROM Dept" , database_connect("yyy"));
    $menu = "<select name = Course_ID>";
    $menu .= '<option selected>--Choose a Course--</option>';
    while ($row = mysql_fetch_array($queried)) {
        $menu .= '<option>' . $row['Course_ID'] . '</option>';
    }
    $menu .= '</select>';
    return $menu;
}


echo print_dropdown("SELECT * FROM Course", database_connect("yyy"));

echo '</form>';

?>



</body>
</html>





I am making a course registering system. I am stuck here.
here I am trying to retrive courses from database when I select major from first dropdown.
so it suppose to do like this: if some one selects a major from department then in another dropdown courses should come of that department

Is This A Good Question/Topic? 0
  • +

Replies To: Problem stuck here : PHP /MYSQL

#2 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 04-December 08

Re: Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 05:48 PM

Hi there are you getting any errors? What are you stuck on?
Was This Post Helpful? 0
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,329
  • Joined: 15-February 11

Re: Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 06:09 PM

On line 53 your query does not specify that the Courses should be of a particular Department. You are to use a WHERE clause here.

Also read up on this tutorial
Simple Solutions: Populating Select Element Using DOM - codeprada.

It will show you how to create Select elements without having to manually join strings together.

Also check out
Content-Code Separation - CTphpnwb

This post has been edited by codeprada: 18 October 2011 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 dharitdesai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-August 09

Re: Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 06:27 PM

View Postsas1ni69, on 18 October 2011 - 06:48 PM, said:

Hi there are you getting any errors? What are you stuck on?


My first Dropdown was working perfect.

Then As per requirement of site functionality I created second dropdown.
When I load the page its giving me
Server error
The website encountered an error while retrieving site.php. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later.
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

View Postcodeprada, on 18 October 2011 - 07:09 PM, said:

On line 53 your query does not specify that the Courses should be of a particular Department. You are to use a WHERE clause here.

Also read up on this tutorial
Simple Solutions: Populating Select Element Using DOM - codeprada.

It will show you how to create Select elements without having to manually join strings together.

Also check out
Content-Code Separation - CTphpnwb



So I should do
 

echo print_dropdown("SELECT Dept.Course, Name.Dept FROM Course,Dept  WHERE Dept.course = Name. Dept ", database_connect("yyy"));

echo '</form>';

?>


??

This post has been edited by JackOfAllTrades: 19 October 2011 - 07:20 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,329
  • Joined: 15-February 11

Re: Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 06:40 PM

Yes something similar to that. However your WHERE clause must be comparing Dept.course to the value of your select element that's holding the department names.

You should set the name attribute in your select element to something more descriptive like say 'department'.
Was This Post Helpful? 0
  • +
  • -

#6 dharitdesai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-August 09

Re: Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 06:50 PM

View Postcodeprada, on 18 October 2011 - 07:40 PM, said:

Yes something similar to that. However your WHERE clause must be comparing Dept.course to the value of your select element that's holding the department names.

You should set the name attribute in your select element to something more descriptive like say 'department'.


Understood. Thanks!
I just tried both drop down functions separately. it worked fine. but when I put both functions together as in the post it did not work and gave me server error.
What can cause it?
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,329
  • Joined: 15-February 11

Re: Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 07:34 PM

Could you copy and paste the error?
Was This Post Helpful? 0
  • +
  • -

#8 dharitdesai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-August 09

Re: Problem stuck here : PHP /MYSQL

Posted 18 October 2011 - 07:38 PM

View Postcodeprada, on 18 October 2011 - 08:34 PM, said:

Could you copy and paste the error?




Server error
The website encountered an error while retrieving site.php. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later.
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
Was This Post Helpful? 0
  • +
  • -

#9 dharitdesai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-August 09

Re: Problem stuck here : PHP /MYSQL

Posted 19 October 2011 - 10:18 AM

I found my mistake.
I was calling print drop down twice so i hade to change the name of the second function
so now its



echo '<form method="get" action="">';
function database_connect($select_database)
{
    $resource_link = mysql_connect('xxx', 'yyy', 'zzz') ;
    if (mysql_select_db($select_database, $resource_link)) {
        return $resource_link;
    } else {
        echo "Cannot connect to DB";
        return false;
    }
}
function print_dropdown($query, $link){
    $queried = mysql_query($query,$link);
    $menu = "<select name = Name>";
    $menu .= '<option selected>--Choose a Department--</option>';
    while ($result = mysql_fetch_array($queried)) {
        $menu .= '<option>' . $result['Name'] . '</option>';
    }
    $menu .= '</select>';
    return $menu;
}


echo print_dropdown("SELECT * FROM Dept", database_connect("yyy"));

echo '</form>';
?>

<?php
echo '<form method="get" action="">';

    function print_second_dropdown($query, $link){
    $queried = mysql_query("SELECT Name FROM Dept" , database_connect("yyy"));
    $qry = mysql_query("SELECT Dept FROM Course", database_connect("yyy"));
    $menu = "<select name= Course_ID>";
    $menu .= '<option selected>--Choose a Course--</option>';
    while ($row = mysql_fetch_row($qry=$queried)) {
        $menu .= '<option>' . $row['Course_ID'] . '</option>';
    }
    $menu .= '</select>';
    return $menu;
}
echo print_second_dropdown("SELECT Course_ID FROM Course", database_connect("yyy"));

echo '</form>';

?>




THnaks for the help guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1