Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Php Select Results from Menu

Options
  • 26-04-2005 7:17pm
    #1
    Registered Users Posts: 884 ✭✭✭


    Hi there, i have what probably is a very basic question ....

    I have a drop down menu and it is populated from a SELECT statement and the user clicks on Submit .... i want to use the option that the user picks to run another SELECT and show the results on another page !!

    How can i run the query based on that ..... i dont know how to carry out the select and format it into columns etc. and send this all to another page. I want to use the other page for loads of different types of searches ... all with very different results.

    Here is my code so far .....

    
    <html>
    
    <body>
    
    <?php
    
    $username = "username";
    $password = "password";
    $hostname = "IP address or localhost";	
    $dbh = mysql_connect($hostname, $username, $password) 
    	or die("Unable to connect to MySQL");
    print "Connected to MySQL<br>";
    $selected = mysql_select_db("databasename",$dbh) 
    	or die("Could not select my database");
    print "Connected to database<br>";
    
    
    function generate_box() { 
    
        $sql = "SELECT ClubNo, ClubName FROM club"; 
        $result = mysql_query($sql) or die(mysql_error());
        $entries = mysql_num_rows($result); 
        $options = '<option value="-1"></option>'; 
    
        while($row = mysql_fetch_assoc($result)) { 
                $options .= "<option value=\"{$row['ClubNo']}\">{$row['ClubName']}</option>"; 
         } 
    
        return $options; 
    
    } 
    
    $options = generate_box();
    
    ?>
    
    
    <table>
    <form method="get" name="forum_select" action="search.php"><table cellspacing="0" cellpadding="0" border="0">
    
    	<tr>
    
                    <td><span>Search By: 
    
    <select name="by">
    
    <? echo "$options"; ?>
    
    <input type="submit" value="Submit"></span></td>
    
    	</tr>
    
    </table></form>
    
    </body>
    
    </html>
    
    


Comments

  • Registered Users Posts: 1,268 ✭✭✭hostyle


    if ($_REQUEST['submit'] == 'submit') {
      do_something_with($_REQUEST['by']);
    }
    


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    hostyle wrote:
    if ($_REQUEST['submit'] == 'submit') {
      do_something_with($_REQUEST['by']);
    }
    

    Could i do something like this ??
    if ($_REQUEST['submit'] == 'submit') {
    [COLOR=DarkSlateBlue]mysql_query("SELECT ClubName, DomesticLeague, TelePhoneNo, FaxNo, EmailAddress, Website, Groundno, YearEstablished, Status, ManagerNo, CoachNo FROM club");
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    print "Club Name: ".$row{'ClubName'}."<br> League: ".$row{'DomesticLeague'}."<br> Telephone: ".$row{'TelePhoneNo'}."<br> Fax No.: ".$row{'FaxNo'}."<br> Email: ".$row{'EmailAddress'}."<br> Website: ".$row{'Website'}."<br> GroundNo: ".$row{'GroundNo'}."<br> Established: ".$row{'YearEstablished'}."<br> Status: ".$row{'Status'}."<br> ManagerNo: ".$row{'ManagerNo'}."<br> CoachNo: ".$row{'CoachNo'}."<br>"; 
    }([/COLOR]$_REQUEST['by']);
    }
    


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    I am getting an error ...

    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in \\fullpath\club.php on line 70

    Here is the code ....
    <?Php
    
    [B]if ($_REQUEST['submit'] == 'submit') {[/B]
    $clubID = $_POST['Clubno']; 
    $result=mysql_query("SELECT ClubName, DomesticLeague, TelePhoneNo, FaxNo, EmailAddress, Website, Groundno, YearEstablished, Status, ManagerNo, CoachNo FROM club WHERE Clubno=$clubID"); 
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    echo "Club Name: $row['ClubName'] <br> League: $row['DomesticLeague'] <br> Telephone: $row['TelePhoneNo'] <br> Fax No.: $row['FaxNo'] <br> Email: $row['EmailAddress'] <br> Website: $row['Website']<br> GroundNo: $row['GroundNo'] <br> Established: $row['YearEstablished'] <br> Status: $row['Status'] <br> ManagerNo: $row['ManagerNo'] <br> CoachNo: $row['CoachNo'] <br>"; 
    }($_REQUEST['by']);
    }
    
    ?>
    


    The IF statement above is line 70

    Any one got any ideas ??


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    Have anyone got any ideas ..... i cant figure this one out ..... it is expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' but i cant understand why ??


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Remove this line:
    }($_REQUEST['by']);
    


  • Advertisement
  • Registered Users Posts: 884 ✭✭✭Cork Skate


    hostyle wrote:
    Remove this line:
    }($_REQUEST['by']);
    


    I just tried it but still the same ...... there could be a problem with that but i dont think there is right now, the error is on line 70 (so it is either that line or before it, never after) ....... then again, i have been known to be wrong :)


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    When you get errors break it down into smaller pieces:
    <?php
    
    if ($_REQUEST['submit'] == 'submit') {
    	$clubID = $_POST['Clubno']; 
    	$result=mysql_query("SELECT ClubName, DomesticLeague, TelePhoneNo, FaxNo, EmailAddress, Website, Groundno, YearEstablished, Status, ManagerNo, CoachNo FROM club WHERE Clubno=$clubID"); 
    	while ($row = mysql_fetch_array($result)) {
    		echo "Club Name: " . $row['ClubName'] . "<br> League: " . $row['DomesticLeague'] . "<br>";
    //		echo "Telephone: $row['TelePhoneNo'] <br> Fax No.: $row['FaxNo'] <br>";
    //		echo "Email: $row['EmailAddress'] <br> Website: $row['Website']<br>";
    //		echo "GroundNo: $row['GroundNo'] <br> Established: $row['YearEstablished'] <br>";
    //		echo "Status: $row['Status'] <br> ManagerNo: $row['ManagerNo'] <br> CoachNo: $row['CoachNo'] <br>"; 
    	}
    }
    
    ?>
    
    

    Change the rest of the lines to match.


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    I have tried this ......

    <?Php
    
    if ($_REQUEST['submit'] == 'Submit') {
    $clubID = $_POST['Clubno']; 
    $result=mysql_query("SELECT ClubName, DomesticLeague, TelePhoneNo, FaxNo, EmailAddress, Website, Groundno, YearEstablished, Status, ManagerNo, CoachNo FROM club WHERE Clubno=$clubID"); 
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    echo "Club Name: {$row['ClubName']} <br> League: {$row['DomesticLeague']} <br> Telephone: {$row['TelePhoneNo']} <br> Fax No.: {$row['FaxNo']} <br> Email: {$row['EmailAddress']} <br> Website: {$row['Website']} <br> GroundNo: {$row['GroundNo']} <br> Established: {$row['YearEstablished']} <br> Status: {$row['Status']} <br> ManagerNo: {$row['ManagerNo']} <br> CoachNo: {$row['CoachNo']} <br>"; 
    }
    }
    
    ?>
    

    And it works .... well .... it doesn't give an error ... but it doesn't show the result of the query.

    The form tag is <form method="post" name="forum_select" action="result.php">

    And it brings me to that but it is meant to display the results on that page .... but it aint !!

    Any ideas ??


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    PHP is case sensitive.
    if ($_REQUEST['submit'] == '[u]S[/u]ubmit') {
    

    Is your submit buttons value submit or Submit?


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    hostyle wrote:
    PHP is case sensitive.
    if ($_REQUEST['submit'] == '[u]S[/u]ubmit') {
    

    Is your submit buttons value submit or Submit?


    <input type="submit" value="Submit">

    I doubt it is that .... i get to result.php alright so it is working ... it is just not bringing the results with it :)

    I dont know if anything has to go onto result.php to indicate which cell the data will appear ... i tried something like this ....

    <?php echo $_POST; ?>

    But that is not giving me anything ?? It just prints the word 'array', i tried it on another page and the same so it does nothing.

    I dont know what should go in there to be honest !!


  • Advertisement
  • Registered Users Posts: 1,268 ✭✭✭hostyle


    Honestly this is going nowhere.

    1. What are you trying to do?
    2. Post your code somewhere (if its over 100 lines link to it externally - if less post it here)
    3. Look up some PHP tutorials. This is very, very basic stuff. The reason I've been giving short answers is to try and give you hints rather than do it for you.


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    hostyle wrote:
    Honestly this is going nowhere.

    Tell me about it !! :)
    3. Look up some PHP tutorials. This is very, very basic stuff. The reason I've been giving short answers is to try and give you hints rather than do it for you.

    I have .... i have tried to avoid asking question, searching through previous posts, looking for similar. I haven't found a tutorial that really covers what i need and i wouldn't mind but i would have thought that it is not that convoluted really. ITs nuts and bolts stuff.

    I know its basic, but i am a total newbie .... i am struggling to be honest but what can i do, i need to get this done.


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    I've PMed you my email address. Send on the code you have so far and I'll get the basics working for you.


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    Cork Skate wrote:
    I know its basic, but i am a total newbie .... i am struggling to be honest but what can i do, i need to get this done.


    searching in past posts here isnt going to help you.

    You need to goto places like


    www.php.net
    www.phpbuilder.com/board/search.php


    and other places that specilise in php, and its tutorials


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    Problem solved !!

    Just wanna post up the correct code ......

    This is to populate the drop down menu ....
    
    <?php
    $username = "username";
    $password = "ipassword";
    $hostname = "localhost";	
    $dbh = mysql_connect($hostname, $username, $password) 
    	or die("Unable to connect to MySQL");
    $selected = mysql_select_db("database",$dbh) 
    	or die("Could not select my database");
    
    function generate_box() { 
        $sql = "SELECT ClubNo, ClubName FROM club"; 
        $result = mysql_query($sql) or die(mysql_error());
        $entries = mysql_num_rows($result); 
        $options = '<option value="-1"></option>'; 
        while($row = mysql_fetch_assoc($result)) { 
                $options .= "<option value=\"{$row['ClubNo']}\">{$row['ClubName']}</option>"; 
         } 
        return $options; 
    } 
    
    $options = generate_box();
    
    ?>
    
    

    And this is the form ...... and it brings you to result.php
    
    <form method="post" name="forum_select" action="result.php"> <table cellspacing="0" cellpadding="0" border="0">
    	<tr><td border="1"><span>Select a club from the drop down list: <br>
    
    <select name="by">
    
    <? echo "$options"; ?>
    
    </select>
    
    <input type="submit" name="submit "value="Submit"></span>
    
    

    And this is the code on result.php ..... in the cell where everything appears.
    
    <?php
    $username = "username";
    $password = "ipassword";
    $hostname = "localhost";		
    $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
    $selected = mysql_select_db("database",$dbh) or die("Could not select my database");
    $clubID = $_POST['by']; 
    $query = ("SELECT ClubName, DomesticLeague, TelePhoneNo, FaxNo, EmailAddress, Website, Groundno, YearEstablished, Status, 
    
    ManagerNo, CoachNo FROM club WHERE ClubNo=$clubID");
    $result = mysql_query($query) or die(mysql_error()); 
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    extract($row);
    echo "Club Name: $row[ClubName] <br> League: $row[DomesticLeague] <br> Telephone: $row[TelePhoneNo] <br> Fax No.: $row[FaxNo] <br> Email: $row[EmailAddress] <br> Website: $row[Website] <br> GroundNo: $row[GroundNo] <br> Established: $row[YearEstablished] <br> Status: $row[Status] <br> ManagerNo: $row[ManagerNo] <br> CoachNo: $row[CoachNo] <br>"; 
    }
    
    ?>
    
    


    Hope this helps someone else !!


  • Registered Users Posts: 1,268 ✭✭✭hostyle


    You should change the last one to make it secure from QUERY_STRING poisoning
    
    <?php
    $username = "username";
    $password = "ipassword";
    $hostname = "localhost";		
    $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
    $selected = mysql_select_db("database",$dbh) or die("Could not select my database");
    
    [b]if ($_POST['by'] == intval($_POST['by'])) {
      $clubID = $_POST['by'];
    }
    
    if ($clubID) {[/b]
    $query = ("SELECT ClubName, DomesticLeague, TelePhoneNo, FaxNo, EmailAddress, Website, Groundno, YearEstablished, Status, 
    
    ManagerNo, CoachNo FROM club WHERE ClubNo=$clubID");
    $result = mysql_query($query) or die(mysql_error()); 
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    extract($row);
    echo "Club Name: $row[ClubName] <br> League: $row[DomesticLeague] <br> Telephone: $row[TelePhoneNo] <br> Fax No.: $row[FaxNo] <br> Email: $row[EmailAddress] <br> Website: $row[Website] <br> GroundNo: $row[GroundNo] <br> Established: $row[YearEstablished] <br> Status: $row[Status] <br> ManagerNo: $row[ManagerNo] <br> CoachNo: $row[CoachNo] <br>"; 
    }
    [b]} else {
      echo "<p>Invalid selection</p>";
    }[/b]
    ?>
    
    


Advertisement