Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Drop Down Executes Query On Post?

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #170624
    MBM
    Participant

    I need to execute a query which corresponds to a number in the dropdown and the results to display once a user clicks submit, I don’t want to use javascript. What am I missing?

    <form action="" method="post" enctype="multipart/form-data" name="form1" id="genericForm">
    <fieldset>
    <p>Filter Rating</p>
    <select name="ratings">
    <option value="1">One Star</option>
    <option value="2">Two Stars</option>
    <option value="3">Three Stars</option>
    <option value="4">Four Stars</option>
    <option value="5">Five Stars</option>
    </select>
    </div>
    
    <input type="submit" name="Submit" value="Submit"><br  />
    </form>

    The php :

    <?php 
    $Link = mysql_connect($Host, $User, $Password);
    if($_POST['value'] == '1') { 
        // query to get all 1 star ratings
        $query = "SELECT * FROM films WHERE genre='action' AND rating='1'"; 
    } 
    elseif($_POST['value'] == '2') { 
        // query to get all 2 star ratings
        $query = "SELECT * FROM films WHERE genre='action' AND rating='2'"; 
    }   
    elseif($_POST['value'] == '3') { 
        // query to get all 3 star ratings
        $query = "SELECT * FROM films WHERE genre='action' AND rating='3'"; 
        }   
    elseif($_POST['value'] == '4') { 
        // query to get all 4 star ratings
        $query = "SELECT * FROM films WHERE genre='action' AND rating='4'"; 
        }   
    elseif($_POST['value'] == '5') { 
        // query to get all 5 star ratings
        $query = "SELECT * FROM films WHERE genre='action' AND rating='5'"; 
    } 
    
    WHILE($row = mysql_fetch_array($result)):
    $title[] = $row['title'];
    $studio[] = $row['studio'];
    $language[] = $row['language'];
    $certification[] = $row['certification'];
    
    echo '
    Title : '.$title.'<br />
    Studio : '.$studio.'<br />
    Language : '.$language.'<br />
    Certification : '.$certification.'<br />
    ;
    endwhile;
    ?>
    #170628
    __
    Participant

    if($_POST['value'] == '1'

    You don’t have any form fields named value. Your select box is named ratings.

    This doesn’t have anything to do with javascript, however. If this is not the problem you are trying to solve, please explain more clearly (e.g., don’t ask “what am I missing?”, but tell us what is going wrong. explain the symptoms!).

    WHILE($board = mysql_fetch_array($result)):

    while it’s not exactly wrong, I would highly recommend against using php’s alternate syntaxes. They are leftovers from much older versions of the language, and there is no advantage to using them. And they are much harder to read than regular, bracketed blocks.

    while( /* condition */ ){
        /* do code */
    }
    
    #170629
    MBM
    Participant

    Thanks for the reply. I changed the name :

    <select name="value">

    When I click submit the screen refreshes so it seems to be connecting to the database and maybe executing something but nothing is displayed.

    https://gist.github.com/gyprosetti/9e1d1465362fddb2f9f1

    What do you suggest instead of :

    WHILE($board = mysql_fetch_array($result)):

    I’ve looked at mysqli_fetch_array but don’t know how to incorporate it within the if statement.

    if ($result=mysqli_query($con,$sql))

    #170631
    __
    Participant

    but nothing is displayed.

    Meaning, just a white screen? That means there is a fatal error, and PHP is configured not to display the message. Look in your error logs, or, enable error reporting while you troubleshoot.

    In the meantime, take a second look at your code*: you choose a query, but you never execute one. Then you try to fetch a result from a variable that doesn’t exist.

    * sorry I didn’t mention this earlier; I assumed the code you posted was an excerpt.

    What do you suggest instead

    The snippet I posted above was the standard while format.

    while( $board = mysql_fetch_array($result) ){
    
        /* rest of code goes here */
    
    }
    
    #170632
    MBM
    Participant

    Sorry no I don’t mean nothing is displayed I mean the page just refreshes with the container, nav elements, dropdown and submit button but it doesn’t fetch the data.

    I thought post would execute the query??!?!? There are no errors in the code, error reporting is on.

    I have changed the while loop to store values as arrays.

    The opening declerations :

    &lt;?php
    session_start();
    include "connect.php";
    $message = $_GET['message'];
    $formValue=array();
    foreach ($_POST as $key =&gt; $value) {
    $formValue[$key] = strip_tags($value);
    $usercomments = nl2br($_POST['usercomments']);
    $_SESSION['post_vars'][$key] = $value;
    }
    ?&gt;
    

    That’s all the php. The other HTML isn’t related to the form.

    #170636
    __
    Participant

    Sorry no I don’t mean nothing is displayed I mean the page just refreshes with the container, nav elements, dropdown and submit button but it doesn’t fetch the data.

    ah.

    I thought post would execute the query?

    No, POST sends form data from the browser to the server.

    There are no errors in the code, error reporting is on.

    Odd. In the code you posted (in your gist), you never execute the query you chose. $result is not defined anywhere before you try to fetch from it. This would cause an error. Are you sure your error reporting is set high enough (E_ALL or better)?

    $formValue=array();
    foreach ($_POST as $key => $value) {
    $formValue[$key] = strip_tags($value);
    $usercomments = nl2br($_POST['usercomments']);
    $_SESSION['post_vars'][$key] = $value;
    }

    I don’t follow why you are doing all this …?
    It doesn’t seem related to the problem we are talking about.

    I did notice, looking back over your code, that you were printing the results (the star ratings) before you actually started your HTML markup (doctype, <head>, etc.). This might have something to do with the problem, but you’d probably still see the results (they would just be in the wrong place).

    #170637
    __
    Participant

    Let’s stop for a moment and look at the logic behind what you’re doing.

    • user chooses a rating and submits it.
    • you check which rating the user selected, and choose the appropriate query to find movies with that rating.
    • query the database
    • loop over results; build HTML describing each matching movie
    • display the page, with the results included.

    Is this correct?

    #170639
    MBM
    Participant

    Yes the other code is not related, it’s an extension of the star rating script.

    error_reporting(E_ALL);

    (which is awesome by the way)

    Gave :

    Notice: Undefined variable: result

    In previous statements I used result as :

    $result = mysql_query("SELECT * FROM table");

    So I have changed $Link to result :

    $result = mysql_connect($Host, $User, $Password);

    The other errors :

    Notice: Undefined index: message in filter.php on line 5
    $message = $_GET['message'];
    Notice: Undefined index: usercomments in filter.php on line 10
    $usercomments = nl2br($_POST['usercomments']);

    Why are these flagged?!?!?! They work! They are flagged in other pages as well.

    The final error :

    Warning: mysql_fetch_row(): supplied resource is not a valid MySQL result resource in filter.php on line 39
    
    Line 39 :
    
    WHILE($row = mysql_fetch_row($result)):
    #170640
    MBM
    Participant

    Yes, that is the logic behind what I want to do. It’s part of a page in which a user can submit a comment and view ALL previous ratings (i.e. from 1-5). The filter is supposed to filter out ratings based on the star rating selected.

    I’ve nearly done it. It outputs one result only at the moment. I need to know how to fix those errors. They only displayed once I switched to using error_reporting(E_ALL);

    #170644
    MBM
    Participant

    Updated code (only ouputs on record) :

    $result = mysql_connect($Host, $User, $Password);
    if(isset($_POST['Submit'])){
    if($_POST['value'] == '1') { 
        // query to get all 1 star ratings
        $query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='1'"); 
    } 
    elseif($_POST['value'] == '2') { 
        // query to get all 2 star ratings
        $query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='2'"); 
    }   
    elseif($_POST['value'] == '3') { 
        // query to get all 3 star ratings
        $query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='3'"); 
        }   
    elseif($_POST['value'] == '4') { 
        // query to get all 4 star ratings
        $query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='4'"); 
        }   
    elseif($_POST['value'] == '5') { 
        // query to get all 5 star ratings
        $query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='5'"); 
    } 
    
    WHILE($row = mysql_fetch_array($query)){
    $title = $row['title'];
    $studio = $row['studio'];
    $language = $row['language'];
    $certification = $row['certification'];
    

    The rest of the code is as previous. I had to remove the [] above as it outputs Array – literally it displayed array.

    #170645
    __
    Participant

    In previous statements I used result as :
    $result = mysql_query("SELECT * FROM table");
    So I have changed $Link to result :
    $result = mysql_connect($Host, $User, $Password);

    It’s not the name of the variable that’s the problem, it what it points at. In your first example, $result holds the return value from mysql_query, which is probably a result set.

    In your second example, it holds the result from mysql_connect, which is probably a database connection resource.

    Notice: Undefined index: message in filter.php on line 5
    Notice: Undefined index: usercomments in filter.php on line 10
    Why are these flagged?!?!?! They work! They are flagged in other pages as well.

    What do you mean by “they work”? If you’re using the html you showed, then they should not “work.” The form you’re using doesn’t have a “usercomments” field, and no GET parameters at all.

    Warning: mysql_fetch_row(): supplied resource is not a valid MySQL result resource

    That’s because the variable you are passing to it ($result) is either undefined (as in your original code), or is a connection resource (from mysql_connect, as in your modified code).

    Yes, that is the logic behind what I want to do.

    Alright, let’s outline that in pseudo-code:

    <?php
    
    Program Logic:
    IF form "rating" was submitted 
    AND "rating" is an integer between 1 and 5:
        …connect to the DB
        …query the DB for matching ratings
        IF query is successful:
            …fetch results
            LOOP: for each result,
                …write HTML "star" ratings 
                …write other details
    
    HTML Output:
    echo HTML markup
    IF there were results from the query
        …insert them into the markup
    

    Right?
    Now, we just use those comments as instructions, and add real code that actually does it all.

    The gist you posted is marked secret. Would you rather I not disclose any of it?

    #170682
    MBM
    Participant

    Without strict Error filtering they work! I was previously using :

    error_reporting(E_ERROR);

    They variables are used in other pages so I edited those pages. I can get rid of the error messages :

    $message = $_GET['message'];

    to :

    (!empty($_GET['$message']) ? $_GET['$message'] : null);

    And :

    $usercomments = nl2br($_POST['usercomments']);

    to :

    (!empty($_POST['$usercomments']) ? $_POST['$usercomments'] : null);

    Or by moving the variables next to their relating statements.
    I use the message variable to display empty form field validation messages. When I made the change above, I get UNDEFINED VARIABLE: MESSAGE for this line (in another script not the filter ratings script) :

    <div>
    <?php print $message ; ?>
    </div>
    

    So I tried removing it from within the HTML and declaring it inside the php (at the top of the page) as follows :

    echo '
    <div>
    '. $message.'.
    '.$usercomments.'
    </div>';
    

    There are no php error messages but the empty form field validation messages do not fire, they’re displayed in the address bar but previously they displayed inside the form, where I wanted them.

    You can post from the gist. I’ve updated it to show the changes.

    The filter ratings and post ratings will all be in one page which is why it may appear as if the variables are not being used, I’ve copied a page as a template and am using it to get the filter working.

    #170710
    __
    Participant

    Without strict Error filtering they work! I was previously using :
    error_reporting(E_ERROR);

    Ah. No, they weren’t “working.” You were just ignoring the error messages.

    E_ERROR means you only want to pay attention to Fatal Errors. Undefined variables are reported at the E_NOTICE level, so you didn’t see them. Same thing with your Warning about the fetch function: that’s E_WARNING, which you had been ignoring.

    I can get rid of the error messages :
    [changing] $message = $_GET['message'];
    to :
    (!empty($_GET['$message']) ? $_GET['$message'] : null);

    That’s how you do it.
    You could also do:

    $message = isset( $_GET['message'] )? $_GET['message']: "";
    

    isset because it is faster than !empty, and "" instead of null because you expect the var to hold a string (though this second part won’t cause problems either way).

    I use the message variable to display empty form field validation messages… So I tried removing it from within the HTML and declaring it inside the php (at the top of the page) as follows :

    You don’t need to move the echo statement. That should stay at the point where you want the variable to be printed. In your php section, just make sure the $message is defined no-matter-what (i.e., even if there is no message to show). For example, if you have something like this:

    <?php
    
    if( $INeedToSendAMessage ){
        $message = "Hello, World!";
    }
    echo $message;
    

    $message will be undefined if you don’t need to send a message, but you try to echo it no-matter-what. So, make sure $message is defined no-matter-what:

    <?php
    
    $message = "";
    
    if( $INeedToSendAMessage ){
        $message = "Hello, World!";
    }
    echo $message;
    

    why it may appear as if the variables are not being used, I’ve copied a page as a template …

    Which is fine. Just make sure they are defined with default/empty values, and it’ll be fine.

    If you end up with a variable that will never be used on your page, however, it is best to simply remove it before you are done.

    You can post from the gist. I’ve updated it to show the changes.

    It is still marked secret, so I can’t fork it (you would need to change it to public). I can copy it manually, though. Just wanted to make sure it was okay.

    #170713
    __
    Participant

    I made a new gist based on our example pseudo-code outline.

    #170741
    MBM
    Participant

    This solved one of my errors :

    $message = isset( $_GET['message'] )? $_GET['message']: "";
    

    I’ll work your way through the code once I’ve debugged some of the other code.

Viewing 15 posts - 1 through 15 (of 24 total)
  • The forum ‘Back End’ is closed to new topics and replies.