48 users online (0 members and 48 guests)  


  Results 1 to 4 of 4

Related

  1. Replies: 1
  2. Replies: 1
  3. Mysql help please    Forum: PHP Forum
    Replies: 1
  1. #1
    Rauldinho's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    1

    PHP & MySQL search

    Hi, im making a page where i have some text boxes where ill take input from the user and use it to make a SELECT...WHERE statement to search in a database. Im using the POST method and all works ok, but the problem is that if a text box is empty then the SELECT wont give me the rigth result because it will be saying something like "SELECT * FROM table WHERE field1 = text_box1" and if text_box1 is empty (the user did not write anything there) then it wont make the search right, hope i make myself clear in what my problem is and that someone can explain to me what can i do to in this cases where i need to validate not to use fields where the user doesnt put data, thanks!!

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: PHP & MySQL search

    play around with this:
    HTML Code:
    <html>
    
    <head>
    <script type="text/javascript">
    function SendForm(form){
    	if(form.txtbox.value){
    		form.submit();
    	}else{
    		alert("you must complete the textbox to continue!");
    	}
    }
    </script>
    </head>
    
    <body>
    
    <form name="myform" method="POST" action="scriptname.php">
    	<input type="text" name="txtbox" size="20"><input type="button" value="Send" name="B1" onclick="SendForm(this.form);">
    </form>
    
    </body>
    
    </html>

  3. #3
    starling's Avatar
    New User

    Status
    Offline
    Join Date
    Jan 2006
    Posts
    3

    Re: PHP & MySQL search

    good info here..i will play around with it too.

  4. #4
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: PHP & MySQL search

    If you don't want to do javascript, it's a bit long-winded, but can be done:

    PHP Code:
    $query 'SELECT * FROM table WHERE ';
    if (
    $_POST['length']) { $query .= "length = '".$_POST['length']."' AND "; }
    if (
    $_POST['width']) { $query .= "width = '".$_POST['width']."'  AND "; }
    if (
    $_POST['height']) { $query .= "height = '".$_POST['height']."'  AND "; } 
    repeat for each $_POST
    PHP Code:
    $query substr($query,0,-4); //remove the "AND" from the end
    if (substr($query,-2)=='WH') { $query substr($query,-3); }
    //if no boxes were full, truncates the query
    //to be "SELECT * FROM table"

    //Now run the query and do whatever you need to do ... 
    I've knocked that up off the top of my head, so please double check the syntax etc



Tags for this Thread