Announcement

Collapse
No announcement yet.

Where clause in PHP doesn't work with variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Where clause in PHP doesn't work with variable

    Hi:

    I am learning PHP and have been trying to make this following work for some time but no luck.
    $sql="SELECT * FROM name1 WHERE LastName= '$LastName'";

    I know the last name I entered is in the database. When I hard coded LastName='Miller', the query got a return.

    Thank you very much in advance for your help!

  • #2
    Re: Where clause in PHP doesn't work with variable

    Your variable $LastName needs to have a value defined for it somewhere in your script. With the snippet you posted $LastName is undefined and will not return any rows.

    I'm guessing you are trying to take user input to look up the name so what you'll need to do is define $LastName as the input from the user.

    $LastName = mysql_real_escape_string($_POST["name"]);

    In the above statement you are defining the variable $LastName to be equal to the user input of the form variable "name". The mysql_real_escape_string function help with security.
    Mattski

    http://www.ScorpioFire.com


    Comment


    • #3
      Re: Where clause in PHP doesn't work with variable

      Thank you very much.

      Can you help me on the following code? The record is in the database but the return is 0.
      $sql="SELECT * FROM name1 WHERE LastName= '$_POST[LastName]' and FirstName = '$_POST[FirstName]'";

      Thank you in advance.

      Comment


      • #4
        Re: Where clause in PHP doesn't work with variable

        It's a better practice not to use $_POST values directly in SQL statements. You really should sanitize them first by using the function mysql_real_escape_string(). If not you run the risk of an SQL injection attack which is a big security risk. So for your query the code would be something like:

        $last = mysql_real_escape_string($_POST["LastName"]);
        $first = mysql_real_escape_string($_POST["FirstName"]);

        $sql="SELECT * FROM name1 WHERE LastName='" . $last . "' AND FirstName='" . $first . "'";

        Strings need to be encapsulated by ' and POST variables are an array so to properly address them the names need to have " around them.

        I didn't change any of the names of the variable so I'm assuming this matches your database table structure. If not make sure you are using the same names as you have in your table.
        Mattski

        http://www.ScorpioFire.com


        Comment


        • #5
          Re: Where clause in PHP doesn't work with variable

          Mattski:

          Thank you so much!!! It works now.

          Comment


          • #6
            Re: Where clause in PHP doesn't work with variable

            Hi:

            When I tried to Insert data into a table in mysql, got error saying "date type wrong" (some thing like that). The problem is the column called Birthday is date field in the table, but on web page it is <input type="text" name=birthday> . Would you please tell me how to prompt for the birthday so it can be saved to database.

            Thank you very much.

            Comment


            • #7
              Re: Where clause in PHP doesn't work with variable

              The typical date format for MySQL is YYYY-MM-DD so you need to match that format when inserting data into your table. There are a couple different ways to approach this:

              1) Force the user to enter the date in that format.
              2) Use a javascript calendar to have them select a date and then manipulate the format using PHP before inserting it into the database.

              Another thing to be aware of is that there are two different date options in MySQL date and datetime. The first one is the one you want to be using otherwise you need a time value as well.
              Mattski

              http://www.ScorpioFire.com


              Comment


              • #8
                Re: Where clause in PHP doesn't work with variable

                Mattski:

                Thank you so much for your help.

                Comment


                • #9
                  Re: Where clause in PHP doesn't work with variable

                  Hi:

                  I want to call another file say "abd.php" at the end of my current file "efg.php" which is called from form action, say after inserting entered data to a database then say 'thank you', a page which it is in another file already designed.

                  Thanks

                  Comment


                  • #10
                    Re: Where clause in PHP doesn't work with variable

                    I'm not sure if I understand this correctly. Here's what I'm assuming.
                    "efg.php" sets up the form.
                    "abd.php" processes the form and inserts data into database.

                    So "abd.php" should be included in your form declaration as the action.

                    Code:
                    <form name="myForm" method="POST" action="abd.php">
                    Then in your "abd.php" file as long as you don't have any output you can use:

                    Code:
                    header( "Location: thankyou.html" );
                    This will redirect the browser to your thank you page.
                    Mattski

                    http://www.ScorpioFire.com


                    Comment


                    • #11
                      Re: Where clause in PHP doesn't work with variable

                      I am sorry that I didn't explain it correctly. I have <FORM... ACTION="Checkinput.php"...> in my form already. Once submit button is clicked. The Checkinput.php will check if the data entered from a web page is in my database or not. If it is then I want to display a page saying "Thank you". I have a web page which does that already and I was hoping I can include it in my Checkinput.php, either by calling the file or include it in my file.

                      THanks

                      Comment


                      • #12
                        Re: Where clause in PHP doesn't work with variable

                        If you want to include the file the syntax is:

                        Code:
                        include('abd.php');
                        This basically pulls the file in to the file you are including it in for you to use it.
                        Mattski

                        http://www.ScorpioFire.com


                        Comment


                        • #13
                          Re: Where clause in PHP doesn't work with variable

                          Thank you so much! Now I find another problem. I don't know why letters html> is on the screen (a "Thank you" screen), which should not be there. In another words, should have only "Thank you..." on the page nothing elso. I use echo to print the "Thank you...". For example:

                          html>

                          Thank you for Coming!

                          Comment


                          • #14
                            Re: Where clause in PHP doesn't work with variable

                            Check to make sure that your echo statement looks like:
                            Code:
                            echo "Thank you for coming";
                            And check that your html tag is properly closed out:
                            Code:
                            </html>
                            I think you may have missed the "</" part of the html tag and it's treating it as text.
                            Mattski

                            http://www.ScorpioFire.com


                            Comment


                            • #15
                              Re: Where clause in PHP doesn't work with variable

                              Thank you so much, Mattski. I missed <.

                              Comment

                              Working...
                              X