Jump to content

Recommended Posts

Hi Guys,

 

I made 3 files:

 

submitrecipe.php

searchrecipe.php

reciperesult.php

 

I don't have any problem submitting imformation with "submitrecipe" to mysql database. I managed to make "searchrecipe.php" too (I think so :lol: ). but I don't know how to make "reciperesult.php to show the results. I just prepared an image on html form to show you what I am trying to do (reciperesults.jpg). I attached other files too which I don't have problems with them (submitrecipe.jpg and searchrecipe)

 

Here is "submitrecipe.php" (I don't have problem with this form)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="submitrecipe.php">
<label><strong>SUBMIT YOUR RECIPE</strong><br />
<br />
Name<br />
  <input type="text" name="yourname" id="yourname" />
  </label>
<p>Country<br />
  <label>
    <select name="yourcountry" id="yourcountry">
      <option>USA</option>
      <option>CANADA</option>
      <option>ENGLAND</option>
    </select>
    </label>
</p>
<p>Type of Food <br />
  <label>
    <select name="typeoffood" id="typeoffood">
      <option>DESERT</option>
      <option>ENTREE</option>
      <option>SPICY</option>
    </select>
  </label>
</p>
<p>Your recipe<br />
  <label>
    <textarea name="yourrecipe" id="yourrecipe" cols="45" rows="5"></textarea>
  </label>
</p>
<p>
  <label>
  <input type="submit" name="Submit" id="Submit" value="Submit" />
  </label></p>
</form>

<?php

$dbcnx = @mysql_connect('localhost', 'root', '');
if (!$dbcnx) {
    exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}

if (!@mysql_select_db('recipetime')) {
    exit('<p>Unable to locate the experience ' . 'database at this time.</p>');
}

if (isset($_POST['yourrecipe'])):
    // A new recipe has been entered
    // using the form.


    $yourname = $_POST['yourname'];
    $yourcountry = $_POST['yourcountry'];
    $typeoffood = $_POST['typeoffood'];
    $yourrecipe = $_POST['yourrecipe'];


    $sql = "INSERT INTO recipeform SET    
    name='$yourname',
    country='$yourcountry',
    typeoffood = '$typeoffood', 
    yourrecipe='$yourrecipe'";


    if (@mysql_query($sql)) {
        echo '<p>New recipe added</p>';
    } else {
        exit('<p>Error adding new recipe: ' . mysql_error() . '</p>');
    }

?>
<?php endif; ?>
</body>
</html>

 

 

Here is "searchrecipe.php" (I don't have problem with this one either. As far as I know)

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php $dbcnx = @mysql_connect('localhost', 'root', '');
if (!$dbcnx) {
  exit('<p>Unable to connect to the ' .
      'database server at this time.</p>');
}

if (!@mysql_select_db('recipetime')) {
  exit('<p>Unable to locate the recipe ' .
      'database at this time.</p>');
}

?>

<form id="form1" name="form1" method="post" action="reciperesults.php">
  <label><strong>SEARCH RECIPE</strong><br />
<br />
</label>
<p>Country<br />
  <label>
    <select name="country" id="country">
      <option>USA</option>
      <option>CANADA</option>
      <option>ENGLAND</option>
    </select>
    </label>
</p>
<p>Type of Food <br />
  <label>
    <select name="typeoffood" id="typeoffood">
      <option>DESERT</option>
      <option>ENTREE</option>
      <option>SPICY</option>
    </select>
  </label>
</p>
<p>
  <label>
  <input type="submit" name="Search" id="Search" value="Search" />
  </label>
</p>
</form>



</body>
</html>

 

 

Now I don't know how to make this file "reciperesult.php"

 

Can anybody help. Thanks a lot.

 

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/128454-solved-php-search-form/
Share on other sites

Heres a VERY basic example of what i think you want..

 

<?php
if(!isset($_POST['submit'])) die("FAILED: nothing selected");
$dbcnx = @mysql_connect('localhost', 'root', '');
if (!$dbcnx)
{
    exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}

if (!@mysql_select_db('recipetime'))
{
    exit('<p>Unable to locate the experience ' . 'database at this time.</p>');
}
$typeoffood = $_POST['typeoffood'];
$country = $_POST['country'];
$sql = "SELECT * FROM recipeform WHERE country='$country' AND typeoffood='typeoffood'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
	echo "name".$row["name"]."<br>";
	echo "country".$row["country"]."<br>";
	echo "typeoffood".$row["typeoffood"]."<br>";
	echo "yourrecipe".$row["yourrecipe"]."<br>";
}
?>

Last question,  when I press search, it just shows one result. It doesn't show all the recipies in that criteria (form example, deserts from USA) ??

There are more than one recipe in that category but it just gives me the first one when I press search. It doesn't give me others.?

 

Last question,  when I press search, it just shows one result. It doesn't show all the recipies in that criteria (form example, deserts from USA) ??

There are more than one recipe in that category but it just gives me the first one when I press search. It doesn't give me others.?

 

 

The code for getting all of the results looks good to me. I even tested it just to make sure I didn't miss a simple mistake but it does work for me (with one of my tables - same concept, different content).

 

PS - You will probably want your results lined up nicely, so I put them into a table that will automatically adjust to the amount of returned rows for you:

 

<?php
   if(!isset($_POST['submit'])) die("FAILED: nothing selected");
   $dbcnx = @mysql_connect('localhost', 'root', '');
   if (!$dbcnx)
   {
       exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
   }

   if (!@mysql_select_db('recipetime'))
   {
       exit('<p>Unable to locate the experience ' . 'database at this time.</p>');
   }
   $typeoffood = $_POST['typeoffood'];
   $country = $_POST['country'];
   $sql = "SELECT * FROM recipeform WHERE country='$country' AND typeoffood='$typeoffood'";
   $result = mysql_query($sql);

if ($result)

{

echo '<table border="0" width="100%"><tr><td width="25%"><b>Name</b></td><td width="25%"><b>Country</b></td><td width="25%"><b>Type</b></td><td width="25%"><b>Recipe</b></td></tr>';

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

        echo '<tr><td width="25%">' . $row['name'] . '</td><td width="25%">' . $row['country'] . '</td><td width="25%">' . $row['type'] . '</td><td width="25%">' . $row['recipe'] . '</td></tr>';

   }

echo '</table>';

}

?>

 

Should work fine unless I made some silly mistake somewhere (should be easy to fix if I did, anyhow).

I solved the problem. Here is the code:

 

<?php


  if(!isset($_POST['Search'])) die("FAILED: nothing selected");
   $dbcnx = @mysql_connect('localhost', 'root', '');
   if (!$dbcnx)
   {
       exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
   }

   if (!@mysql_select_db('recipetime'))
   {
       exit('<p>Unable to locate the recipe' . 'database at this time.</p>');
   }
   $typeoffood = $_POST['typeoffood'];
   $country = $_POST['country'];
   $sql = "SELECT * FROM recipeform WHERE country='$country' AND typeoffood='$typeoffood'";
   $result = mysql_query($sql);
   
   
   
  
   	echo "HERE IS ALL THE $typeoffood RECIPES FROM $country"."<br>"."<br>";   
     
      
      
      //my stuff
        
     while ($rec = mysql_fetch_array($result)) {
      $recname = htmlspecialchars($rec['name']);
      $reccountry = htmlspecialchars($rec['country']);
      $rectypeoffood = htmlspecialchars($rec['typeoffood']);
      $recyourrecipe = htmlspecialchars($rec['yourrecipe']);
      
      
      
		 echo "Name: <td>$recname</td>\n"."<br>";
		 echo "Country: <td>$reccountry</td>\n"."<br>";
		 echo "Typeoffood: <td>$rectypeoffood</td>\n"."<br>";
		 echo "Your Recipe: <td>$recyourrecipe</td>\n"."<br>"."<br>";
	  
  	echo( '<a href="searchrecipe.php">Search Recipe</a>' )."<br>";
	echo( '<a href="submitrecipe.php">Submit Recipe</a>' )."<br>"."<br>";
	  
	  
	  }	   

		 //mystuff   

?>

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.