hakmir Posted October 14, 2008 Share Posted October 14, 2008 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 ). 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] Quote Link to comment Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
hakmir Posted October 15, 2008 Author Share Posted October 15, 2008 Thanks a lot for the answer. I really appreciate. But when I use the code that you gave me it says: FAILED: nothing selected I don't understand. Obviously it doesn't get the values from "searchrecipe.php" page. (id=country and id=typeoffood) Quote Link to comment Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 opps, change if(!isset($_POST['submit'])) die("FAILED: nothing selected"); to if(!isset($_POST['Search'])) die("FAILED: nothing selected"); Quote Link to comment Share on other sites More sharing options...
hakmir Posted October 15, 2008 Author Share Posted October 15, 2008 Awesome. It works perfect. I just had to correct the part that you forgot to put "$" sign to "typeoffood". Thanks a lot. Do you have a paypal account that I can send money. It is not much but just to show my appreciation. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted October 15, 2008 Share Posted October 15, 2008 I do but a thanx will do I'm happy to help Quote Link to comment Share on other sites More sharing options...
hakmir Posted October 15, 2008 Author Share Posted October 15, 2008 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.? Quote Link to comment Share on other sites More sharing options...
hakmir Posted October 15, 2008 Author Share Posted October 15, 2008 That's ok, I will just work on it. Thanks a lot. After I finish this project I will prepare an educational file to teach people (who are very new to php like me) how to make a simple php mysql form from A to Z. Thanks again. Quote Link to comment Share on other sites More sharing options...
Andy17 Posted October 15, 2008 Share Posted October 15, 2008 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). Quote Link to comment Share on other sites More sharing options...
hakmir Posted October 15, 2008 Author Share Posted October 15, 2008 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 ?> Quote Link to comment Share on other sites More sharing options...
hakmir Posted October 15, 2008 Author Share Posted October 15, 2008 Belive me I almost gave up learning php. But now I finished my first project with your help. Thanks a lot. Quote Link to comment Share on other sites More sharing options...
Andy17 Posted October 15, 2008 Share Posted October 15, 2008 Looks to me as if you have some syntax errors in there (in your echos), but if you say it works for ya... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.