a listbox populated with query strings when selected particular query to be executed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahilar12
    New Member
    • Feb 2009
    • 27

    a listbox populated with query strings when selected particular query to be executed

    hi all,

    somebody help me out on this

    i have a listbox where i have the values as starts with and ends with and next to that is a textbox where we give the input whether the fieldname should start with a particular letter or end with a particular letter

    say for example the field name lastname is to be executed

    so what we do is select a value from listbox say startswith and in the textbox we give as letter p and that should execute a mysql query that the lastname should start with letter p and display all the lastnames which all starts with letter p

    hope could be understood

    thanks in advance
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    this sounds like a hell of validation to do… anyways you might want to look here on string comparison in MySQL.

    Comment

    • Ciary
      Recognized Expert New Member
      • Apr 2009
      • 247

      #3
      i think you better write 3 SQL Queries looking like this:
      Code:
      if($_POST['validateType'] == 'start'){
         $query = "SELECT * FROM yourtable WHERE lastname LIKE " . $_POST['myval'] . "%";
      } if($_POST['validateType'] == 'end'){
         $query = "SELECT * FROM yourtable WHERE lastname LIKE %" . $_POST['myval'];
      } else {
         $query = "SELECT * FROM yourtable WHERE lastname LIKE %" . $_POST['myval'] . "%";
      }
      i'm not sure if this is what you need but it generates a recordset depending on what you selected

      Comment

      • ahilar12
        New Member
        • Feb 2009
        • 27

        #4
        reply for comparision of query string

        hi dormi,

        could u clarify me what these variables represent in this following code

        1.validateType
        2.start
        3.myval


        Code:
        	  if($_POST['validateType'] == 'start')
        		  {
            $query = "SELECT * FROM yourtable WHERE lastname LIKE " . $_POST['myval'] . "%";
         } if($_POST['validateType'] == 'end'){
            $query = "SELECT * FROM yourtable WHERE lastname LIKE %" . $_POST['myval'];
         } else {
            $query = "SELECT * FROM yourtable WHERE lastname LIKE %" . $_POST['myval'] . "%";
         }
        Last edited by Markus; Apr 27 '09, 10:41 AM. Reason: Fixed [code] tags.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by ahilar12
          could u clarify me what these variables represent in this following code

          1.validateType
          2.start
          3.myval
          1. where to have the search sting ("start": at the beginning, "end": at the end, anywhere in the text)
          2. see above
          3. the search term

          Comment

          • ahilar12
            New Member
            • Feb 2009
            • 27

            #6
            reply

            hi ciary
            check out this code and tell me whats wrong

            Code:
            <html>
            
            <head>
            <script language="javascript">
            function ShowHideForm(o){
                if(o.checked == true)
                    document.getElementById("formId").style.display="block";
                else
                    document.getElementById("formId").style.display="none";
            }
            </script>
            <body>
            
            <?php
            include 'menu.php';
            ?>
            
            <form name="createreport" action="insertreport.php" method="POST">
            <input type="checkbox" name="test" onclick="ShowHideForm(this)"> First Name
            <br>
            <br>
            <div id="formId" style="display:none">
            <table border="1">
              <tbody>
                <tr>
                  <td>FirstName</td>
            	  <td><input type="text" name="myval"/>   </td>
            	  <td><select name="validatetype">
            	  <option name="start">start</option>
            	  <option name="end">end</option>
            
            
            </select>
            </td>
            
            <td><input type="submit" value="submit"/></td>
            
                </tr>
              </tbody>
            </table>
            
            </div>
            </form>
            
            </body>
            </html>
            Code:
            <?php
            
                   include 'connect.php';
            
            	   $ins=mysql_query("insert into createreport(validatetype,myval)values('$_POST[validatetype]','$_POST[myval]')");
            
            if($ins)
            {
              echo "inserted";
            }
            
            if(!$ins)
            {
            	echo "mysql_error()";
            }
            
            
            	  if($_POST['validatetype'] == 'start')
              {
                $query = mysql_query("SELECT * FROM createreport WHERE lastname LIKE " . $_POST['myval'] . "%");
            	echo $query;
             } 
             else if($_POST['validatetype'] == 'end')
            {
                $query = mysql_query("SELECT * FROM createreport WHERE lastname LIKE %" . $_POST['myval']);
            	echo $query;
             } else 
            {
                $query = mysql_query("SELECT * FROM createreport WHERE lastname LIKE %" . $_POST['myval'] . "%");
            	echo $query;
             }
            ?>
            Last edited by Markus; Apr 27 '09, 10:41 AM. Reason: Fixed [code] tags.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by ahilar12
              check out this code and tell me whats wrong
              there's no input validation (see SQL Injection). and some indices are not quoted.

              what is the code not doing / doing, what it should do / not do?

              are there any error messages?

              Comment

              • Ciary
                Recognized Expert New Member
                • Apr 2009
                • 247

                #8
                what does it say as error? or what do you see?

                ps. try codeblocks, makes it easier to read

                Comment

                • ahilar12
                  New Member
                  • Feb 2009
                  • 27

                  #9
                  reply

                  hi ciary,

                  what is code blocks?

                  i m a beginner to php.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    codeblocks belong to the forum to make source code easier to read.

                    Code:
                    // this is a code block

                    Comment

                    • Ciary
                      Recognized Expert New Member
                      • Apr 2009
                      • 247

                      #11
                      it's not php. it's when you post code on the forum. on top of your editor when replying, you see a symbol like this: #. it will add codeblocks. then put your code between them.

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        [code] code goes here [/code].

                        [PHP] and [HTML] used to do the same thing, but no more. Please use the above.

                        Moderator.

                        Comment

                        • ahilar12
                          New Member
                          • Feb 2009
                          • 27

                          #13
                          thanks

                          oh ok

                          understood

                          thanks

                          Comment

                          • Ciary
                            Recognized Expert New Member
                            • Apr 2009
                            • 247

                            #14
                            np :)

                            now, you said something about an error in the code. what does it do? is that what you want it to do? are there any errors?

                            Comment

                            Working...