Jump to content

Recommended Posts

Hello I am trying to have someone enter somthing into a text block and submit it and have the results found in the database be shown.  I am not sure what I am doing wrong here.  You can see my site at: http://thaczero.com/search.php

 

My coding is like this:

<?
  	$top_form = "<form method=\"post\" action=\"$_SERVER[php_SELF]\">
     <table border=\"2\" cellpadding=\"2\" cellspacing=\"2\" bordercolor=\"#333333\" bgcolor=\"#CCCCCC\">
        <tr align=\"center\" valign=\"middle\">
          <td colspan=\"1\">";
$bottom_form = "</td>
        </tr>
	<tr>
          <td>Enter your search word, name, or phrase</td>
          <td><input type=\"text\" value=\"$_POST[search]\"></td>
        </tr>
        <tr align=\"center\" valign=\"middle\">
          <td colspan=\"2\">
	  <input type=\"hidden\" name=\"op\" value=\"ds\">
	  <input type=\"submit\" name=\"Submit\" value=\"Search\"></td>
        </tr>
      </table>
  </form>";
  
if ($_POST[op] != "ds") 
{ 
	echo "In the if";
	echo "$top_form"."$bottom_form";
} 
else
	{
		echo "in the else";
		echo "$_POST[search]";
		$item = $_POST[search];
		echo "$item";
		$sql = 'SELECT * FROM `ddoitems` WHERE `ItemDescription` LIKE "$item"';
		database_connect();
		$result = mysql_query($sql) or die(mysql_error());
		echo "$top_form"."$bottom_form";
   		while($row = mysql_fetch_array($result)) {
			echo '<tr>';
        		echo '<td><div align="center" class="style1">'.$row['ItemName'].'</div></td>';
			echo '<td><div align="center" class="style1">'.$row['ItemDescription'].'</div></td>';
			echo '<td><div align="center" class="style1">'.$row['Type'].'</div></td>';
			echo '<td><div align="center" class="style1">'.$row['QuestName'].'</div></td>';
			echo '<td><div align="center" class="style1">'.$row['lvl'].'</div></td>';
			echo '<td><div align="center" class="style1">'.$row['Notes'].'</div></td>';
        		echo '</tr>';

		}
	}

?>


Link to comment
https://forums.phpfreaks.com/topic/115621-_post-help/
Share on other sites

Try putting your $item variable inside %'s like so:

 

<?php
  $sql = 'SELECT * FROM `ddoitems` WHERE `ItemDescription` LIKE "%$item%"';
?>

 

 

Also, This isn't necessary but I recommend putting quotes around your $_POST, $_GET, etc. variables.

 

Example

<?php
  $item = $_POST['search'];
  instead of
  $item = $_POST[search];
?>

Link to comment
https://forums.phpfreaks.com/topic/115621-_post-help/#findComment-594362
Share on other sites

I made some changes to your code

@Goldeneye it is necessary imagine a constant named search having a value foobar, would take a long time to debug

 

<?

  $top_form = "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">

    <table border=\"2\" cellpadding=\"2\" cellspacing=\"2\" bordercolor=\"#333333\" bgcolor=\"#CCCCCC\">

       <tr align=\"center\" valign=\"middle\">

         <td colspan=\"1\">";

$bottom_form = "</td>

       </tr>

<tr>

         <td>Enter your search word, name, or phrase</td>

         <td><input type=\"text\" value=\"{$_POST['search']}\"></td>

       </tr>

       <tr align=\"center\" valign=\"middle\">

         <td colspan=\"2\">

 <input type=\"hidden\" name=\"op\" value=\"ds\">

 <input type=\"submit\" name=\"Submit\" value=\"Search\"></td>

       </tr>

     </table>

 </form>";

 

if ($_POST['op'] != "ds")

{

echo "In the if";

echo "$top_form"."$bottom_form";

}

else

{

echo "in the else";

echo "$_POST['search']";

$item = $_POST['search'];

echo "$item";

$sql = 'SELECT * FROM `ddoitems` WHERE `ItemDescription` LIKE "%$item%"';

database_connect();

$result = mysql_query($sql/*, $dbconnection check php.net for the right syntax */) or die(mysql_error());

echo "$top_form"."$bottom_form";

  while($row = mysql_fetch_array($result)) {

echo '<tr>';

        echo '<td><div align="center" class="style1">'.$row['ItemName'].'</div></td>';

echo '<td><div align="center" class="style1">'.$row['ItemDescription'].'</div></td>';

echo '<td><div align="center" class="style1">'.$row['Type'].'</div></td>';

echo '<td><div align="center" class="style1">'.$row['QuestName'].'</div></td>';

echo '<td><div align="center" class="style1">'.$row['lvl'].'</div></td>';

echo '<td><div align="center" class="style1">'.$row['Notes'].'</div></td>';

        echo '</tr>';

 

}

}

 

?>

Link to comment
https://forums.phpfreaks.com/topic/115621-_post-help/#findComment-594363
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.