Results 1 to 7 of 7

Thread: SQL question in php

  1. #1
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default SQL question in php

    Hi

    are working on a php site where u can make a SQL question
    by choosing from different options from a php dropdown meny:

    PHP Code:
            <b>Select:</b>
            <
    select name="Select"
            <
    option>Välj</option>
            <
    option>Alla (*)</option>
            <
    option>Förnamn</option>
            <
    option>Efternamn</option>
            <
    option>Adress</option>
            <
    option>Telefon</option>
            <
    option>E-post</option>
            <
    option>Personnummer</option>
            </
    select>
            <
    b>From:</b>
            <
    select name="From">
            <
    option>Välj</option>
            <
    option>DB</option>
            <
    option>DB2</option>
            </
    select>
            <
    b>Where:</b>
            <
    select name="Where"
            <
    option>Välj</option>
            <
    option>Alla (*)</option>
            <
    option>Förnamn</option>
            <
    option>Efternamn</option>
            <
    option>Adress</option>
            <
    option>Telefon</option>
            <
    option>E-post</option>
            <
    option>Personnummer</option>
            </
    select>
            <
    select name="Where2">
            <
    option>Välj</option>
            <
    option>=</option>
            <
    option>></option>
            <
    option><Less then</option>
            <
    option>>=</option>
            <
    option><=</option>
            <
    option><></option>
            <
    option>LIKE</option>
            </
    select>
            <
    b>Optional:</b>
            <
    input type="text" size="30" name="Optional"></br
    The php dokument:

    PHP Code:
    <?php
        mysql_connect
    ("localhost""admin""") or die('Du är inte välkomen här!!!');
        
    mysql_select_db('test');

        
    $Select=$_POST['Select'];
        
    $From=$_POST['From'];
        
    $Where=$_POST['Where'];
        
    $Where2=$_POST['Where2'];
        
    $Optional=$_POST['Optional'];
        
        if (
    $Select==$_POST['Select'] && $From=$_POST['From'] && $Where=$_POST['Where'] && $Where2=$_POST['Where2'] && $Optional=$_POST['Optional'])
        {
        echo 
    "Data ur databas $From med följande SQL kod:""</p>";
        echo 
    "<b>Select</b> $Select <b>From</b> $From <b>Where</b> $Where <b>Where2</b> $Where2 <b>Optional</b> $Optional";
            
    // Make a MySQL Connection
            
    $query "SELECT $Select FROM $From WHERE $Where$Where2 '$Optional'"
                 
            
    $result mysql_query($query) or die(mysql_error());
            echo 
    "<table border =1> </br>";
            echo 
    "<tr>
                <th>
    $Select</th>                
                </tr>"
    ;

            while(
    $row mysql_fetch_array($result))
                {
                echo 
    "<tr>".
                    
    "<td>".$row[$Select]."</td>".    
                    
    "</tr>";
                }
            echo 
    "</table>";
        }
        else
        {
        echo 
    "...";
        }
        
    ?>
    but i cant get it to work the problom starts when it are defining the where question, do any1 have any idée what the problom is and have a solution to it?

  2. #2
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    try echoing each variable you receive
    ie
    echo $select;
    echo $from;
    etc or just
    echo $select.$from

    and see if the contents are what you wanted.

    if everything seems fine then echo $query. once you've got the query echoed, put that to the database and see if it comes back with answers and take it from there.

    hope that helps
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  3. The Following User Says Thank You to city_coder For This Useful Post:

    jes___per (03-02-2009)

  4. #3
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    PHP Code:
    $query "SELECT '$Select' FROM '$From' WHERE '$Where' '$Where2' '$Optional' "
    You must place single quotes around your variables in the SQL query.

    They way your query is written, $Optional is not optional.

    Good luck.

  5. The Following User Says Thank You to JasonDFR For This Useful Post:

    jes___per (03-02-2009)

  6. #4
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by city_coder View Post
    try echoing each variable you receive
    ie
    echo $select;
    echo $from;
    etc or just
    echo $select.$from

    and see if the contents are what you wanted.

    if everything seems fine then echo $query. once you've got the query echoed, put that to the database and see if it comes back with answers and take it from there.

    hope that helps
    ok, thx but when i try it, the code printing i get from it are "Select Förnamn From 1 Where 1 Where2 1 Optional test"

    when From are selected as DB, Where as Förnamn and Where2 as =

  7. #5
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    As Jason says, i think the problem may be that there needs to be quotes round the variables otherwise it will just use the variable names.

    See if that makes a difference.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  8. The Following User Says Thank You to city_coder For This Useful Post:

    jes___per (03-02-2009)

  9. #6
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    yeah think that did the trick, thanks for all.

    BTW hove do i make a checkbox that when i check it a new "select dropdown" will apere?

  10. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    That will be one for the JavaScript section

Bookmarks

Posting Permissions

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