PHP Login System with Admin Features

This forum was created to talk about the PHP Login System with admin features created by jpmaster77 on evolt's website


    Put array values into SELECT stmt

    Share
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Put array values into SELECT stmt

    Post  mbrad on Sat May 02, 2009 12:31 pm

    Thanks for taking the time to read my question.

    I have a query in session.php that pulls data based on the criteria string I build. This all works fine. The query returns all the data I want. I then fill an array with each rows unique ID.

    Where I'm stuck is trying to get the data out of that array while I'm in my recipe.php page. This is where the search is originating from, and where the criteria comes for the query on session.php page. I need the values of my array to go into a SELECT stmt on the recipe.php page so that the query can return values for the code to populate the recipe.php page with. Right now I get nothing.

    I've put my code below. Any ideas on what I'm doing wrong?

    Thanks,
    Brad

    recipe.php
    Code:

    http://Recipe.php page
    <?php   
    include("MainHeader.php");
    ?>
    <body onLoad="setFocus()">
    <?php   
    include("MainHeader2.php");
    ?>
    <div id="ContentContainer">
    <?php
    /**
     * User has already logged in, so display relavent links, including
     * a link to the admin center if the user is an administrator.
     */
    if($session->logged_in){

          ?>
    <div id="NavContainer" style="width:100%;">
       <ul id="mainmenu">
                    <li><a href="new_recipe.php" onmouseover="MoveIconDown('new_messages')"; onmouseout="MoveIconUp('new_messages')";>New Recipe<img id="new_messages" class="menuicon" src="images/Post-It_Small.png" /></a></li>
                    <li><a href="recipe.php" onmouseover="MoveIconDown('all_messages')"; onmouseout="MoveIconUp('all_messages')";>Recipe<img id="all_messages" class="menuicon" src="images/Post-It_Small.png" /></a></li>
       </ul>
        <h3 class="SubTitle">Recipe(s)</h3>
    </div>
    <a name="top" id="top"></a>
    <div id="SearchContainer" style="width:100%;">
    <p class="SearchIns">Enter all search criteria you would like to search on. If you enter a value in Title as well as in Meal, the search will return results that have both criteria. No results will be found if only one of the criteria is met</p>
    <p class="SearchIns">When searching on Ingredients, enter the first ingredient at the top of the list. If you want to search more than one ingredient, select either the 'And' or the 'Or' option. And will require all criteria are met for one recipe, Or will allow any recipe's with any of the ingredients to be returned.</p>
    <div class="Search">
    <div class="SearchTitle">Title</div>
    <form action="recipe.php#Results" method="POST">
    <table>
    <tr>
    <td><input id="TitleSearch" name="TitleSearch" value="<?php echo $_POST['TitleSearch']; ?>" /></td>
    </tr>
    </table>
    </div>
    <div class="Search">
    <div class="SearchTitle">Ingredients</div>
    <table cellspacing="0" cellpadding="2">
    <tr>
    <td>&nbsp;</td>
    <td><input id="IngredSearch1" name="IngredSearch1" value="<?php echo $_POST['IngredSearch1']; ?>" /></td>
    </tr>
    <tr>
    <td style="border-left: solid 1px #000000; border-top: solid 1px #000000; border-right: solid 1px #000000;"><input id="ITS3And" name="ITS3And" type="radio" value="And" />And</td>
    <td><input id="IngredSearch2" name="IngredSearch2" value="<?php echo $_POST['IngredSearch2']; ?>" /></td>
    </tr>
    <tr>
    <td style="border-left: solid 1px #000000; border-bottom: solid 1px #000000; border-right: solid 1px #000000;"><input id="ITS3Or" name="ITS3Or" type="radio" value="Or" />Or</td>
    <td><input id="IngredSearch3" name="IngredSearch3" value="<?php echo $_POST['IngredSearch3']; ?>" /></td>
    </tr>
    </table>
    </div>
    <div class="Search">
    <div class="SearchTitle">Meal</div>
    <table>
    <tr>
    <td><input id="MealSearch" name="MealSearch" value="<?php echo $_POST['MealSearch']; ?>" /></td>
    </tr>
    </table>
    </div>
    <div class="SearchButton">
    <table>
    <td><input type="hidden" id="subCreateRecipe" name="subTitleSearch" value="1" /></td>
    <td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Search" /></td>
    </table>
    </form>
    </div>
    </div>
    <?php



    echo "<a class=\"SearchResultsHeader\" id=\"Results\" name=\"Results\">Search Results Below</a>";
    echo "<div class=\"SearchResultsCont\">";

    // I need to get the string of tblRecipeID's into the query below so that it has some values to create the table on. HELP!!!!!!!

    $q = "SELECT * FROM `tblrecipe` WHERE `ActiveRecipe`=1 AND `tblRecipeID`=".$session->RecipeSearchResults("tblRecipeID")." GROUP BY `tblRecipeID`;";
    $result = mysql_query($q);

    while ($row = mysql_fetch_assoc($result)) {
       foreach($row as $key => $value) {
          if($key == 'RecipeName') {
          //Make tblBudget table
          echo "<table width=\"800\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><a name=\"".$row['RecipeName']."\">&nbsp;</a>\n";
          echo "<tr>\n";
          echo "<td class=\"ThinBorderBudgetH BudgetName\">Recipe Name</td>\n";
          echo "<td class=\"ThinBorderBudgetH\">Category</td>\n";
          echo "<td class=\"ThinBorderBudgetH\">Sub Category</td>\n";
          echo "<td class=\"ThinBorderBudgetH\">From</td>\n";
          echo "<td class=\"ThinBorderBudgetH\">Date Edited</td>\n";
          echo "<td class=\"ThinBorderBudgetH\">Active</td>\n";
          echo "<td><form action=\"edit_recipe.php\" method=\"POST\"><input type=\"hidden\" name=\"rowIDRecipe\" value=\"" . $row['tblRecipeID']. "\" /><input type=\"submit\" value=\"Edit\" /><span>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#top\" class=\"TopLink\">Top</a></span></form></td>";
          echo "</tr>\n";
          echo "<tr>\n";
          echo "<td class=\"ThinBorderBudget BudgetName\">".$row['RecipeName']."</td>";
          echo "<td class=\"ThinBorderBudget\">".$row['Category']."</td>";
          echo "<td class=\"ThinBorderBudget\">".$row['SubCategory']."</td>";
          echo "<td class=\"ThinBorderBudget\">".$row['Fromm']."</td>";
          echo "<td class=\"ThinBorderBudget\">".$row['DateEdited']."</td>";
          echo "<td class=\"ThinBorderBudget\">".$row['ActiveRecipe']."</td>";
          echo "</tr>\n";
          echo "<tr>\n";
          echo "<td colspan=\"6\" class=\"ThinBorderBudgetH\">Instructions</td>\n";
          echo "</tr>\n";
          echo "<tr>\n";
          echo "<td colspan=\"6\" class=\"ThinBorderBudget\">".nl2br($row['SpecialInstructionsMain'])."</td>";
          echo "</tr>\n";
          echo "</table>\n";
          echo "<br />";
          //Make tblBudgetDetail table wth while and foreach. Need seperate query to generate this. Need tblBudgetID for the where in this query.

          echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n";
          echo "<tr>\n";
          echo "<td class=\"ThinBorderBudgetDetailH\">Ingredients</td>\n";
          echo "<td class=\"ThinBorderBudgetDetailH\">Amount</td>\n";
          echo "<td class=\"ThinBorderBudgetDetailH\">Special Instructions</td>\n";
          echo "</tr>\n";
          
          $q2 = "SELECT `Ingredient`, `Amount`, `SpecialInstructions` FROM qryrecipeinfo WHERE `RecipeID`=".$session->RecipeSearchResults("tblRecipeID").";";
          $result2 = mysql_query($q2);
             while ($row2 = mysql_fetch_assoc($result2)) {
                echo "<tr>\n";
                foreach($row2 as $key2 => $value2) {
                if ($key2<>'RecipeID'){
                //nl2br("Welcome\r\nThis is my HTML document", false);
                   if ($key2=='SpecialInstructions'){
                      echo "<td class=\"ThinBorderBudgetDetail\">";
                      echo nl2br($value2);
                      echo "</td>\n";
                   }
                   else {
                      echo "<td class=\"ThinBorderBudgetDetail\">";
                      echo $value2;
                      echo "</td>\n";
                   }
                }
                }
                echo "</tr>\n";
             }
          echo "</table>\n<br /><hr /><br />";            
          }
       }
    }
    ?>
    </div>
    <?php
    //}
    //else {
    //   echo "<h1>No search results found</h1>";
    //}
    echo "</div>";

    }
    else{
    if($form->num_errors > 0){
      echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
    }
    }
    ?>
    </div>
    </div>
    </body>
    </html>
    relevant part from session.php
    Code:

    //at the top of session.php under the rest of the var's that are defined
    var $RecipeSearchResults = array();

    //My function
      //return the results of the recipe search based on Recipe Name 
      function RecipeSearchResults($Name) {
      global $database;
      //$q = "Select `tblRecipeID` FROM `tblrecipe` WHERE `RecipeName` LIKE '%" .$_POST['TitleSearch']. "%';";
     
      $TheSearchString = "";
      $TitleSearch = $_POST['TitleSearch'];
      $IngredSearch1 = $_POST['IngredSearch1'];
      $IngredSearch2 = $_POST['IngredSearch2'];
      $IngredSearch3 = $_POST['IngredSearch3'];
      $MealSearch = $_POST['MealSearch'];
      $IngredSearchAll = "";
     
    //Find out which Ingredients fields have values in them and add the values to the search string
       if ($IngredSearch1 <> "" ){
          $IngredSearchAll = "`Ingredient` Like '%" . $IngredSearch1 . "%'";
       }
       
       if ($IngredSearchAll == ""){
          if ($IngredSearch2 <> "" ){
             $IngredSearchAll = "`Ingredient` Like '%" . $IngredSearch2 . "%'";
          }
       }
       else {
          if ($IngredSearch2 <> "" ){
             $IngredSearchAll = $IngredSearchAll . " OR `Ingredient` Like '%" . $IngredSearch2 . "%'";
          }
       }
       
       if ($IngredSearchAll == ""){
          if ($IngredSearch3 <> "" ){
             $IngredSearchAll = "`Ingredient` Like '%" . $IngredSearch3 . "%'";
          }
       }
       else {
          if ($IngredSearch3 <> "" ){
             $IngredSearchAll = $IngredSearchAll . " OR `Ingredient` Like '%" . $IngredSearch3 . "%'";
          }
       }

    //Find out if the Title has a value in it, and if it does add it to the search string
       if ($TheSearchString == "") {
          if ($TitleSearch <> "" ) {
             $TheSearchString = "`RecipeName` LIKE '%" . $TitleSearch . "%'";
          }
       }
       
    //Find out if the Meal has a value in it, and if it does add it to the search string
       if ($TheSearchString == "") {
          if ($MealSearch <> "" ) {
             $TheSearchString = "`Category` LIKE '%" . $MealSearch . "%'";
          }
       }
       else {
          if ($MealSearch <> "" ) {
             $TheSearchString = $TheSearchString . " AND `Category` LIKE '%" . $MealSearch . "%'";
          }
       }

    //add the Ingredient string to the search string
       if ($TheSearchString == "") {
          if ($IngredSearchAll <> "" ) {
             $TheSearchString = $IngredSearchAll;
          }
       }
       else {
          if ($IngredSearchAll <> "" ) {
             $TheSearchString = $TheSearchString . " AND " . $IngredSearchAll . "%'";
          }
       }
       
       if ($TheSearchString <> "") {
          echo "<script>alert('".$TheSearchString."');</script>";
       }
       
      $q = "SELECT `tblRecipeID` FROM `qryrecipesearch` WHERE " . $TheSearchString .";";
      $result = $database->query($q);
      $rows = array();
      while ($row = mysql_fetch_assoc($result)) {
         $rows[] = $row;
      }
      $this->RecipeSearchResults = $rows;
      return $this->RecipeSearchResults["$Name"];
     
      //$this->RecipeSearchResults = mysql_fetch_array($result);
      //return $this->RecipeSearchResults["$Name"];
      }
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: Put array values into SELECT stmt

    Post  Linchpin311 on Mon May 04, 2009 10:12 pm

    what exactly is happening? are you getting an error or are your values just not being displayed? are you running from a live server or a local one? have you tried running your query through phpMyAdmin to see if it should be returning any values at all?

    ...i looked quickly through your code and didnt see anything that stood out. if you can give me a little more information maybe we can figure out where the error is occurring.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Re: Put array values into SELECT stmt

    Post  mbrad on Tue May 05, 2009 9:23 am

    Thanks so much for the reply Linchpin311.

    What is happening is that my code in session.php, when I use print_r($rows) prints out the results of the new array, with all the values exactly right.

    This is what print_r($rows) spits out on the recipe.php page:
    Array ( [0] => Array ( [tblRecipeID] => 9 ) [1] => Array ( [tblRecipeID] => 9 ) [2] => Array ( [tblRecipeID] => 9 ) [3] => Array ( [tblRecipeID] => 9 ) [4] => Array ( [tblRecipeID] => 9 ) [5] => Array ( [tblRecipeID] => 9 ) [6] => Array ( [tblRecipeID] => 9 ) [7] => Array ( [tblRecipeID] => 9 ) [8] => Array ( [tblRecipeID] => 9 ) [9] => Array ( [tblRecipeID] => 9 ) [10] => Array ( [tblRecipeID] => 9 ) [11] => Array ( [tblRecipeID] => 9 ) [12] => Array ( [tblRecipeID] => 11 ) [13] => Array ( [tblRecipeID] => 11 ) [14] => Array ( [tblRecipeID] => 11 ) [15] => Array ( [tblRecipeID] => 11 ) [16] => Array ( [tblRecipeID] => 14 ) [17] => Array ( [tblRecipeID] => 14 ) [18] => Array ( [tblRecipeID] => 14 ) [19] => Array ( [tblRecipeID] => 14 ) [20] => Array ( [tblRecipeID] => 14 ) [21] => Array ( [tblRecipeID] => 14 ) [22] => Array ( [tblRecipeID] => 14 ) [23] => Array ( [tblRecipeID] => 14 ) [24] => Array ( [tblRecipeID] => 14 ) [25] => Array ( [tblRecipeID] => 14 ) )

    What is happening is if I comment out the array part of the code at the bottom and uncomment these two lines, I get one result in my recipe.php page

    Uncomment these two lines:
    Code:
      //$this->RecipeSearchResults = mysql_fetch_array($result);
      //return $this->RecipeSearchResults["$Name"];

    Comment out these:
    Code:
    $rows = array();
      while ($row = mysql_fetch_assoc($result)) {
          $rows[] = $row;
      }
      $this->RecipeSearchResults = $rows;
      return $this->RecipeSearchResults["$Name"];

    But when I leave the code as posted, I get no results displayed. It seems that I am unable to pull the results out of the array $session->RecipeSearchResults when I'm back on the results.php page.

    The page is on a local server.

    No Errors (but I don't have any sort of error reporting code in place)

    All queries I use in my pages I test in phpMyAdmin first, as I'm not that great at queries yet. So yes, they have been tested and they return what I'm expecting them to return.

    I could send you the required files if you like as well as the DB. It's not big at all, just a bit of test data.


    Thanks,
    Brad
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: Put array values into SELECT stmt

    Post  Linchpin311 on Wed May 06, 2009 4:58 pm

    yea, if you could send me a PM with your exported database that would be great.

    i have a feeling you may have a problem with your query on line 77 of recipe.php. im not sure if the $session->RecipeSearchResults() function knows what to do with its argument.


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Re: Put array values into SELECT stmt

    Post  mbrad on Wed May 06, 2009 4:59 pm

    Will do

    Thanks!!

    Brad
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: Put array values into SELECT stmt

    Post  Linchpin311 on Thu May 07, 2009 5:40 pm

    alright, well i think i have it working here.

    i started with your function in session.php and i removed the argument to make the first line of the function look like function RecipeSearchResults() {. then i just went to the (almost) last line of the funcion where you return the value and changed it to return $this->RecipeSearchResults;. i removed the argument from this function because without running an additional query there really isnt any way to pass the value you wanted to pass to this function. thats it for session.php

    then i went into recipe.php and altered a few things. first, i took $session->RecipeSearchResults and removed the argument from that. we just removed the ability for this function to take an argument so leaving it here is pointless. then i actually removed the $session->RecipeSearchResults() function from the queries you had it in and put it in a variable above the query just to make things a little more linear. now this is where things take off... then i pretty much put the whole thing in a for loop so it could process each tblRecipeID seperately. the for loop looks like this:for($i = 0; $i < count($tblRecipeID); $i++){. this creates a loop from 0 to however many tblRecipeID's were returned in the search. inside the for loop i changed your next two queries to $q = "SELECT * FROM `tblrecipe` WHERE `ActiveRecipe`=1 AND `tblRecipeID`=" . $tblRecipeID[$i]['tblRecipeID'] . " GROUP BY `tblRecipeID`;"; and $q2 = "SELECT `Ingredient`, `Amount`, `SpecialInstructions` FROM qryrecipeinfo WHERE `RecipeID`=" . $tblRecipeID[$i]['tblRecipeID'] . ";";, respectively. this seems to work for me!

    take a look at this snippet from recipe.php:
    Code:
    <?php



    echo "<a class=\"SearchResultsHeader\" id=\"Results\" name=\"Results\">Search Results Below</a>";
    echo "<div class=\"SearchResultsCont\">";

    // I need to get the string of tblRecipeID's into the query below so that it has some values to create the table on. HELP!!!!!!!

    $tblRecipeID = $session->RecipeSearchResults();

    for($i = 0; $i < count($tblRecipeID); $i++){

       $q = "SELECT * FROM `tblrecipe` WHERE `ActiveRecipe`=1 AND `tblRecipeID`=" . $tblRecipeID[$i]['tblRecipeID'] . " GROUP BY `tblRecipeID`;";
       $result = mysql_query($q);
       
       while ($row = mysql_fetch_assoc($result)) {
          foreach($row as $key => $value) {
             if($key == 'RecipeName') {
             //Make tblBudget table
             echo "<table width=\"800\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><a name=\"".$row['RecipeName']."\">&nbsp;</a>\n";
             echo "<tr>\n";
             echo "<td class=\"ThinBorderBudgetH BudgetName\">Recipe Name</td>\n";
             echo "<td class=\"ThinBorderBudgetH\">Category</td>\n";
             echo "<td class=\"ThinBorderBudgetH\">Sub Category</td>\n";
             echo "<td class=\"ThinBorderBudgetH\">From</td>\n";
             echo "<td class=\"ThinBorderBudgetH\">Date Edited</td>\n";
             echo "<td class=\"ThinBorderBudgetH\">Active</td>\n";
             echo "<td><form action=\"edit_recipe.php\" method=\"POST\"><input type=\"hidden\" name=\"rowIDRecipe\" value=\"" . $row['tblRecipeID']. "\" /><input type=\"submit\" value=\"Edit\" /><span>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#top\" class=\"TopLink\">Top</a></span></form></td>";
             echo "</tr>\n";
             echo "<tr>\n";
             echo "<td class=\"ThinBorderBudget BudgetName\">".$row['RecipeName']."</td>";
             echo "<td class=\"ThinBorderBudget\">".$row['Category']."</td>";
             echo "<td class=\"ThinBorderBudget\">".$row['SubCategory']."</td>";
             echo "<td class=\"ThinBorderBudget\">".$row['Fromm']."</td>";
             echo "<td class=\"ThinBorderBudget\">".$row['DateEdited']."</td>";
             echo "<td class=\"ThinBorderBudget\">".$row['ActiveRecipe']."</td>";
             echo "</tr>\n";
             echo "<tr>\n";
             echo "<td colspan=\"6\" class=\"ThinBorderBudgetH\">Instructions</td>\n";
             echo "</tr>\n";
             echo "<tr>\n";
             echo "<td colspan=\"6\" class=\"ThinBorderBudget\">".nl2br($row['SpecialInstructionsMain'])."</td>";
             echo "</tr>\n";
             echo "</table>\n";
             echo "<br />";
             //Make tblBudgetDetail table wth while and foreach. Need seperate query to generate this. Need tblBudgetID for the where in this query.
       
             echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n";
             echo "<tr>\n";
             echo "<td class=\"ThinBorderBudgetDetailH\">Ingredients</td>\n";
             echo "<td class=\"ThinBorderBudgetDetailH\">Amount</td>\n";
             echo "<td class=\"ThinBorderBudgetDetailH\">Special Instructions</td>\n";
             echo "</tr>\n";
             
             $q2 = "SELECT `Ingredient`, `Amount`, `SpecialInstructions` FROM qryrecipeinfo WHERE `RecipeID`=" . $tblRecipeID[$i]['tblRecipeID'] . ";";
             $result2 = mysql_query($q2);
                while ($row2 = mysql_fetch_assoc($result2)) {
                   echo "<tr>\n";
                   foreach($row2 as $key2 => $value2) {
                   if ($key2<>'RecipeID'){
                   //nl2br("Welcome\r\nThis is my HTML document", false);
                      if ($key2=='SpecialInstructions'){
                         echo "<td class=\"ThinBorderBudgetDetail\">";
                         echo nl2br($value2);
                         echo "</td>\n";
                      }
                      else {
                         echo "<td class=\"ThinBorderBudgetDetail\">";
                         echo $value2;
                         echo "</td>\n";
                      }
                   }
                   }
                   echo "</tr>\n";
                }
             echo "</table>\n<br /><hr /><br />";            
             }
          }
       }
    }
    ?>


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Re: Put array values into SELECT stmt

    Post  mbrad on Fri May 08, 2009 9:35 am

    Sheer Genius!!

    The only changes I had to make were

    1. I added a GROUP BY to my VIEW in MySQL db, that way I only report on one recipe, not on a recipe for each ingredient.

    Thank you so very much Linchpin311. I will study the code and learn from it.

    Have a great day,

    mbrad
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Re: Put array values into SELECT stmt

    Post  mbrad on Fri May 08, 2009 10:02 am

    OK, actually that didn't work... my change I mean.

    What does work is to add GROUP BY `tblRecipeID` to $q in session.php

    Code:
    $q = "SELECT `tblRecipeID` FROM `qryrecipesearch` WHERE ".$TheSearchString." GROUP BY `tblRecipeID`;";

    mbrad
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: Put array values into SELECT stmt

    Post  Linchpin311 on Fri May 08, 2009 4:51 pm

    so its all working 100% for you now?


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Re: Put array values into SELECT stmt

    Post  mbrad on Fri May 08, 2009 5:00 pm

    You bet. You're awesome. I'm going through the code trying to understand what I did wrong.

    I'm also trying to get my page edit_recipe.php to update the recipe. I used your code from the search and applied it to my new edit_recipe.php page, and got that working. Now I'm just trying to make the actual UPDATE work (it's in process.php). For some reason the UPDATE part is not working, but the rest does

    I'm in process.php in a new function, can I do

    $_POST['TitleSearch'] = $_POST['RecipeName'];

    to set $_POST['TitleSearch']?

    Brad
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: Put array values into SELECT stmt

    Post  Linchpin311 on Fri May 08, 2009 7:12 pm

    I think you can... But POST doesn't register till a page is submitted. Meaning that you won't have access to it till the next page is loaded

    in your code, where you tell php to UPDATE the database instead of saying mysql_query($q); (or whatever..) type echo $q; so we can see what the query actually looks like. hopefully theres just a simple error in the query. you could also use the tried and true mysql_query($q) or die(mysql_error()); method. this will kill your script and display any mysql error messages if any mysql errors are detected...

    im kinda banking on that this is a query issue and nothing more complicated, but if it its not we'll have to take a deeper look at your code


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Re: Put array values into SELECT stmt

    Post  mbrad on Sun May 10, 2009 9:13 pm

    Thanks for the reply.

    I made both the queries in phpMyAdmin, and then substituted $_POST[''] for the actual values I used in the phpMyAdmin query.

    I've attached the code for my edit_recipe.php page and the function from my process.php page.

    edit_recipe.php
    Code:

    <?php   
    include("MainHeader.php");
    ?>
    <body>
    <?php   
    include("MainHeader2.php");
    ?>
    <div id="ContentContainer">
    <?php
    /**
     * User has already logged in, so display relavent links, including
     * a link to the admin center if the user is an administrator.
     */
     //$session->RecipeInfoDetail("RecipeID")
    if($session->logged_in){
    ?>

    <div id="NavContainer" style="width:100%;">
       <ul id="mainmenu">
                    <li><a href="new_recipe.php" onmouseover="MoveIconDown('new_messages')"; onmouseout="MoveIconUp('new_messages')";>New Recipe<img id="new_messages" class="menuicon" src="images/Post-It_Small.png" /></a></li>
                    <li><a href="recipe.php" onmouseover="MoveIconDown('all_messages')"; onmouseout="MoveIconUp('all_messages')";>Recipe<img id="all_messages" class="menuicon" src="images/Post-It_Small.png" /></a></li>
       </ul>
        <h3 class="SubTitle">Edit Recipe</h3>
    </div>
    <table border="0" cellspacing="0" cellpadding="3" id="NewMessage">
    <tr>
    <td>
    <form action="process.php" method="POST">
        <table>
        <caption>Please enter information in all fields below.</caption>
       <tr>
        <td class="title">Recipe Name</td>
        <td class="title">Category</td>
        <td class="title">Sub Category</td>
        <td class="title">From</td>
        </tr>   
         <tr>
        <td><input class="keyboardInput" id="RecipeName" name="RecipeName" value="<?php echo $session->RecipeInfo("RecipeName"); ?>" /></td>
        <td><select id="Category" name="Category"><option value="sel">Select</option><option value="Breakfast">Breakfast</option><option value="Lunch">Lunch</option><option value="Supper">Supper</option><option value="Dessert">Dessert</option><option value="Treats">Treats</option></select></td>
    <td><select id="SubCategory" name="SubCategory"><option value="sel">Select</option><option value="None">None</option></select></td>
        <td><input class="keyboardInput" id="Fromm" name="Fromm" value="<?php echo $session->RecipeInfo("Fromm"); ?>" /></td>
        </tr>
        <tr>
        <tr>
        <td colspan="4" class="title">Special Instruction</td>
        </tr>
        <td colspan="4"><textarea class="keyboardInput" rows="3" cols="60" id="SpecInsMain" name="SpecInsMain"><?php echo $session->RecipeInfo("SpecialInstructionsMain"); ?></textarea><input type="hidden" class="keyboardInput" id="tblRecipeID" name="tblRecipeID" value="<?php echo $session->RecipeInfo("tblRecipeID"); ?>" /</td>
        </tr>
        </table>
       
        <table>
       <tr>
        <td class="title">Recipe Line</td>
        <td class="title">Ingredient</td>
        <td class="title">Amount</td>
        <td class="title">Special Instruction</td>
        </tr>

       <?php
          $tblRecipeDetailID = $session->RecipeInfoDetail();
          $num=0;
          $numb=1;
          for($i = 0; $i < count($tblRecipeDetailID); $i++){
             echo "<tr>\n";
             echo "<td>Line ".$numb."</td>\n";
             echo "<td>";
             echo "<input class=\"keyboardInput\" id=\"NewIngred" . $numb . "\" name=\"NewIngred" . $numb . "\" value=\"". $tblRecipeDetailID[$i]['Ingredient'] ."\" />";
             echo "</td>\n";
             echo "<td>";
             echo "<input class=\"keyboardInput\" id=\"NewAmt" . $numb . "\" name=\"NewAmt" . $numb . "\" value=\"".$tblRecipeDetailID[$i]['Amount']."\" />";
             echo "</td>\n";
             echo "<td>";
             echo "<textarea class=\"keyboardInput\" rows=\"2\" cols=\"40\" id=\"NewSpecIns" . $numb . "\" name=\"NewSpecIns" . $numb . "\">".$tblRecipeDetailID[$i]['SpecialInstructions']."</textarea><input type=\"hidden\" name=\"tblRecipeDetailID\" id=\"tblRecipeDetailID\" value=\"".$tblRecipeDetailID[$i]['tblRecipeDetailID']."\" />";
             echo "</td>\n";
             echo "</tr>\n";
             $num++;
             $numb++;
          }
        ?>
            <tr>
                <td><input class="keyboardInput" type="hidden" id="subEditRecipe" name="subEditRecipe" value="1" /><input class="keyboardInput" type="hidden" name="TheNumRows" id="TheNumRows" value="<?php echo $num; ?>" /></td>
                <td>&nbsp;</td><td>&nbsp;</td><td><input class="keyboardInput" type="submit" value="Save Changes" /></td>
            </tr>
        </table>
    </form>
    </td>
    </tr>
    </table>
    </div>
    <?php
    }
    else{
    if($form->num_errors > 0){
      echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
    }
    }
    ?>
    </div>
    </div>
    </body>
    </html>

    process.php function
    Code:

         /* User submitted Make new Recipe Request */    //***************************Added This*********************************
          else if(isset($_POST['subEditRecipe'])){
           $this->procEditRecipe();
          }

       // This is the code that updates the recipe data in the database.
       function procEditRecipe() {
       global $database, $session, $form;
       /*echo "<script>alert('HI')</script>";*/
       $q1 = "UPDATE `tblrecipe` SET `RecipeName`='". $_POST['RecipeName']."', `Category`='". $_POST['Category']."', `SubCategory`='". $_POST['SubCategory']."', `Fromm`='". $_POST['Fromm']."', `SpecialInstructionsMain`='". $_POST['SpecInsMain']."', `DateEdited`='".date('Y-m-d')."', `ActiveRecipe`='1' WHERE `tblRecipeID`='".$_POST['tblRecipeID']."';";
       $retval = $database->query($q1);

       $RecipeName = $_POST['RecipeName'];
       $TheNumRows = $_POST['TheNumRows'];
       echo "<script>alert('HI');</script>";
       //$UID = mysql_insert_id();
       
       $num=0;
       $numb=1;
       
       //$TheValues=1;
       do {
          $q2 = "UPDATE `testlogin`.`tblrecipedetail` SET `Ingredient` = '".$_POST['NewIngred'.$numb]."', `Amount` = '".$_POST['NewAmt'.$numb]."', `SpecialInstructions` = '".$_POST['NewSpecIns'.$numb]."', `DateEdited` = '".date('Y-m-d')."' WHERE `tblrecipedetail`.`tblRecipeDetailID` = ".$_POST['tblRecipeDetailID']." LIMIT 1;";
          $retval2 = $database->query($q2);      
          $num++;
          $numb++;
       } while ($num < $TheNumRows);

         $_POST['TitleSearch'] = $_POST['RecipeName'];
       //print_r($_POST['TitleSearch']);
       //print_r("HI");
       

       header("Location: recipe.php#Results");

       }

    $q1 works, but $q2 doesn't. $q2 works in phpMyAdmin, but with value in it. Not sure if I have to form the query differently for it to work. I used the same function form when I add a NEW recipe to the database.

    Thanks again,

    Brad
    avatar
    Linchpin311

    Number of posts : 220
    Age : 32
    Localisation : Long Island
    Registration date : 2007-05-14

    Re: Put array values into SELECT stmt

    Post  Linchpin311 on Tue May 12, 2009 12:18 am

    in edit_recipe.php on line 38 you call a function from session.php but i dont see it in my version of session.php. are you using this in your version or are you trying to run everything out of process.php?


    _________________
    in the end, the universe tends to unfold as it should...

    Also note that it is your responsibility to die() if necessary.
    avatar
    mbrad

    Number of posts : 42
    Registration date : 2009-02-19

    Re: Put array values into SELECT stmt

    Post  mbrad on Tue May 12, 2009 11:38 am

    Sorry, I forgot to add that info to the post.

    session.php code
    Code:
      var $RecipeInfo = array();     //Contact Information  //*************Added this************************
      var $RecipeInfoDetail = array();     //Contact Information  //*************Added this************************
      var $NumMessages; //*************Added this************************

    ==========================================================

      // This should get the info into edit_recipe.php    //***************************Added  This*********************************
      function RecipeInfo($ID){
         global $database;  //The database connection 
         $this->RecipeInfo = $database->getRecipeInfo($_POST['rowIDRecipe']);
           return $this->RecipeInfo["$ID"];
      } 

      // This should get the info into edit_recipe.php    //***************************Added  This*********************************
          function RecipeInfoDetail() {
         global $database;
         $q = "SELECT * FROM `tblrecipedetail` WHERE `RecipeID`=".$_POST['rowIDRecipe'];
          $result = $database->query($q);
         $rows = array();
         while ($row = mysql_fetch_assoc($result)) {
            $rows[] = $row;
         }
         $this->RecipeInfoDetail = $rows;
         return $this->RecipeInfoDetail;
        
      } 
     
     
      function ReturnNumNewMessages() {
      global $database;
      $q = "SELECT Count(`tblMessageID`) AS NumNewMessages FROM `tblmessages` WHERE `MessageFor`='Braddm' AND `MessageRead` = '0';";
      $retval = $database->query($q);
      //return $this->NumMessages = mysql_fetch_assoc($retval);
      return $this->NumMessages = 5;

      }

    Sponsored content

    Re: Put array values into SELECT stmt

    Post  Sponsored content


      Current date/time is Sun Oct 22, 2017 4:49 pm