duancha1065 2013-12-06 14:55
浏览 34

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

I have been assigned a task to create a search engine and a layout with a group of people, and am working with another to create the search engine itself, as a school project. We are using a database with multiple tables and plenty of content to work with.

To the issue, the two of us are are using the $_SESSION variable (from here on out just called session) to save the post data from a html form. Basically, the first search will bring up a table of contents, with one of the columns possessing anchor tags to go on further into the table (essentially first recieving a list of blueprints and the hyperlink leading to a table containing all the needed parts for it, or something similar.)

The issue in this case, if you click one of these links the new table shows just fine. But if you make a new search with the engine and recieve a different iteration of the first table, the top tag will lead to the same table of parts as before, despite going from a new table.

Code:

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>
<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>
";

  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>
");
  } 
  print("</table>
");
   }
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 (shows tables)

<!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>

As far as i can tell, the variable called "$session['setname']['0'][$nr]" in search.php is the major problem here, thats whats not updating properly. Hopefully someone here can help me find out what's wrong here, as all of you are probably way better at this than i am.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大