将搜索到的数据输出到另一个表单


Outputting searched data to another form

我目前正在构建一个系统,它得到一个唯一的值,并使用它作为一个登录即参考号码。参考号然后搜索数据库并将所有相应的数据输出到另一个页面,我正在努力解决这个问题,我的代码是:

index . php

<input name="search_box"  type="text" class="auto-style1" id="search_box" style="width: 240px; height: 30px" maxlength="12">
<input type="submit" name="search" value="Enter" class="auto-style1" style="width: 63px; height: 30px"></td>
<?php $reasons = array("search_box" => "Please Enter Valid Reference Number", "" => "Error"); if ($_GET["loginFailed"]) echo $reasons[$_GET["reason"]]; ?>
</form>

Check.php

 <?php
include "conn.php";
mysql_connect("localhost","root") or die(header("location:index.php?loginFailed=true&reason=search_box"));
mysql_select_db("DB1") or die(header("location:index.php?loginFailed=true&reason=search_box"));
$reference1 = $_POST['search_box'];
$sql = "SELECT * FROM test_table1 WHERE No =$R1";
$result = mysql_query($sql) or die(mysql_error());
    if ($result)
    $count = mysql_num_rows($result);
    else
    $count = 0;
if($count == 1)
 {
  session_register('search_box');
  header("location:result.php");
  }
else 
{
echo (header("location:index.php?loginFailed=true&reason=search_box"));
 }
 ?> 

Output.php

 <?php
include "conn.php";
$sq2 = "SELECT * FROM test_table1";
if (isset($_POST['search']))
 {
    $search_term = mysql_real_escape_string($_POST['search_box']);
    $sq2 .= "WHERE No =  '{$search_term}'";
}
$query= mysql_query($sq2, $con);
while($row = mysql_fetch_array($query)) { ?>
&nbsp;<font size="4" face="Calibri"><b> Ref Number:   </b> </font><?php echo $row['No']; ?></td>
<p>
&nbsp;<font size="4" face="Calibri"><b> Location:   </b></font><?php echo $row['Country']; ?></td>
<p>
<?php
mysql_close($con);
?> 

你们能给予的任何帮助都将是非常感激的,谢谢。

Qwerty .

您的问题(您现在可能想知道的问题(因为似乎有一些更潜在的问题和漏洞))是在Output.php

当你从Login_check.php调用该文件时(因为你正在重定向浏览器),你的$_POST数组不再包含任何数据。

为了让自己更容易,为什么不使用include "Output.php";而不是通过headers()重定向浏览器?这样您就不需要再次在Output.php中重新初始化会话或数据库。

如果你需要这个重定向,在会话初始化后将$_POST['search']存储到$_SESSION['search'],然后在Output.php中引用$_SESSION['search'],而不是(就像你现在拥有的那样)引用$_POST['search']

希望对大家有帮助。

所以我假设你有一个表donations,其中你有一些捐赠的donId(参考号),捐赠的人的donName,捐赠者的国家donCountrydonAmount他们捐赠了多少钱。你的文件看起来像这样(我不会打扰所有适当的html标题,所以结果不会是一个w3c有效的html,但它应该在你的浏览器工作没有问题。

我认为我必须说,你不应该使用mysql_query和所有其他mysql_...函数使用代替mysqli_*。下面的代码仍然使用旧的已经弃用的mysql_函数,所以你有兴趣将其升级为mysqli(这是一个家庭作业;)lol)


文件donInfo.php:

<html>
<body>
<form action='donInfo.php' method='post'>
    <label for='donId'>Reference No.:</label>
    <input type='text' size='6' name='donId' id='donId' value='' />
    <input type='submit' name='do' value='  Show me!  ' style='margin-left:2em;'/>
</form>
<?php
    /* table definition: 
    CREATE TABLE donations (
        donId int unsigned not null,
        donCountry varchar(80) not null,
        donName varchar(80) not null,
        donAmount numeric(11,2) not null,
        PRIMARY KEY (donId)
    )
    */
    if (!isset($_POST['do']) || !isset($_POST['donId']) || !$_POST['donId']) 
        exit;
    require_once 'connection.php';
    $don=mysql_fetch_assoc(
        mysql_query('SELECT * '.
            'FROM donations '.
            'WHERE donId="'.mysql_real_escape_string($_POST['donId'],$con).'"',$con));
    if ($don===false || !$don['donId'])
        print '<h3>Donation id #'.$_POST['donId'].' does not exist!</h3>';
    else {
        print '<h3>Information about donation id #'.$_POST['donId'].'</h3>'.
            'State: '.$don['donCountry'].'<br/>'.
            'Donator: '.$don['donName'].'<br/>'.
            'Amount: $ '.number_format($don['donAmount'],2).'<br/>'.
            '<hr/>';
        $sumP=mysql_fetch_assoc(
            mysql_query('SELECT SUM(donAmount) total, COUNT(*) donx '.
                'FROM donations '.
                'WHERE donName="'.mysql_real_escape_string($don['donName'],$con).'" '.
                'GROUP BY donName',$con));
        print '<h4>Donations from '.$don['donName'].':</h4>'.
            'Total Amount: $ '.number_format($sumP['total'],2).'<br/>'.
            'Donated <b>'.number_format($sumP['donx'],0).'</b> times to date.<br/>'.
            '<hr/>';
        $sumC=mysql_fetch_assoc(
            mysql_query('SELECT SUM(donAmount) total, COUNT(DISTINCT donName) donators, COUNT(*) donx '.
                'FROM donations '.
                'WHERE donCountry="'.mysql_real_escape_string($don['donCountry'],$con).'" '.
                'GROUP BY donCountry',$con));
        print '<h4>Donations from '.$don['donCountry'].':</h4>'.
            'Total Amount: $ '.number_format($sumC['total'],2).'<br/>'.
            'Total of <b>'.number_format($sumC['donx']).'</b> donations from <b>'.number_format($sumC['donators'],0).'</b> donators.<br/>'.
            '<hr/>';
        $sumW=mysql_fetch_assoc(
            mysql_query('SELECT SUM(donAmount) total, COUNT(DISTINCT donName) donators, COUNT(DISTINCT donCountry) countries, COUNT(*) donx '.
                'FROM donations '.
                'GROUP BY 1=1',$con));
        print '<h4>Donations Total:</h4>'.
            'Total Amount: $ '.number_format($sumW['total'],2).'<br/>'.
            'Total of <b>'.number_format($sumW['donx']).'</b> donations from <b>'.number_format($sumW['countries'],0).'</b> countries and <b>'.number_format($sumW['donators'],0).'</b> donators.<br/>'.
            '<hr/>';        
    }
?>
</body>
</html>
我已经测试了代码,所以它应该工作得很好。我正在使用你的connection.php脚本,(我猜)你正在初始化你的数据库。

功能的简短描述:请求一个ID,然后在提交时检查它是否存在,如果存在,它将打印关于该捐赠ID的信息,关于该ID的用户的信息,关于该ID的国家和总捐赠统计。如果捐赠Id不存在,则等待输入另一个Id时,只会显示:does not exist。