Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: php mysql form

  1. #1
    Regular Coder Bengal313's Avatar
    Join Date
    Jun 2002
    Location
    Metro Detroit
    Posts
    298
    Thanks
    8
    Thanked 0 Times in 0 Posts

    php mysql form

    I have a quick beginner question. i have a form (search.php) that you input, in one box address number and in the other street name. Now i want to click submit which will take to say "results.php" How do I write a MYSQL statement that will search the database, "warren" in table "contacts" in fields "number" and "street" for the street number and name entered in the previous page. Can anyone show me the way.
    "Knowledge Speaks But Wisdom Listens"

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    maybe something like this:

    SELECT * FROM contacts WHERE number LIKE %warren% OR street LIKE %warren%;

  • #3
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Sorry, I mis understood the question, the correct SQL would be as follows:

    In your code create a variable that is from the posted form
    In PHP
    Code:
    $searchTerm = $_POST['street'];
    (Assumes your form has an input field like)
    Code:
    <input tpye="text" name="street" />
    then your sql will need to be
    SELECT * FROM contacts WHERE street LIKE %$searchTerm%;

    the php code would be something like:
    Code:
    $usr="YOUR USER NAME";
    $passwd="YOUR PASSWORD";
    $db="warren";
    $linkID = mysql_connect("localhost","$usr", "$passwd");
    if ($linkID != FALSE) {
    	mysql_select_db("$db", $linkID);
    	$sqlID = mysql_query("SELECT * FROM contacts WHERE street LIKE %'$searchTerm'%", $linkID);
    if($sqlID) {
                   while($data=mysql_fetch_assoc($sqlID))
                                print $data['FIELD FROM DATABASE'];
                    }
             }
    }


  •  

    Posting Permissions

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