We help IT Professionals succeed at work.

Code showing up in search page of PHP

austenr
austenr asked
on
211 Views
Last Modified: 2013-12-13
The PHP code is showing up in the form.  This PHP switches back and forth from HTML to PHP.  Also, when you click the search button, nothing happens.

 <?
// Search Code
// Standard Connection Data.
$db_host = "localhost";
//$db_user = "myusername";
//$db_pass = "password";
$db_name = "cpdata";
$dbac = mysql_connect($db_host);
mysql_select_db ($db_name) or die ("Cannot connect to database");

if (!$_POST['search']){
// Breaking out of php to display the form.

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "****://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta ****-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CPT Search Form</title>
<body>

<center><table width="500"><tr><td><p><font size="+1"><center>Clinical Database Search Form<center></font></b></p><br><br><p></p>
<p>This search will result in 4 fields being displayed.</p>
On this form type in what you want to search for or any part of it.
</td></tr></table><br>
<center><form name="Search CPT" method="post" action="<?php echo $PHP_SELF;?>">
Enter Your Search Criteria Here:<br>
<label>
<input name="swords" type="text" size="30" maxlength="30">
</label>
<br>
<label>
<input name="search" type="submit" id="search" value="Search">
</label>
</form> </center>
</b
}else{
// Adding slashes and changing tags so that people trying to take advantage of the system can't
$searchwords = addslashes(htmlspecialchars($_POST['swords']));
// Checking the length of string to make sure its more than 1 characters long
if (strlen($searchwords) < 1){
echo "The selection you have entered is too short, please enter another one.";
}else{
$words = explode(' ',$searchwords);
$totalwords = count($words);
$i = 0;
$searchstring = "";
// Looping to get the search string.
while ($i != $totalwords){
if ($i != 0 and $i != $wordcount){
$searchstring .= " and ";;
}
$searchstring .= "description LIKE '%$words[$i]%' or default_cpt LIKE '%$words[$i]%' ";
// Incrementing the value
$i = $i + 1;
}
// Execute the query with the search string just created.
$query = mysql_query("SELECT * FROM cpddata where $searchstring");
// Check for results
if (mysql_num_rows($query) == 0){
echo "No results where found sorry ";
}else{
while ($fet = mysql_fetch_array($query)){
   " <p>Description: {$fet['description']}<br />
         Default_CPT: {$fet['defaultcpt']}<br />
         Industrial: {$fet['industrial']}<br> />
                         Federal: {fet['federal']}}</p>";
}// End While
}// End Else
}// End Else
}// End Else


?>
</body></html>

Comment
Watch Question

Perhaps short-open tags are not enabled on your server. Change all instances of:

  <?

to:

  <?php

and see if that makes a difference. Also, there is an error here:

</form> </center>
</b
}else{

Should be (2 changes):

</form> </center>
</b>
<?php
}else{

Author

Commented:
That helped.  Now I have these warnings and nothing happens.

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\1clinical.php on line 8

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\wamp\www\1clinical.php on line 9

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in C:\wamp\www\1clinical.php on line 9
Cannot connect to database

Current Code:

 <?php
// Search Code
// Standard Connection Data.
$db_host = "localhost";
//$db_user = "myusername";
//$db_pass = "password";
$db_name = "cpdata";
$dbac = mysql_connect($db_host);
mysql_select_db ($db_name) or die ("Cannot connect to database");

if (!$_POST['search']){
// Breaking out of php to display the form.

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "****://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta ****-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CPT Search Form</title>
<body>

<center><table width="500"><tr><td><p><font size="+1"><center>Clinical Database Search Form<center></font></b></p><br><br><p></p>
<p>This search will result in 4 fields being displayed.</p>
On this form type in what you want to search for or any part of it.
</td></tr></table><br>
<center><form name="Search CPT" method="post" action="<?php echo $PHP_SELF;?>">
Enter Your Search Criteria Here:<br>
<label>
<input name="swords" type="text" size="30" maxlength="30">
</label>
<br>
<label>
<input name="search" type="submit" id="search" value="Search">
</label>
</form></center>
</b>
<?php
}else{
// Adding slashes and changing tags so that people trying to take advantage of the system can't
$searchwords = addslashes(htmlspecialchars($_POST['swords']));
// Checking the length of string to make sure its more than 1 characters long
if (strlen($searchwords) < 1){
echo "The selection you have entered is too short, please enter another one.";
}else{
$words = explode(' ',$searchwords);
$totalwords = count($words);
$i = 0;
$searchstring = "";
// Looping to get the search string.
while ($i != $totalwords){
if ($i != 0 and $i != $wordcount){
$searchstring .= " and ";;
}
$searchstring .= "description LIKE '%$words[$i]%' or default_cpt LIKE '%$words[$i]%' ";
// Incrementing the value
$i = $i + 1;
}
// Execute the query with the search string just created.
$query = mysql_query("SELECT * FROM cpddata where $searchstring");
// Check for results
if (mysql_num_rows($query) == 0){
echo "No results where found sorry ";
}else{
while ($fet = mysql_fetch_array($query)){
   " <p>Description: {$fet['description']}<br />
         Default_CPT: {$fet['defaultcpt']}<br />
         Industrial: {$fet['industrial']}<br> />
                         Federal: {fet['federal']}}</p>";
}// End While
}// End Else
}// End Else
}// End Else


?>
</body></html>
See http://www.php.net/mysql_connect for information on using this function. Should be:

$dbac = mysql_connect($db_host, $db_user, $db_pass);

You will need to uncomment those variable declarations, and make sure that you are using a valid username/password combination for your MySQL server.

Author

Commented:
I was debugging while you were looking at my code.  I remembered a similar error so I applied that code (below) to this problem and got rid of the warnings.  Now, what is left is that

Parse error: syntax error, unexpected '}' in C:\wamp\www\1clinical.php on line 54

Current code below:

 <?
// Search Code


// Standard Connection Data.
$db_host = "localhost";
//$db_user = "myusername";
//$db_pass = "password";

//mysql_connect("localhost");

//mysql_select_db("cpdata" user="root" password=" ");
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make cpdata the current db
$db_selected = mysql_select_db('cpdata', $link);
if (!$db_selected) {
    die ('Can\'t use cpdata : ' . mysql_error());
}
//$db_name = "cpt_info";
//$dbac = mysql_connect($db_host);
//mysql_select_db ($db_name) or die ("Cannot connect to database");

if (!$_POST['search']){
// Breaking out of php to display the form.

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "****://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta ****-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CPT Search Form</title>
<body>

<center><table width="500"><tr><td><p><font size="+1"><center>Clinical Database Search Form<center></font></b></p><br><br><p></p>
<p>This search will result in 4 fields being displayed.</p>
On this form type in what you want to search for, Description, Default CPT.
</td></tr></table><br>
<center><form name="Search Tutorial" method="post" action="">
Enter Your Search Criteria Here:<br>
<label>
<input name="swords" type="text" size="30" maxlength="30">
</label>
<br>
<label>
<input name="search" type="submit" id="search" value="Search">
</label>
</form> </center>
<?php
}else{
// Adding slashes and changing tags so that people trying to take advantage of the system can't
$searchwords = addslashes(htmlspecialchars($_POST['swords']));
// Checking the length of string to make sure its more than 1 characters long
if (strlen($searchwords) < 1){
echo "The selection you have entered is too short, please enter another one.";
}else{
$words = explode(' ',$searchwords);
$totalwords = count($words);
$i = 0;
$searchstring = "";
// Looping to get the search string.
while ($i != $totalwords){
if ($i != 0 and $i != $wordcount){
$searchstring .= " and ";;
}
$searchstring .= "description LIKE '%$words[$i]%' or default_cpt LIKE '%$words[$i]%' ";
// Incrementing the value
$i = $i + 1;
}
// Execute the query with the search string just created.
$query = mysql_query("SELECT * FROM cpt_info where $searchstring");
// Check for results
if (mysql_num_rows($query) == 0){
echo "No results where found sorry ";
}else{
while ($fet = mysql_fetch_array($query)){
     "<p>Description: {$fet['description']}<br />
         Default CPT: {$fet['default_cpt']}<br />
         Industrial: {$fet['industrial']}<br> />
                         Federal: {fet['federal']}}</p>";
}// End While
}// End Else
}// End Else
}// End Else


?>
</body></html>

Commented:
just noticed on line 68 you have two semi colons
$searchstring .= " and ";;

and i think you need to echo this on line 81

while ($fet = mysql_fetch_array($query)){
     "<p>Description: {$fet['description']}<br />
         Default CPT: {$fet['default_cpt']}<br />
         Industrial: {$fet['industrial']}<br> />
                         Federal: {fet['federal']}}</p>";

Commented:
also looks like you changed back the <? php back to just <? in a few places.
If its not parsing the php with the <? tag, then the line with the error is the first instance that it is recognising php and therefore is starting out with <? }
which would cause error.

so change the first line to <? php

Author

Commented:
Ok I will try to work on it today and get back to you

Author

Commented:
Now I get this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\clinicalhome.php on line 77

No results where found sorry

I need it to be able to search the entire table for what is entered in the search box even if it is a partial match and find all rows that meet the criteria and display them.  

Here is my current code which displays the form fine now but when you enter the search criteria and press submit, you get the warning error above.


 <?php
// Search Code


// Standard Connection Data.
$db_host = "localhost";
//$db_user = "myusername";
//$db_pass = "password";

//mysql_connect("localhost");

//mysql_select_db("cpdata" user="root" password=" ");
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make cpdata the current db
$db_selected = mysql_select_db('cpdata', $link);
if (!$db_selected) {
    die ('Can\'t use cpdata : ' . mysql_error());
}
//$db_name = "cpt_info";
//$dbac = mysql_connect($db_host);
//mysql_select_db ($db_name) or die ("Cannot connect to database");

if (!$_POST['search']){
// Breaking out of php to display the form.

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "****://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta ****-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CPT Search Form</title>
<body>

<center><table width="500"><tr><td><p><font size="+1"><center>Clinical Database Search Form<center></font></b></p><br><br><p></p>
<p>This search will result in 4 fields being displayed.</p>
On this form type in what you want to search for, Description, Default CPT.
</td></tr></table><br>
<center><form name="Search Tutorial" method="post" action="">
Enter Your Search Criteria Here:<br>
<label>
<input name="swords" type="text" size="30" maxlength="30">
</label>
<br>
<label>
<input name="search" type="submit" id="search" value="Search">
</label>
</form> </center>
<?php
}else{
// Adding slashes and changing tags so that people trying to take advantage of the system can't
$searchwords = addslashes(htmlspecialchars($_POST['swords']));
// Checking the length of string to make sure its more than 1 characters long
if (strlen($searchwords) < 1){
echo "The selection you have entered is too short, please enter another one.";
}else{
$words = explode(' ',$searchwords);
$totalwords = count($words);
$i = 0;
$searchstring = "";
// Looping to get the search string.
while ($i != $totalwords){
if ($i != 0 and $i != $wordcount){
$searchstring .= " and ";
}
$searchstring .= "description LIKE '%$words[$i]%' or default_cpt LIKE '%$words[$i]%' ";
// Incrementing the value
$i = $i + 1;
}
// Execute the query with the search string just created.
$query = mysql_query("SELECT * FROM cpt_info where $searchstring");
// Check for results
if (mysql_num_rows($query) == 0){
echo "No results where found sorry ";
}else{

while ($fet = mysql_fetch_array($query)){
echo     "<p>Description: {$fet['description']}<br />
         Default CPT: {$fet['default_cpt']}<br />
         Industrial: {$fet['industrial']}<br> />
                         Federal: {fet['federal']}}</p>";
}// End While
}// End Else
}// End Else
}// End Else


?>
</body></html>
 

Author

Commented:
OK I have it working, well almost.  When I submit my query I get this in return:

Description: REM SKIN TAG 1 15
Default CPT: 11200
Industrial: 192.60
/> Federal: {fet['federal']}}

Here is my present code:

 <?php
// Search Code


// Standard Connection Data.
$db_host = "localhost";
//$db_user = "myusername";
//$db_pass = "password";

//mysql_connect("localhost");

//mysql_select_db("cpdata" user="root" password=" ");
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make cpdata the current db
$db_selected = mysql_select_db('cpdata', $link);
if (!$db_selected) {
    die ('Can\'t use cpdata : ' . mysql_error());
}
//$db_name = "cpt_info";
//$dbac = mysql_connect($db_host);
//mysql_select_db ($db_name) or die ("Cannot connect to database");

if (!$_POST['search']){
// Breaking out of php to display the form.

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "****://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
<meta ****-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CPT Search Form</title>
<body>

<center><table width="500"><tr><td><p><font size="+1"><center>Clinical Database Search Form<center></font></b></p><br><br><p></p>
<p>This search will result in 4 fields being displayed.</p>
On this form type in what you want to search for, Description, Default CPT.
</td></tr></table><br>
<center><form name="Search Tutorial" method="post" action="">
Enter Your Search Criteria Here:<br>
<label>
<input name="swords" type="text" size="30" maxlength="30">
</label>
<br>
<label>
<input name="search" type="submit" id="search" value="Search">
</label>
</form> </center>
<?php
}else{
// Adding slashes and changing tags so that people trying to take advantage of the system can't
$searchwords = addslashes(htmlspecialchars($_POST['swords']));
// Checking the length of string to make sure its more than 1 characters long
if (strlen($searchwords) < 1){
echo "The selection you have entered is too short, please enter another one.";
}else{
$words = explode(' ',$searchwords);
$totalwords = count($words);
$i = 0;
$searchstring = "";
// Looping to get the search string.
while ($i != $totalwords){
if ($i != 0 and $i != $wordcount){
$searchstring .= " and ";
}
$searchstring .= "description LIKE '%$words[$i]%' or defaultcpt LIKE '%$words[$i]%' ";
// Incrementing the value
$i = $i + 1;
}
// Execute the query with the search string just created.
$query = mysql_query("SELECT * FROM cpddata where $searchstring");
// Check for results
echo mysql_error();
if (mysql_num_rows($query) == 0){
echo mysql_error();
echo "No results where found sorry ";
}else{

while ($fet = mysql_fetch_array($query)){
echo     "<p>Description: {$fet['description']}<br />
         Default CPT: {$fet['defaultcpt']}<br />
         Industrial: {$fet['industrial']}<br> />
                         Federal: {fet['federal']}}</p>";
                         
}// End While
}// End Else
}// End Else
}// End Else


?>
</body></html>


What is missing?

Author

Commented:
OK I got it to function as I wanted.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.