Match two fields in MYSQL table via php search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesmoore
    New Member
    • Jan 2011
    • 47

    #16
    One tutorial said to do this:

    Code:
    $query = "SELECT name,location,msg FROM contact WHERE name = '$searchname' AND location = '$searchlocation' ";
    AND:

    One tutorial said to do this:

    Code:
    $query = "SELECT name,location,msg FROM contact WHERE name = '$name' AND location = '$location' ";
    But that also hasn't worked...

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #17
      Those are just variables, you can call them whatever you want. The important part is to populate that variable with the correct POST data.

      You have an example of that in line 57 of your original post. But I suspect you didn't use the correct POST variable name. I don't know what you named your inputs so I can't tell you what that's supposed to be.

      Comment

      • jamesmoore
        New Member
        • Jan 2011
        • 47

        #18
        I have submitted examples into my database like the one above:

        name: shannon
        location: bexleyheath

        So I am typing the correct text in. What kind of coding am I needing/missing instead of

        Code:
        $query = "SELECT name,location,msg FROM contact WHERE name = '$name' AND location = '$location' ";
        The post parts of my script are parts another web developer told me to put in to stop blank searches retreiving all data in my search results and to stop search results appearing when space bar is put in the search box with nothing else.

        Comment

        • jamesmoore
          New Member
          • Jan 2011
          • 47

          #19
          [deleted post deleted post deleted post]

          Comment

          • jamesmoore
            New Member
            • Jan 2011
            • 47

            #20
            Referring back to POST
            this is the script I use for posting information to my database:

            Code:
            <?php
            
            // contact to database
            $connect = mysql_connect("", "", "") or die ("Error , check your server connection.");
            mysql_select_db("");
             
            //Get data in local variable
            $v_name=$_POST['name'];
            $v_location=$_POST['location'];
            $v_msg=$_POST['msg'];
             
            // check for null values
            if ($v_name==""  or $v_msg=="")
            echo "All fields must be entered, hit back button and re-enter information";
            else{
            $query="insert into contact(name,location,msg) values('$v_name','$v_location','$v_msg')";
            mysql_query($query)  or die(mysql_error());
            echo "Your message has been received";
            }
             
            
            ?>
            <html>
            <body>
            <a href="seaside.php">Back</a>
            </body
            </html>

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #21
              Lines 8 and 9 are how you get information out of POST. Line 16 is how you use the information you got out of POST. Do the same thing for the search.

              Comment

              • jamesmoore
                New Member
                • Jan 2011
                • 47

                #22
                I have tried to include this but I am so confused as where to put it

                <?php
                if(strlen(trim( $_POST['search'])) > 0) {
                //all of your php code for the search

                $search = "%" . $_POST["search"] . "%";

                mysql_connect ("", "", "");
                mysql_select_db ("");
                if (!empty($_POST["search_str ing"]))
                {
                // then perform your queries and stuff here.
                }
                $query = "SELECT name,location,m sg FROM contact WHERE name='$v_name'' AND location='$v_lo cation'";
                $result = mysql_query ($query);
                if ($result) {
                while ($row = mysql_fetch_arr ay ($result)) {
                echo "<br>$row[0]<br/>";
                echo $row[1];
                echo "<br>$row[2]<br/><br><br/>";

                }
                }
                }
                ?>

                This failed to work so what do I put instead of that?

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #23
                  From post #21, you only did the second part, you still have to do the first part.

                  Comment

                  • jamesmoore
                    New Member
                    • Jan 2011
                    • 47

                    #24
                    I have tried to put it all the places i think it could be but It still returns no results...
                    Is it a case of just copying lines 8 and 9 and placing them? Or does it involve including more code?
                    Sorry to be a pain! I have only started using php since Sunday!

                    Thanks again!




                    Code:
                    <?php
                    if(strlen(trim($_POST['search'])) > 0) {
                    //all of your php code for the search
                    $v_name=$_POST['name'];
                    $v_location=$_POST['location'];
                    $v_msg=$_POST['msg'];
                       $search = "%" . $_POST["search"] . "%";
                     
                      mysql_connect ("cust-mysql-123-03", "ufou_576458_0001", "trotman1");
                      mysql_select_db ("fourwaysdpcouk_576458_db1");
                     if (!empty($_POST["search_string"])) 
                       { 
                          // then perform your queries and stuff here. 
                       }  
                      $query = "SELECT name,location,msg FROM contact WHERE name='$v_name'' AND location='$v_location'";
                      $result = mysql_query ($query);
                      if ($result) {
                        while ($row = mysql_fetch_array ($result)) {
                          echo "<br>$row[0]<br/>";
                          echo $row[1];
                          echo "<br>$row[2]<br/><br><br/>";
                          
                        }
                      }
                    }
                    ?>

                    Comment

                    • jamesmoore
                      New Member
                      • Jan 2011
                      • 47

                      #25


                      This is the mock up input and output forms I am using here to make it clearer to you.

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #26
                        That depends on if you actually named your inputs with those names on the form. You also have two single quotes in your select query after the name variable. And you're checking for a search_string variable in your post which I doubt is in the input form.

                        Comment

                        Working...