Results 1 to 20 of 20

Thread: Where clause in PHP doesn't work with variable
      
   

  1. #1
    Join Date
    Jan 2010
    Posts
    14

    Question 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. #2
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    78

    Default 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.

  3. #3
    Join Date
    Jan 2010
    Posts
    14

    Default 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.

  4. #4
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    78

    Default 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.

  5. #5
    Join Date
    Jan 2010
    Posts
    14

    Post Re: Where clause in PHP doesn't work with variable

    Mattski:

    Thank you so much!!! It works now.

  6. #6
    Join Date
    Jan 2010
    Posts
    14

    Default 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.

  7. #7
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    78

    Default 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.

  8. #8
    Join Date
    Jan 2010
    Posts
    14

    Default Re: Where clause in PHP doesn't work with variable

    Mattski:

    Thank you so much for your help.

  9. #9
    Join Date
    Jan 2010
    Posts
    14

    Default 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

  10. #10
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    78

    Default 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.

  11. #11
    Join Date
    Jan 2010
    Posts
    14

    Default 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

  12. #12
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    78

    Default 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.

  13. #13
    Join Date
    Jan 2010
    Posts
    14

    Default 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!

  14. #14
    Join Date
    Jan 2008
    Location
    Colorado
    Posts
    78

    Default 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.

  15. #15
    Join Date
    Jan 2010
    Posts
    14

    Default Re: Where clause in PHP doesn't work with variable

    Thank you so much, Mattski. I missed <.

  16. #16
    Join Date
    Jan 2010
    Posts
    14

    Default Re: Where clause in PHP doesn't work with variable

    Hi:

    I want to have a pull down menu for US states. But don't know how to get a value. I found some thing from web:
    <form action="./" method="GET">
    <select name="state">
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
    </select>

    But the name return '' after selecting.

    Please help me.

  17. #17
    Join Date
    Jan 2010
    Posts
    14

    Default Re: Where clause in PHP doesn't work with variable

    Hi:

    I am confused with the date field. I have the following code for a birthdate entry:
    <td>Birth Date: <input type="text" name="birthdate" size="8" maxlength="8"> ex. 19910101</td
    ....
    mysql_query("INSERT INTO studentname (birthdate) values ('$_POST[birthdate]')";

    Now I got error says:

    Error: Incorrect date value: '20000212'

    Should I do some conversion? How do I do it?

    Thank you for your help.

  18. #18
    Join Date
    Jan 2010
    Posts
    14

    Default Re: Where clause in PHP doesn't work with variable

    Thank you all. I have fixed the problems.

  19. #19
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    12,080

    Default Re: Where clause in PHP doesn't work with variable

    Why don't you use ABVFP to automatically create the table and store the form values ???

    Unless you are doing this for learning purposes.
    Navaldesign
    Logger Lite: Low Cost, Customizable, multifeatured Login script
    Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
    DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
    Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!


  20. #20
    Join Date
    Jan 2008
    Posts
    1

    Default Re: Where clause in PHP doesn't work with variable

    i need to have youtube on my web and want to publish my video on my website .what do i do ? i have youtube account.i don't want to publish/uppload directly to youtube .

    thank you inadvance!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Posting Permissions

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