Reputation: 1233
I have a code that is up and running and for some reason just suddenly stops working, whenever I do a Search, this is the only result I am getting and table does not show: Searched For: pam
Now when i try to dump the result, and added this code: {var_dump($result);}
in the code below
while($result = mysql_fetch_array($sql)) {var_dump($result);}
Im getting the table already but its empty and here is the result:
array(32) { [0]=> string(3) "valueA" ["testA"]=> string(3) "valueA" [1]=> string(2) "valueB" ["testB"]=> string(2) "valueB" [2]=> string(7) "valueC" ["testC"]=> string(7) "valueC" [3]
here is my entire code:
<body>
<form name="search" method="post" action="">
<fieldset style='width: 500px'>
<legend align='left'><strong>Search</strong></legend>
<p>
Seach for: <input type="text" name="find" id="find" /> in
<Select NAME="field" id="field">
<Option VALUE="testA">A</option>
<Option VALUE="testB">B</option>
<Option VALUE="testC">C</option>
<Option VALUE="testD">D</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" id="search" value="Search" />
</p>
</fieldset>
</form>
<?php
//This is only displayed if they have submitted the form
if (isset($_POST['searching']) && $_POST['searching'] == "yes")
{
//If they did not enter a search term we give them an error
if (empty($_POST['find']))
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
mysql_connect("host", "username", "passw") or die(mysql_error());
mysql_select_db("testdb") or die(mysql_error());
// We preform a bit of filtering
$find = strtoupper($_POST['find']);
$find = strip_tags($_POST['find']);
$find = trim ($_POST['find']);
$field = trim ($_POST['field']);
//Now we search for our search term, in the field the user specified
$data = mysql_query("SELECT * FROM testtable WHERE UPPER($field) LIKE UPPER('%$find%')");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
$myRes = "<form action='' method='post'>
<fieldset style='width: 10px'>
<legend align='left'><strong>Results</strong></legend>
<p>
<table width='auto' border='1' cellspacing='1' cellpadding='1' align='center'>
<tr>
<th align='center' scope='row'>A</th>
<td><textarea class=readonly name=testA id=testA cols=65 rows=3>" . $result['testA'] . "</textarea></td>
</tr>
<tr>
<th scope='row'>B</th>
<td><textarea class=readonly name=testB id=testB cols=65 rows=3>" . $result['testB'] . "</textarea></td>
</tr>
</table>
</p>
</fieldset>
</form>";
}
echo $myRes;
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
Upvotes: 0
Views: 78
Reputation: 141
Looking at your code you might have a small problem with the myRes variable. You just set it again and again for every result, you should do a concatenation inside the while, not just setting the value. So instead of:
$myRes = "......"
You should do it like this:
$myRes .= "......"
Also do an init before using it in the while, do something like
$myRes = "";
while (....){
Hope it this helps you come closer to your answer.
Upvotes: 1