Need Help With Printing Problem

Discussion in 'PHP' started by WD1812, Apr 9, 2010.

  1. #1
    Hi,

    I'm very much new to php/mysql, and hoping someone can help me straighten this out. I'm working w/Dreamweaver CS4.

    I have two tables.

    1. recipes -

    recipe_id (Set as a primary key, auto increment)
    Title
    Ingredients
    Prep
    Serves

    2. categories -

    recipe_id (Set as a primary key, auto increment)
    category_name

    Two pages I'm working with right now are desserts.php, and recipesdesserts.php.

    Here's the scenario:

    User 1 enters the following into a form on the Desserts Page:

    Title: Apple Strudel
    Ingredients: Apples, Flour, Water
    Prep: Core and slice apples, etc.
    Serves: 6

    User 2 enters the following into the same form:

    Title: Blueberry Pie
    Ingredients: Blueberries, Flour, Water
    Prep: Wash blueberries, etc.
    Serves: 6

    Both titles now appear on the Desserts page, which is what I want. When User 3 clicks on either Apple Strudel or Blueberry Pie, the ending of the URL shows the corresponding recipe id # and the title. But that's it. The recipes are not showing, and I have not been able to figure out why. What am I doing wrong? And just to clarify. I cannot put any recipe names into the database itself, as I have no idea what a user is going to enter.

    This is the Php code that appears at the beginning of desserts.php:

    <?php require_once('../../Connections/clan_db.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    
    $currentPage = $_SERVER["PHP_SELF"];
    
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO Recipes (Title, Ingredients, Prep, Serves) VALUES (%s, %s, %s, %s)",
                           GetSQLValueString($_POST['Title'], "text"),
                           GetSQLValueString($_POST['Ingredients'], "text"),
                           GetSQLValueString($_POST['Prep'], "text"),
                           GetSQLValueString($_POST['Serves'], "text"));
    
      mysql_select_db($database_clan_db, $clan_db);
      $Result1 = mysql_query($insertSQL, $clan_db) or die(mysql_error());
    
      $insertGoTo = "../Recipes/recipesdesserts.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    
    mysql_select_db($database_clan_db, $clan_db);
    $query_GetRecipes = "SELECT recipes.recipe_id, recipes.Title FROM recipes";
    $GetRecipes = mysql_query($query_GetRecipes, $clan_db) or die(mysql_error());
    $row_GetRecipes = mysql_fetch_assoc($GetRecipes);
    $totalRows_GetRecipes = mysql_num_rows($GetRecipes);$colname_GetRecipes = "-1";
    if (isset($_GET['recipe_id'])) {
      $colname_GetRecipes = $_GET['recipe_id'];
    }
    mysql_select_db($database_clan_db, $clan_db);
    $query_GetRecipes = sprintf("SELECT * FROM recipes WHERE recipe_id = %s", GetSQLValueString($colname_GetRecipes, "int"));
    $GetRecipes = mysql_query($query_GetRecipes, $clan_db) or die(mysql_error());
    $row_GetRecipes = mysql_fetch_assoc($GetRecipes);
    $totalRows_GetRecipes = mysql_num_rows($GetRecipes);
    
    
    $maxRows_GetRecipes = 10;
    $pageNum_GetRecipes = 0;
    if (isset($_GET['pageNum_GetRecipes'])) {
      $pageNum_GetRecipes = $_GET['pageNum_GetRecipes'];
    }
    $startRow_GetRecipes = $pageNum_GetRecipes * $maxRows_GetRecipes;
    
    mysql_select_db($database_clan_db, $clan_db);
    $query_GetRecipes = "SELECT * FROM Recipes"; 
    $query_limit_GetRecipes = sprintf("%s LIMIT %d, %d", $query_GetRecipes, $startRow_GetRecipes, $maxRows_GetRecipes);
    $GetRecipes = mysql_query($query_limit_GetRecipes, $clan_db) or die(mysql_error());
    $row_GetRecipes = mysql_fetch_assoc($GetRecipes);
    
    if (isset($_GET['totalRows_GetRecipes'])) {
      $totalRows_GetRecipes = $_GET['totalRows_GetRecipes'];
    } else {
      $all_GetRecipes = mysql_query($query_GetRecipes);
      $totalRows_GetRecipes = mysql_num_rows($all_GetRecipes);
    }
    $totalPages_GetRecipes = ceil($totalRows_GetRecipes/$maxRows_GetRecipes)-1;
    
    $queryString_GetRecipes = "";
    if (!empty($_SERVER['QUERY_STRING'])) {
      $params = explode("&", $_SERVER['QUERY_STRING']);
      $newParams = array();
      foreach ($params as $param) {
        if (stristr($param, "pageNum_GetRecipes") == false && 
            stristr($param, "totalRows_GetRecipes") == false) {
          array_push($newParams, $param);
        }
      }
      if (count($newParams) != 0) {
        $queryString_GetRecipes = "&" . htmlentities(implode("&", $newParams));
      }
    }
    $queryString_GetRecipes = sprintf("&totalRows_GetRecipes=%d%s", $totalRows_GetRecipes, $queryString_GetRecipes);
    ?>
    PHP:
    This is the code in the body of desserts.php:
    
    <ol>
    <?php do { ?>
    <li><a href="../Recipes/recipesdesserts.php?recipeID=<?php echo $row_GetRecipes['recipe_id']; ?><?php echo $row_GetRecipes['Title']; ?>"><strong><?php echo $row_GetRecipes['Title']; ?></strong></a></li>
    <?php } while ($row_GetRecipes = mysql_fetch_assoc($GetRecipes)); ?>
    </ol>
    PHP:
    This is the code that appears at the beginning of recipesdesserts.php:

    <?php require_once('../../Connections/clan_db.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
    if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
    }
    
    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break; 
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }
    
    $currentPage = $_SERVER["PHP_SELF"];
    
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $insertSQL = sprintf("INSERT INTO Recipes (Title, Ingredients, Prep, Serves) VALUES (%s, %s, %s, %s)",
    GetSQLValueString($_POST['Title'], "text"),
    GetSQLValueString($_POST['Ingredients'], "text"),
    GetSQLValueString($_POST['Prep'], "text"),
    GetSQLValueString($_POST['Serves'], "text"));
    
    mysql_select_db($database_clan_db, $clan_db);
    $Result1 = mysql_query($insertSQL, $clan_db) or die(mysql_error());
    
    $insertGoTo = "recipesdesserts.php";
    if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $insertGoTo));
    }
    
    $maxRows_GetRecipes = 10;
    $pageNum_GetRecipes = 0;
    if (isset($_GET['pageNum_GetRecipes'])) {
      $pageNum_GetRecipes = $_GET['pageNum_GetRecipes'];
    }
    $startRow_GetRecipes = $pageNum_GetRecipes * $maxRows_GetRecipes;
    
    $colname_GetRecipes = "-1";
    if (isset($_GET['recipe_id'])) {
      $colname_GetRecipes = $_GET['recipe_id'];
    }
    mysql_select_db($database_clan_db, $clan_db);
    $query_GetRecipes = sprintf("SELECT * FROM recipes WHERE recipe_id = %s", GetSQLValueString($colname_GetRecipes, "int"));
    $query_limit_GetRecipes = sprintf("%s LIMIT %d, %d", $query_GetRecipes, $startRow_GetRecipes, $maxRows_GetRecipes);
    $GetRecipes = mysql_query($query_limit_GetRecipes, $clan_db) or die(mysql_error());
    $row_GetRecipes = mysql_fetch_assoc($GetRecipes);
    
    if (isset($_GET['totalRows_GetRecipes'])) {
      $totalRows_GetRecipes = $_GET['totalRows_GetRecipes'];
    } else {
      $all_GetRecipes = mysql_query($query_GetRecipes);
      $totalRows_GetRecipes = mysql_num_rows($all_GetRecipes);
    }
    $totalPages_GetRecipes = ceil($totalRows_GetRecipes/$maxRows_GetRecipes)-1;
    
    $queryString_GetRecipes = "";
    if (!empty($_SERVER['QUERY_STRING'])) {
    $params = explode("&", $_SERVER['QUERY_STRING']);
    $newParams = array();
    foreach ($params as $param) {
    if (stristr($param, "pageNum_GetRecipes") == false && 
    stristr($param, "totalRows_GetRecipes") == false) {
    array_push($newParams, $param);
    }
    }
    if (count($newParams) != 0) {
    $queryString_GetRecipes = "&" . htmlentities(implode("&", $newParams));
    }
    }
    $queryString_GetRecipes = sprintf("&totalRows_GetRecipes=%d%s", $totalRows_GetRecipes, $queryString_GetRecipes);
    ?>
    PHP:
    This is the code in the body of recipesdesserts.php:

    <div id="titlecontainer">
      <?php echo $row_GetRecipes['Title']; ?></div>
         
         <div id="ingredientscontainer"><?php echo $row_GetRecipes['Ingredients']; ?></div>
         
         <div id="prepcontainer">
    	 <?php echo $row_GetRecipes['Prep']; ?></div>
         
        <div id="servescontainer">
    	 <?php echo $row_GetRecipes['Serves']; ?></div>
    
    PHP:
    Thank you!
     
    Last edited: Apr 9, 2010
    WD1812, Apr 9, 2010 IP