数据库搜索引擎无法正确更新$ _SESSION

时间:2013-12-06 14:55:28

标签: php html mysql session

我被分配了一个任务来创建一个搜索引擎和一组人的布局,并与另一个人合作创建搜索引擎本身,作为一个学校项目。我们正在使用一个包含多个表和大量内容的数据库。

对于这个问题,我们两个人正在使用$ _SESSION变量(从此处开始只调用会话)来保存html表单中的帖子数据。基本上,第一次搜索将显示一个目录,其中一列拥有锚标签以进一步进入表格(基本上首先收到蓝图列表和超链接,通向包含所有所需部分的表格或类似的东西。)

在这种情况下的问题是,如果您单击其中一个链接,则新表显示正常。但是如果您使用引擎进行新搜索并接收第一个表的不同迭代,则顶部标记将导致与之前相同的部分表,尽管来自新表。

代码:

的search.php

<?php
include("error_logger.php");

session_cache_limiter('private_no_expire');

function select_loop($select)
{
    $connect = mysql_connect("mysql.itn.liu.se", "lego") or die("connection failed!". mysql_error);
    mysql_select_db("lego");
    if($select == 1)
    {
        $contents = mysql_query("SELECT DISTINCT itemtypes.Itemtypename FROM itemtypes JOIN inventory
        ON inventory.ItemtypeID = itemtypes.ItemtypeID WHERE inventory.ItemtypeID = itemtypes.ItemtypeID
        ORDER BY itemtypes.Itemtypename ASC");
    }
    else if($select == 2)
    {
        $contents = mysql_query("SELECT DISTINCT categories.CategoryName FROM categories JOIN sets ON
        sets.CatID = categories.CatID WHERE sets.CatID = categories.CatID
        ORDER BY categories.Categoryname ASC");
    }
    else if($select == 3)
    {
        $contents = mysql_query("SELECT DISTINCT colors.Colorname FROM colors JOIN inventory ON
        inventory.ColorID = colors.ColorID WHERE inventory.ColorID = colors.ColorID
        ORDER BY colors.Colorname ASC");
    }
    else if($select == 4)
    {
        $contents = mysql_query("SELECT DISTINCT sets.Year FROM sets JOIN inventory ON
        sets.SetID = inventory.SetID WHERE sets.SetID = inventory.SetID
        ORDER BY Year DESC");
    }
    while($rowname = mysql_fetch_row($contents))
    {
        echo "<option value=\"$rowname[0]\">$rowname[0]</option>";
    }
    mysql_close($connect);
}

function sql_out($post) {
$selectarr = array("selector1" => "", "selector2" => "", "selector3" => "", "selector4" => "");
$wherearr = array("selector1" => "", "selector2" => "", "selector3" => "", "selector4" => "");
    if($post['setnr'] != "")
    {
        $post['setnr'] .= "-";
    }
    if($post['selector1'] != "")
    {
        $selectarr['selector1'] = "itemtypes." . $post['selector1'] . ", ";
        $wherearr['selector1'] = "itemtypes.Itemtypename = '" . $post['selector1'] . "' AND ";
    }
    if($post['selector2'] != "")
    {
        $selectarr['selector2'] = "categories." . $post['selector2'] . ", ";
        $wherearr['selector2'] = "categories.Categoryname = '" . $post['selector2'] . "' AND ";
    }
    if($post['selector3'] != "")
    {
        $selectarr['selector3'] = "colors." . $post['selector3'] . ", ";
        $wherearr['selector3'] = "colors.Colorname = '" . $post['selector3'] . "' AND ";
    }
    if($post['selector4'] != "")
    {
        $selectarr['selector4'] = "sets." . $post['selector4'];
        $wherearr['selector4'] = "sets.Year = '" . $post['selector4'] . "' AND ";
    }
    $connect = mysql_connect("mysql.itn.liu.se","lego");
    mysql_select_db("lego");
    $contents = mysql_query("SELECT DISTINCT sets.SetID, sets.Setname,  
                            categories.Categoryname,  sets.Year
                            FROM sets JOIN categories ON sets.CatID = categories.CatID
                            JOIN inventory ON sets.SetID = inventory.SetID
                            JOIN colors ON inventory.ColorID = colors.ColorID
                            JOIN itemtypes ON inventory.ItemtypeID = itemtypes.ItemtypeID
                            WHERE ".$wherearr['selector1'].$wherearr['selector2'].
                            $wherearr['selector3'].$wherearr['selector4']."
                            sets.SetID LIKE '".$post['setnr']."%' AND sets.Setname LIKE '%".$post['name']."%'
                            ORDER BY ".$post['order']." ".$post['order2']);

write_table($connect, $contents, 0);
}

function write_table($con, $contents, $imgvar)
{
global $arrstorage;
$arrstorage = array();
$k = 0;
    if(mysql_num_rows($contents) == 0)
   {
  print("Inga satser funna!");
   }
   else
   {
  print("<table border=1>\n<tr>");
  for($i=0; $i<mysql_num_fields($contents) + $imgvar; $i++)
  {
     if($i < mysql_num_fields($contents))
     {
        $fieldname = mysql_field_name($contents, $i);
        if($fieldname != "ColorID")
        {
            print("<th>$fieldname</th>");
        }
     }
     else
     {
        print("<th>Images</th>");
     }
  }
  print "</tr>\n";

  while($row = mysql_fetch_row($contents))
  {
     print("<tr>");
     $colid;
     $itemid;
     for($i=0; $i<mysql_num_fields($contents) + $imgvar; $i++)
     {
        if($i<mysql_num_fields($contents))
        {
            $fieldnamecheck = mysql_field_name($contents, $i);
            if($fieldnamecheck == "ColorID")
            {
                $colid = $row[$i];
            }
            if($fieldnamecheck == "PartID")
            {
                $itemid = $row[$i];
            }
            if($fieldnamecheck == "Setname")
            {
                array_push($arrstorage, $row[$i]);
                print("<td><a href=\"results.php?id=$k\">$row[$i]</a></td>");
                $k++;
            }
            else if($fieldnamecheck != "ColorID")
            {
                print("<td>$row[$i]</td>");
            }
        }
        else
        {
            print("<td><img src=\"http://webstaff.itn.liu.se/~stegu/img.bricklink.com/P/$colid/$itemid.gif\" 
                   style=\"margin: auto; display: block; max-height: 500px; max-width: 500px;\"/>
                   <img src=\"http://webstaff.itn.liu.se/~stegu/img.bricklink.com/P/$colid/$itemid.jpg\" 
                   style=\"margin: auto; display: block; max-height: 500px; max-width: 500px;\"/></td>");
        }
     }
     print("</tr>\n");
  } 
  print("</table>\n");
   }
mysql_close($con);

}

function show_set_parts($session, $nr)
{
    $connect = mysql_connect("mysql.itn.liu.se","lego");
    mysql_select_db("lego");
    var_dump($session['setname']['0'][$nr]);
    if(!empty($session['post']['0']['selector3']))
    {
        $session['post']['0']['selector3'] = "AND colors.Colorname = '" . $session['post']['0']['selector3'] . "'";
    }
    $contents = mysql_query("SELECT DISTINCT parts.Partname, parts.PartID, inventory.Quantity, 
                             colors.Colorname, colors.ColorID, itemtypes.Itemtypename
                             FROM inventory JOIN parts ON inventory.ItemID = parts.PartID
                             JOIN sets ON inventory.SetID = sets.SetID
                             JOIN colors ON inventory.ColorID = colors.ColorID
                             JOIN itemtypes ON inventory.ItemtypeID = itemtypes.ItemtypeID
                             WHERE sets.SetID = inventory.SetID 
                             AND inventory.ItemID = parts.PartID
                             AND sets.Setname = '".$session['setname']['0'][$nr]."' 
                             ".$session['post']['0']['selector3']."");
    unset($_SESSION);
    session_destroy();
    write_table($connect, $contents, 1);

}

results.php(显示表格)

<!DOCTYPE html>
<html>
<head>



</head>
<body>
<?php
include("search.php");

session_start();

if($_POST)
{
    $_SESSION['post'] = array();
    array_push($_SESSION['post'], $_POST);
}
if($_POST)
{
    sql_out($_POST);
}
else
{
    show_set_parts($_SESSION, $_REQUEST['id']);
}
if($_POST)
{
    $_SESSION['setname'] = array();
    array_push($_SESSION['setname'], $arrstorage);
}



?>


</body>
</html>

legodemo.php

<html>
<head>
<?php include("search.php"); ?>
<meta charset="utf-8"/>
</head>
<body>
<form action="results.php" method="POST">
  <p>Ange numret på en Lego-sats:</p>
  <p>Set Name: &nbsp;<input type="text" name="name" /></p>
  <p>Set ID: &nbsp;<input type="text" name="setnr" /></p>
  <p>Item Type: &nbsp;
  <select name="selector1" id="selector1">
  <option value="">Select All</option>
  <?php
      select_loop(1);
  ?>
  </select>
  <br />
  <br />
  Item Category: &nbsp;
  <select name="selector2" id="selector2">
  <option value="">Select All</option>
  <?php
      select_loop(2);
  ?> 
  </select>
  <br />
  <br />
  Item Color: &nbsp;
  <select name="selector3" id="selector3">
  <option value="">Select All</option>
  <?php
      select_loop(3);
  ?> 
  </select>
  <br />
  <br />
  Item Release Year: &nbsp;
  <select name="selector4" id="selector4">
  <option value="">Select All</option>
  <?php
      select_loop(4);
  ?> 
  </select>
  <br />
  <br />
  <p>Order by:
  <br />
  <input type="radio" value="sets.Setname" name="order" checked="checked"/>Setname
  <br />
  <input type="radio" value="sets.SetID" name="order"/>SetID
  <br />
  <input type="radio" value="colors.Colorname" name="order"/>Color
  <br />
  <input type="radio" value="categories.Categoryname" name="order"/>Category
  <br />
  <input type="radio" value="sets.Year" name="order"/>Year
  </p>
  <p>
  <input type="radio" value="ASC" name="order2" checked="checked"/>Ascending
  <br/>
  <input type="radio" value="DESC" name="order2"/>Descending
  </p>
  <p><input type="submit" value="Send request" /> &nbsp;
  <input type="reset" value="Reset request" /></p> 
</form>
</body>
</html>

据我所知,search.php中名为“$ session ['setname'] ['0'] [$ nr]”的变量是这里的主要问题,那就是没有正确更新。希望这里有人能帮助我找出这里的错误,因为你们所有人在这方面可能比我更好。

0 个答案:

没有答案