close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

MySqli search issue

Thread began 5/06/2017 8:44 pm by GaryMorris | Last modified 9/01/2017 3:45 pm by Ray Borduin | 273 views | 7 replies |

GaryMorris

MySqli search issue

Ok..I have to ask for some input.. been working on this most of the day and although I am getting closer I am now at a loss.
On my main admin page, I want to include a search area so the admin can search for several fields.

Here is the code. I actually got most of it from a post here and modified it for my needs. I am not getting any errors that I can tell. Just no results.
Here is the form code and the table to display the results.. What am I missing?

<form name="admsrch" method="get" action="">
Enter Record #, Last Name or License Number to search.
<br>

<table width="400" border="0">
<tr>
<td width="195" align="right">Record #</td>
<td width="195"><label for="mainID"></label>
<input name="mainID" type="text" id="mainID" size="30"></td>
</tr>
<tr>
<td align="right">Last Name</td>
<td><label for="lastName"></label>
<input name="lastName" type="text" id="lastName" size="30"></td>
</tr>
<tr>
<td align="right">License Number</td>
<td><label for="licenseNumber"></label>
<input name="licenseNumber" type="text" id="licenseNumber" size="30"></td>
</tr>
</table>
<br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>

<table width="500" border="0">
<tr>
<td>Record Number</td>
<td>Last Name</td>
<td>License Number</td>
</tr>
<?php
while(!$rsResults->atEnd()) {
?>
<tr>
<td>
<?php echo($rsResults->getColumnVal("mainID")); ?></td>
<td><?php echo($rsResults->getColumnVal("lastName")); ?></td>
<td><?php echo($rsResults->getColumnVal("licenseNumber")); ?></td>
</tr> <?php
$rsResults->moveNext();
}
$rsResults->moveFirst(); //return RS to first record
?>
</table>

and here is the php I have.

<?php
$rsResults = new WA_MySQLi_RS("rsResults",$conn_mdb1,0);
$rsResults->setQuery("SELECT * FROM maintbl WHERE mainID LIKE ? OR lastName LIKE ? AND licenseNumber LIKE ? ORDER BY mainID ASC");
$rsResults->bindParam("i", "".(isset($_GET['mainID'])?$_GET['mainID']:"") ."", "-1"); //colname
$rsResults->bindParam("s", "".(isset($_GET['firstName'])?$_GET['firstName']:"") ."", "-1"); //paramNam
$rsResults->bindParam("s", "".(isset($_GET['licenseNumber'])?$_GET['licenseNumber']:"") ."", "-1"); //paramLic
$rsResults->execute();
?>
Thanks ahead of time.

New Update.. Fixed a couple of misspels firstName should be lastName and one other. I got it to search by all 3 fields by changing the ANDs to ORs. Is that correct?

Sign in to reply to this post

Ray BorduinWebAssist

Whether to use AND or OR is really up to you and how you want your search to work. Do you want to require all fields to have valid values and have each entry limit the results? If so, then AND is the correct solution. Usually searches use AND so that each search field entry limits the result more (instead of expanding it).

Sign in to reply to this post

GaryMorris

That makes sense.. Originally it was Id OR lastname AND license number.
It would not find anything if I entered a name or license number. search by Id worked. As soon as I changed it to OR OR it worked fine. The only one that would possibly have more than one is the lastname field..
Thanks for the info..

Sign in to reply to this post

GaryMorris

I have two questions here.. How do I modify this code:

<?php
$rsResults = new WA_MySQLi_RS("rsResults",$conn_mdb1,0);
$rsResults->setQuery("SELECT * FROM maintbl WHERE mainID LIKE ? OR firstName LIKE ? OR lastName LIKE ? OR licenseNumber LIKE ? ORDER BY mainID ASC");
$rsResults->bindParam("i", "".(isset($_GET['mainID'])?$_GET['mainID']:"") ."", "-1"); //colname
$rsResults->bindParam("s", "".(isset($_GET['firstName'])?$_GET['firstName']:"") ."", "-1"); //paramNam
$rsResults->bindParam("s", "".(isset($_GET['lastName'])?$_GET['lastName']:"") ."", "-1"); //paramNam
$rsResults->bindParam("s", "".(isset($_GET['licenseNumber'])?$_GET['licenseNumber']:"") ."", "-1"); //paramLic
$rsResults->execute();
?>

so it will find partial matches.. Like say the name was Reddy, searching for Red does not find it.

Secondly - see attached page.
I also have modified this search to use only one search field. I have it working but I wanted to try it without using WA to do it. Just to learn.
All I am trying to figure out is how to format the results from the array. I have it echoing the results to the screen fine but cant figure out how to get it into a formatted table. code on the attached page prints a table but it is one long row not rows/columns.
Would appreciate any input.

Thanks.

Sign in to reply to this post

Ray BorduinWebAssist

Instead of "s" you can use "c" to do a "contains" search instead of an exact match.

For your hand made page you just have to include a <tr> in your repeated code so that a new row is created for each row in the table.

Sign in to reply to this post

GaryMorris

Thanks for the info.

Got the WA updated and works great.. Also got my own version working and used it on 2 separate sites..

Simple question.. Should you (I) always use prepared statements for even simple queries.. I understand the implications in inserting and all but is it necessary for say just a simple query listing say all fields that match whatever....
Thanks. Hope its ok to ask this.. Trying to learn this on my own. So many opinions on the net...

Sign in to reply to this post

Ray BorduinWebAssist

There are different opinions on prepared statements. Some say it is too much overhead to use, others say it is worth the overhead for the guarantee of security. I've updated the library to allow users to turn off prepared statements, but have it turned on by default, and that is a reflection of my overall opinion on the topic.

Sign in to reply to this post

GaryMorris

Ok Ray, Thank you for you opinion.. I figure I may as well learn them as security would be a concern these days.
Have a great holiday!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...