链接按钮到每个数据库搜索结果

时间:2013-12-05 08:48:32

标签: php mysql

我一直在寻找几天,似乎无法找到答案,所以想知道这里是否有人可以伸出援助之手。

我正在显示搜索数据库的结果列表,它显示每个结果,并显示在Checkout,Return或Edit按钮下面。我想要实现的是从每个搜索结果中获取每个结果下方按钮的正确值,然后在下一页上使用它们来进行实际结帐。因此,当用户点击此页面上的结帐,返回或编辑按钮时,上述值将被存储/发送到准备检出的下一页。

如果我尝试使用$ _SESSION变量,它只会存储最后一个值。我很抱歉,如果这是一个简单的问题,我现在已经停留了一段时间,并且需要完成它:)

我暂时使用mysql而不是mysqli,因为它是我的老板想要的,但是一旦他让我继续,我们将来会更新。

感谢您的帮助,我为编码错误道歉,因为我对这一切仍然很新。

再次感谢。

<?php session_start(); 

include ('conbase.php'); 
  if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
  } else {
      header("Refresh: 0; url=loginfailure.php");
  }
?>

<!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>Search Results</title>
<link href="css/calcsearchs.css" type="text/css" rel="stylesheet" />
</head>
<body>

<div id="headDiv" style="background-color:#000"></div>
<div id="picDiv" style="padding-left: 25%; float: left"><a href="index.php"><img  src="images/ogplogo.jpg" /></a></div><div style="float: left; padding-top: 80px; padding- left: 10px"><p><h1>OGPSS Inventory Database</h1><br /><h2>Search Results</h2></p></div>
<div style="clear: both">
<div id="apDiv1">Logged in as:</div>
<div id="apDiv2"><?php echo ucfirst($_SESSION['namevar']); ?></div>
<div id="apDiv3"><?php echo date('d-m-Y - H:ia'); ?></div>
<div id="apDiv4"><input name="logoutbutton" type="submit" value="Log Out" style="height: 27px; width: 85px; font-size: 12px" /></div>

<div style="padding-left: 35%">
<form action="confirmcheckout.php" method="post">
<?php


$query = $_POST['searchbox'];
$min_length = 3;

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

    $query = htmlspecialchars($query);
    // changes characters used in html to their equivalents, for example: < to &gt;

    $query = mysql_real_escape_string($query);
    // makes sure nobody uses SQL injection

    $raw_results = mysql_query("SELECT * FROM itemsinstock
        WHERE (`item_name` LIKE '%".$query."%') OR (`item_category` LIKE '%".$query."%') OR(`sernum` LIKE '%".$query."%') OR (`item_id` LIKE '%".$query."%') OR (`date_added` LIKE '%".$query."%') OR (`item_location` LIKE '%".$query."%') OR (`entered_by` LIKE '%".$query."%')") or die(mysql_error());

    // * means that it selects all fields, you can also write: `id`, `title`, `text`
    // articles is the name of our table

    // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
    // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
    // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

        while($results = mysql_fetch_array($raw_results)){
        // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

            echo "<p><b>Item S/N: </b>".$results['sernum']."</p>";
            echo "<p><b>Item ID: </b>".$results['item_id']."</p>";
            echo "<p><b>Item Name: </b>".$results['item_name']."</p>";
            echo "<p><b>Date Added: </b>".$results['date_added']."</p>";
            echo "<p><b>Item Category: </b>".$results['item_category']."</p>";
            echo "<p><b>Item Location: </b>".$results['item_location']."</p>";
            echo "<p><b>Item Qty: </b>".$results['item_qty']."</p>";
            echo "<p><b>Entered By: </b>".$results['entered_by']."</p>";

            echo "<div><form action='checkform.php' method='post'><input type='submit' name='checkoutbut' value='Checkout' style='height: 27px; width: 85px; font-size: 12px'/><input type='submit' name='returnbut' value='Return' style='height: 27px; width: 85px; font-size: 12px'/><input type='submit' name='editbut' value='Edit'style='height: 27px; width: 85px; font-size: 12px' /></form></div></br></br>"; 
            // posts results gotten from database(title and text) you can also show id ($results['id'])
        }

    }
    else{ // if there is no matching rows do following
        echo "There are no results to display matching your query.";
    }

} if($query == ''){ // if query length is more or equal minimum length then


    $query = $_POST['indexselect'];  
    $query = htmlspecialchars($query);
    // changes characters used in html to their equivalents, for example: < to &gt;

    $query = mysql_real_escape_string($query);
    // makes sure nobody uses SQL injection

    $raw_results = mysql_query("SELECT * FROM itemsinstock
        WHERE (`item_name` LIKE '%".$query."%') OR (`item_category` LIKE '%".$query."%') OR(`sernum` LIKE '%".$query."%') OR (`item_id` LIKE '%".$query."%') OR (`date_added` LIKE '%".$query."%') OR (`item_location` LIKE '%".$query."%') OR (`entered_by` LIKE '%".$query."%')") or die(mysql_error());

    // * means that it selects all fields, you can also write: `id`, `title`, `text`
    // articles is the name of our table

    // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
    // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
    // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

        while($results = mysql_fetch_array($raw_results)){
        // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

            echo "<p><b>Item S/N: </b>".$results['sernum']."</p>";
            echo "<p><b>Item ID: </b>".$results['item_id']."</p>";
            echo "<p><b>Item Name: </b>".$results['item_name']."</p>";
            echo "<p><b>Date Added: </b>".$results['date_added']."</p>";
            echo "<p><b>Item Category: </b>".$results['item_category']."</p>";
            echo "<p><b>Item Location: </b>".$results['item_location']."</p>";
            echo "<p><b>Item Qty: </b>".$results['item_qty']."</p>";
            echo "<p><b>Entered By: </b>".$results['entered_by']."</p>";

            echo "<div><form action='confirmcheckout.php' method='post'><input type='submit' name='checkoutbut' value='Checkout' style='height: 27px; width: 85px; font-size: 12px'/><input type='submit' name='returnbut' value='Return' style='height: 27px; width: 85px; font-size: 12px'/><input type='submit' name='editbut' value='Edit'style='height: 27px; width: 85px; font-size: 12px' /></form></div></br></br>"; 
            // posts results gotten from database(title and text) you can also show id ($results['id'])
        }

    } 
}
?>
</form>
</div>

<div id="footerDiv"><p align="center">Copyright OGPSS 2014</p></div>

</body>
</html>

编辑:

嗨再次,非常感谢你的帮助。

这是我正在寻找的!现在唯一的事情是我确定我在显示条目的最后一页上做错了。使用这种方法我不确定我是否将$ _GET变量放在正确的位置。当我来到这个页面时,在文本框中我看到应该有值的未定义索引。

从一些例子中我已经看到这应该有效,但同时我确定我在这里做错了所以如果你能告诉我在哪里?

<?php session_start(); 
    if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
    } else {
        header("Refresh: 0; url=loginfailure.php");
        }
?>  

<!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>Confirm Checkout</title>
<link href="css/confirmcheckout.css" type="text/css" rel="stylesheet" />
</head>

<body>

<div id="apDiv1"></div>

<div id="topBarDiv"><a href="index.php"><img src="images/ogplogo.jpg" width="119"  height="205" alt="OGPLOGO" /></a></div>

<div id="apDiv3"><h1>OGPSS Inventory Database</h1></div>

<div id="apDiv4"><h3>Confirm Checkout</h3></div>

<form action="checkoutcomplete.php" method="post">

    <div id="mainDiv">Item S/N:</div>
    <div id="apDiv2"><input name="checksnbox" type="text" size="30" value="<?php echo $_GET['itemsn']; ?>" /></div>

    <div id="apDiv5">Item ID:</div>
    <div id="apDiv6"><input name="checkidbox" type="text" size="30" value="<?php echo $_GET['itemid']; ?>" /></div>

    <div id="apDiv8">Taken By:</div>
    <div id="apDiv9"><input name="checkusernamebox" type="text" size="30" value="<?php echo ucfirst($_SESSION['namevar']) ?>" /></div>

    <div id="apDiv10">Date Taken:</div>
    <div id="apDiv11"><input name="checkdatebox" type="text" size="30" value="<?php echo date('d-m-y'); ?>" /></div>

    <div id="apDiv14">Qty Taken:</div>
    <div id="apDiv15"><input name="checkqtybox" type="text" size="30" value="<?php echo $_GET['itemqty']; ?>" /></div>

    <div id="apDiv12">Item Name:</div>
    <div id="apDiv13"><input name="checitemnamebox" type="text" size="30" value="<?php echo $_GET['itemname']; ?>" /></div>

    <div id="apDiv7"><input style="height: 27px; width: 85px" name="checkcheckbut" type="submit" value="Confirm" /></div>

</form>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

您可以按如下方式发送包含行值的隐藏输入,您需要将其添加到表单中

<input type='hidden' name='row_id' value='{$results['id']}'/>

您可以稍后捕获它,就像在请求中发送的任何其他值一样 所以

$id = $_POST['row_id'];

答案 1 :(得分:0)

为什么不在表单中使用<a>标记来执行相同的操作,而不是在表单中使用表单。

 echo "<div><a href='confirmcheckout.php?id=$results['id']' method='post'>
       <input type='button' name='checkoutbut' value='Checkout' style='height: 27px; 
       width: 85px; font-size: 12px'/><input type='button' name='returnbut' 
       value='Return' style='height: 27px; width: 85px; font-size: 12px'/>
       <input type='button' name='editbut' value='Edit'style='height: 27px; 
        width: 85px; font-size: 12px' /></a></div></br></br>";

confirmcheckout.php:

$id = $_GET['id'];

答案 2 :(得分:0)

首先,你不应该在另一个表格中有一个表格。这在任何浏览器中都无法正常工作,并且特别禁止使用,正如您在W3C XHTML官方规范B节“元素禁止”中所见,声明:“表单不得包含其他表单元素。” http://www.w3.org/TR/xhtml1/#prohibitions

说这个,接下来就是你不应该在搜索结果页面或会话中有太多数据。最好的方法是简单地传递元素的id,然后在下一页中执行另一次搜索,以检索包含所有必要数据的项目。 哟可以通过隐藏的字段传递id,如@AliTRixx sais,带

<input type='hidden' name='row_id' value='{$results['id']}'/>

或在GET参数中,更改表单按钮形式链接,例如

<a href ='confirmcheckout.php?id=ITEM_ID'>checkout</a>