Help. php reading before it should

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • webandwe
    New Member
    • Oct 2006
    • 142

    #1

    Help. php reading before it should

    Hi,

    My script lets you choose if you want to view table 1 or table 2 in mysql after you selected 1 or 2 from the drop down menu and press submit.

    The problem is if you run the script for the very first time it gives an error at the query. cause no one has told the code where to go and look.

    How can I make it so it does not run the script before you click submit or have it select and display the first table until your select?

    Here my script I have marked the section that is giving the problem.. with *********my text*********** ********

    Thanks


    [PHP]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>view</title>
    </head>

    <body>
    <?PHP

    $self = $_SERVER['PHP_SELF'];
    $table = $_POST['table'];


    ?>
    <form action="<?php echo( $self ); ?>" method="post">
    <select name="table">
    <option value="my_table ">1</option>
    <option value="my_table 2">2</option>
    </select>

    <label></label>
    <br>
    <input type="submit" value="submit"> </form>


    <?php

    If( $self)

    {
    $conn=@mysql_co nnect( "localhost" , "design", "superdesig n")
    or die( "Err:Conn") ;

    $rs= @mysql_select_d b ( "gptesting_co_z a_-_italchamdir", $conn )
    or die ( "Err:Db");

    # Create the query ************ How do I select a row? I tried stuff and codes but
    #cannot get it to work*********** **What happen here is because $table is not
    #specified the very first time you come to the page it gives an error..I just need to #somehow do like a "else if select first_name from a ************

    $sql="select first_name from $table";
    # this is what i tried $sql="select first_name from $table" else if ("select first_name from $table");

    #execute

    $rs = mysql_query( $sql, $conn );

    #write data

    While( $row = mysql_fetch_arr ay( $rs ) )
    {
    echo( "ID: " . $row["first_name "] );
    echo(" - First Name: " . $row["first_name "] . "<br>") ;
    }
    }
    ?>
    </body>
    </html>
    [/PHP]
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    The first thing you want to do in your script is see if this is the first time the script has been called or the second. You can do this by checking your $_POST variable to see if your form has been submitted.
    You can then use a conditional statement to perform the correct actions (show the form, or show the results).

    Comment

    • webandwe
      New Member
      • Oct 2006
      • 142

      #3
      Don't know what to do

      Hi, I cannot get the thing to work...

      If I get the thing not to display at the opening of the page it does not diplay after I called the query.

      What I don't understand is the script says "if ($self)" ->. Why does it then show the table query at the start of the page.

      I tried a few things now but could not get it to work. Can someone please help me or show me an example or start the code for me as I really don't know what to do an search the net, books and on the php site.


      This is the working code..I need to get it to run the script after you submited the form.

      Please look at location:
      # Create the query ************ I want the query to show "from a" the first time you open the page*********** *************** ***

      [PHP]
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>view</title>
      </head>

      <body>

      <form action="<?php echo( $self ); ?>" method="post">
      <select name="table">
      <option value="my_table ">1</option>
      <option value="my_table 2">2</option>
      </select>

      <label></label>
      <br>
      <input type="submit" value="submit"> </form>


      <?php
      $submit = $_POST['submit'];
      $self = $_SERVER['PHP_SELF'];
      $table = $_POST['table'];



      if ($self)

      {
      $conn=@mysql_co nnect( "localhost" , "design", "superdesig n")
      or die( "Err:Conn") ;

      $rs= @mysql_select_d b ( "gptesting_co_z a_-_italchamdir", $conn )
      or die ( "Err:Db");

      # Create the query ************ I want the query to show "from a" the first time you open the page*********** *************** ***

      $sql="select first_name from $table";

      #execute

      $rs = mysql_query( $sql, $conn );

      #write data

      While( $row = mysql_fetch_arr ay( $rs ) )
      {
      echo( "ID: " . $row["first_name "] );
      echo(" - First Name: " . $row["first_name "] . "<br>") ;
      }
      }
      ?>
      </body>
      </html>

      [/PHP]

      Kind Regards
      Louwrens
      Last edited by webandwe; Apr 17 '07, 08:39 AM. Reason: chnages

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        The variable $self is declared in this line
        Code:
        $self = $_SERVER['PHP_SELF'];
        The following is a meaningless test. What are you tring to achieve with this.
        [PHP]If( $self) [/PHP] Motoma gave you the answer but you have ignored him
        [PHP]if(isset($_POST['submit']))[/PHP]

        Comment

        • webandwe
          New Member
          • Oct 2006
          • 142

          #5
          Oh...sorry I am still new to PHP. I thought I meant I must put the whole php script in an if statement. That why I did not understand it cause it was already in a if...

          Thanks - Let me try it...

          Comment

          • webandwe
            New Member
            • Oct 2006
            • 142

            #6
            Is this how it should be, because it aint working.

            [PHP]
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <title>view</title>
            </head>

            <body>

            <form action="<?php echo( $self ); ?>" method="post">
            <select name="table">
            <option value="my_table ">1</option>
            <option value="my_table 2">2</option>
            </select>

            <label></label>
            <br>
            <input type="submit" value="submit"> </form>


            <?php
            $submit = $_POST['submit'];
            $self = $_SERVER['PHP_SELF'];
            $table = $_POST['table'];



            if(isset($_POST['submit']))

            {
            $conn=@mysql_co nnect( "localhost" , "design", "superdesig n")
            or die( "Err:Conn") ;

            $rs= @mysql_select_d b ( "gptesting_co_z a_-_italchamdir", $conn )
            or die ( "Err:Db");

            # Create the query

            $sql="select first_name from $table";

            #execute

            $rs = mysql_query( $sql, $conn );

            #write data

            While( $row = mysql_fetch_arr ay( $rs ) )
            {
            echo( "ID: " . $row["first_name "] );
            echo(" - First Name: " . $row["first_name "] . "<br>") ;
            }
            }
            ?>
            </body>
            </html>

            [/PHP]

            Comment

            • webandwe
              New Member
              • Oct 2006
              • 142

              #7
              Thanks guys got it working!

              Comment

              Working...