Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 it is : this line here if (isset($post['searchID']) && $post['searchbyequipmenttype' != '') is checking the information returned passed in from the form and only running the query if the information is there to run it. Make sure and use the form to load the page, don't just refresh it. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1336999 Share on other sites More sharing options...
blepblep Posted April 13, 2012 Author Share Posted April 13, 2012 Cool thanks for that. What do you mean make sure and use the form to load the page, not just refresh it?! I've got off the website and came back done it again and entered a number and it keeps showing the error - There was a problem with the form, please try again and make sure you have filled in a document number Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337000 Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 I didn't write that line right, I missed off a closing ] so the code shouldn't even run at all, you should be getting a parse error (unless you fixed it)...it should be if (isset($post['searchID']) && $post['searchbyequipmenttype'] != '') Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337002 Share on other sites More sharing options...
blepblep Posted April 13, 2012 Author Share Posted April 13, 2012 Yeah I fixed that so it would run. Whenever I run it it goes to the searchByDocID page but displays nothing.. I honestly don't no why this isnt working, would it have anything got to do with how I have this done? As it's not giving me any errors but not displaying anything! Would you like access to my database or what? while ($result = mysql_fetch_array( $data )) { echo $row ['doc_id'] . " <br /> " . $row ['doc_title'] . " <br /> " . $row ['doc_number'] . " <br /> " . $row ['doc_type'] . " <br /> " . $row ['revision'] . " <br /> " . $row ['cdm_link'] . " <br /> " . $row ['mars_link'] . " <br /> " . $row ['checklist_link'] . " <br /> " . $row ['link_internal_1_3']. " <br /> " . $row ['impacted_products']. " <br /> " . $row ['scope']. " <br /> " . $row ['impacted_products_2'] ." <br /> " . $row ['review_class']. " <br /> " . $row ['full_simplified'] . " <br /> " . $row ['pref_earliest'] . " <br /> " . $row ['pref_latest'] . " <br /> " . $row ['prev_reviews'] . " <br /> " . $row ['proj_name'] . " <br /> " . $row ['auth_name'] . " <br /> " . $row ['req_list'] . " <br /> " . $row ['optional_list'] . " <br /> " . $row ['information_only'] . " <br /> " . $row ['chairperson'] . " <br /> " . $row ['req_reviewers'] . " <br /> " . $row ['review_duration'] . " <br /> " . $row ['document_abstract'] . " <br /> "; } Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337012 Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 if you can give access in a secure manner (like temp user and pass) sure, pm it to me with the host address and I'll see what I can do. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337024 Share on other sites More sharing options...
blepblep Posted April 13, 2012 Author Share Posted April 13, 2012 I'll just send it to you through pm, it's not live though so I don't no if that will matter? Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337028 Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 ok, been looking over everything and have noticed I passed over a fairly important point, change the SQL statement to this: $sql = <<<SQL SELECT * FROM tc_tool.forms WHERE ( doc_number =$id ) SQL; Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337034 Share on other sites More sharing options...
blepblep Posted April 13, 2012 Author Share Posted April 13, 2012 Nope, still not displaying anything Heres a screenshot of my database if this is any help? I thought by echoing ['doc_id'] etc it would display what id it was along with the ['doc_title'] and the rest of them. This is when I browse the table - Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337037 Share on other sites More sharing options...
blepblep Posted April 13, 2012 Author Share Posted April 13, 2012 Heres it described Muddy - Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337038 Share on other sites More sharing options...
Muddy_Funster Posted April 13, 2012 Share Posted April 13, 2012 OK, I have this working with a sample dataset: FORM PAGE : <?php //---------------- // Include files // - Connect to DB //---------------- include "newheader.php"; include "connect_db.php"; include "lib/toolfunctions.php"; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Language" content="en" /> </head> <body> <p> <form action = "searchByDocID.php" method = "post"> <!-- body onLoad focuses textbox on load -- Only works in Chrome at the moment --> <body onLoad = "documentNumber.focus()"> <input type= "text" id= "documentNumber" name= "searchbyequipmenttype"/> <input type= "submit" name= "searchID" value="Search By Doc ID" /><br /> <font size = '2'>EG: 123</font> </p> </form> <div id="toolFooterWrapper"> <div id="toolFooter"> TC Tool<br/>Version 1.00 </div> </div> </body> </html> and searchByDocID PAGE : <?php include 'connect_db.php'; include 'newheader.php'; function sanitize_data($data) { $data = array_map('trim',$data); $data = array_map('strip_tags',$data); $data = array_map('htmlspecialchars',$data); $data = array_map('mysql_real_escape_string',$data); return $data; } $post = sanitize_data($_POST); if (isset($post['searchID']) && $post['searchbyequipmenttype'] != '') { $id = $post['searchbyequipmenttype']; $sql = <<<SQL SELECT * FROM sampleformid WHERE ( doc_id = $id ) SQL; $data = mysql_query($sql) or die(mysql_error()."<br>-----------<br>$sql"); $num = mysql_num_rows($data); while($row = mysql_fetch_assoc($data)) { echo "{$row['doc_id']}<br />". "{$row['doc_title']}<br />". "{$row['doc_number']}<br />". "{$row['doc_type']}<br />". "{$row['revision']}<br />". "{$row['cdm_link']}<br />". "{$row['mars_link']}<br />". "{$row['checklist_link']}<br />". "{$row['link_internal_1_3']}<br />". "{$row['impacted_products']}<br />". "{$row['scope']}<br />". "{$row['impacted_products_2']}<br />". "{$row['review_class']}<br />". "{$row['full_or_simplified']}<br />". "{$row['earliest_date']}<br />". "{$row['latest_date']}<br />". "{$row['previous_tc_reviews']}<br />". "{$row['proj_name']}<br />". "{$row['author_name']}<br />". "{$row['req_list']}<br />". "{$row['optional_list']}<br />". "{$row['information_only']}<br />". "{$row['chairperson']}<br />". "{$row['reviewers_required']}<br />". "{$row['review_duration']}<br />". "{$row['document_abstract']}<br />"; } } else{ echo "there was a problem with the form, please try again and make sure you have filled in a document number"; } include 'footer.php'; ?> the last include thre may be off as I deleted it and put it back in (the notice was bugging me) The Result I got was: 123 test X123-V2 test 1.0 n/a pluto till after none NARROW still none c simple like me whenever thenever none test me no no probably me not likely short as possible sure is I forgot to say - you will need to change the table name back from "FROM sampleformid" in the SQL code block, yours should read "FROM forms" Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337042 Share on other sites More sharing options...
blepblep Posted April 13, 2012 Author Share Posted April 13, 2012 IT WORKS!!!!! YES! Thank you so much Muddy, really really appreciate it. Thanks A LOT man!! :D :D Can I ask you another thing, in my database my most recent entry is 342, when I enter 341 or below nothing appears. Is there any way I can make a message appear saying something like - Sorry, no entry for that ID, please enter another. Any way to do that if someone enters an ID or say 341 or below, or something like 1000? Basically to give an error if an ID doesnt exist in the database.. Thanks again I really appreciate it by the way. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337099 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 $data = mysql_query($sql) or die(mysql_error()."<br>-----------<br>$sql"); $num = mysql_num_rows($data); //<<<------ This line here is counting the number of rows returned by the query while($row = mysql_fetch_assoc($data)) look at the comment above. You see how $num is being assigned with the number of rows returned from the query? you just need to write an if statement to check that at least 1 row is fouund from the query, if it is, display the results, otherwise show your message about not finding the document. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337740 Share on other sites More sharing options...
blepblep Posted April 16, 2012 Author Share Posted April 16, 2012 What do you mean, something like this is it Muddy? if($num != 0) { while($row = mysql_fetch_assoc($data)) { echo "...... ....." else{ echo "Sorry, no entry for that ID, please enter another."; } Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337745 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 yeah, something like that should do it. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337748 Share on other sites More sharing options...
blepblep Posted April 16, 2012 Author Share Posted April 16, 2012 Thanks Muddy for all the help, really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337752 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 no worries, glad I could help. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337757 Share on other sites More sharing options...
blepblep Posted April 16, 2012 Author Share Posted April 16, 2012 Ahhhh balls, this doesnt work in Internet Explorer!!! Search returns nothing in IE, can never be straight forward :'( Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337759 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 PHP isn't browser specific, it has no direct interface with the browser (you need javascript for that). If it's working on one browser and not another it's most likely 1. your loading an old version of the page in the other browser 2. CSS is out (some browsers are stricter than others about errors in css) 3. HTML is malformed (some browsers won't render pages if the HTML code is too badly formed, others will just put up with it a bit better) tick off all three of those and you *should* get it working on all browsers. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337763 Share on other sites More sharing options...
blepblep Posted April 16, 2012 Author Share Posted April 16, 2012 Cool, thanks Muddy. I've one more question if you dont mind me asking. In my forms, I have certain fields that are not applicable to each forms, so in the fields that are not applicable they say N/A. But the N/A fields have to be there as they are in all of the forms. The one problem I have with this is that the value N/A doesnt store in the database or display in the form when the ID is entered to search for. Have you any idea how I can get the N/A to display in the form when the ID is searched and have it store in the database too, without the user having to type it in? This is one field that is N/A - <tr> <td>Review Duration</td> <td><input type = "text" size = "51" name = "review_duration" value = "N/A" readonly = "readonly"/></td> </tr> And then when searched it appears like this, the fields that are meant to say N/A are the ones that have nothing beside them like Full or Simplified etc - Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337798 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 you don't need to store the N/A in the database, infact it's probably best that you dont do it. all you need to do is some conditional formating on your display. Something along the lines of: while($row = mysql_fetch_assoc($data)) //------------------------------ //this will check each result and replace any empty fields with 'N/A' foreach($row as $k => $v){ if(trim($v) == ''){ $v = 'N/A'; } } //------------------------------ { echo "{$row['doc_id']}<br />". Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337799 Share on other sites More sharing options...
blepblep Posted April 16, 2012 Author Share Posted April 16, 2012 Sorry I don't really understand to be honest. The reason I need N/A is because I'm using only oneDB, and if full or simplified isn't used in one form it is used in another if you get me? If I don't store something in the database (Like N/A for example) I get an error saying Query doesn't match column count, so I have to have some value being passed into the fields that are not needed. // Prints out details related to ID entered if($num != 0) { while($row = mysql_fetch_assoc($data)) //------------------------------ //this will check each result and replace any empty fields with 'N/A' foreach($row as $k => $v) { if(trim($v) == '') { $v = 'N/A'; } } //------------------------------ { echo "<b>Document ID:</b> {$row['doc_id']}<br />". But it doesnt displaying anything that was entered when I search for the document, it leaves everything blank.. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337808 Share on other sites More sharing options...
Muddy_Funster Posted April 16, 2012 Share Posted April 16, 2012 you can fix the Query doesn't match comlumn count by explicitly listing the fields that you want to insert into : INSERT INTO tableName (fieldname1, fieldname3, fieldname46, fieldname47) VALUES ($var1, $var2, $var3, $var4) that little bit of code shouldn't affect the display at all other than to put N/A where the blanks would have been. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337812 Share on other sites More sharing options...
blepblep Posted April 16, 2012 Author Share Posted April 16, 2012 Cool thanks, I fixed the query now! But now that I've gotten rid of the fields that aren't relevant to the forms (Instead of having N/A), when I search it still displays nothing if I put in the - if($num != 0) { while($row = mysql_fetch_assoc($data)) //this will check each result and replace any empty fields with 'N/A' foreach($row as $k => $v) { if(trim($v) == '') { $v = 'N/A'; } } And if I take out the foreach of above, it displays the search results but still shows blank fields. Would it be better to leave the fields as N/A in the form, submit them to the database or is there a way I can do what you showed, like pass in N/A to the returned search fields so they wont be left blank? Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1337842 Share on other sites More sharing options...
Muddy_Funster Posted April 17, 2012 Share Posted April 17, 2012 Let's step it up, throw away the foreach that's causing the problem, we can do the N/A thing by writing a tiny little custom function, then applying that to the output: add this code neer the top of the page: function custOut($val){ if (is_array($val)){ foreach($val as $k=> $v){ if(trim($v) == '' || empty($v) || !isset($v)){ $v = 'N/A'; } } } else{ if(trim($val) == '' || empty($val) || !isset($val)){ $val = 'N/A'; } } return $val; } then we change up the code for the display: echo custOut($row['doc_id'])."<br />". custOut($row['doc_title'])."<br />". custOut($row['doc_number'])."<br />". custOut($row['doc_type'])."<br />". custOut($row['revision'])."<br />". custOut($row['cdm_link'])."<br />". custOut($row['mars_link'])."<br />". custOut($row['checklist_link'])."<br />". custOut($row['link_internal_1_3'])."<br />". custOut($row['impacted_products'])."<br />". custOut($row['scope'])."<br />". custOut($row['impacted_products_2'])."<br />". custOut($row['review_class'])."<br />". custOut($row['full_or_simplified'])."<br />". custOut($row['earliest_date'])."<br />". custOut($row['latest_date'])."<br />". custOut($row['previous_tc_reviews'])."<br />". custOut($row['proj_name'])."<br />". custOut($row['author_name'])."<br />". custOut($row['req_list'])."<br />". custOut($row['optional_list'])."<br />". custOut($row['information_only'])."<br />". custOut($row['chairperson'])."<br />". custOut($row['reviewers_required'])."<br />". custOut($row['review_duration'])."<br />". custOut($row['document_abstract'])."<br />" That should do it. Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1338029 Share on other sites More sharing options...
blepblep Posted April 17, 2012 Author Share Posted April 17, 2012 Yep, it works also, you are a genius Muddy, cannot thank you enough! Would you be able to explain this function a little please if you have the time? Not too sure how it works. function custOut($val){ if (is_array($val)){ foreach($val as $k=> $v){ if(trim($v) == '' || empty($v) || !isset($v)){ $v = 'N/A'; } } } else{ if(trim($val) == '' || empty($val) || !isset($val)){ $val = 'N/A'; } } return $val; } Quote Link to comment https://forums.phpfreaks.com/topic/260798-php-search-form-help/page/2/#findComment-1338044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.