【问题标题】:Search and display results from SQL database从 SQL 数据库中搜索并显示结果
【发布时间】:2013-02-23 16:55:53
【问题描述】:

我有一个 MySQL 数据库,我需要搜索交易编号(它会显示我的姓名、电子邮件和 item_name)或搜索我的电子邮件地址、姓名和购买日期,然后我会得到我的交易编号和 item_name - 除非电子邮件、姓名和购买日期都正确,否则我不希望出现任何内容。

我的数据库中的字段是:

iname
iemail
itransaction_id
item_name

有人可以帮忙吗.... 这是我目前拥有的......

<html>

<head>
<title>Search</title>
</head>

<body bgcolor=#ffffff>

<h2>Search</h2>

<form name="search" method="post" action="findme.php">
Seach for: <input type="text" name="find" /> 


<input type="submit" name="search" value="Search" />
</form>

</body>

</html>

保存为 findme.html

下一个:

<html>
<head><title>Searching for a student...</title>
</head>
<body bgcolor=#ffffff>

<?php

echo "<h2>Search Results:</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}

// Otherwise we connect to our Database
mysql_connect("xxxx.com", "xxxx", "xxxxpw") or             die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());

// We perform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);

//Now we search for our search term, in the field the user specified
$iname = mysql_query("SELECT * FROM ibn_table WHERE upper($field) LIKE'%$find%'");

//And we display the results
while($result = mysql_fetch_array( $iname ))
{
echo $result['firstname'];
echo " ";
echo $result['lastname'];
echo "<br>";
echo $result['idnumber'];
echo "<br>";
echo "<br>";
}

//This counts the number or results - and if there wasn't any it gives them a     little     message explaining that
$anymatches=mysql_num_rows($iname);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
//}
?> 


</body>
</html>

另存为findme.php

这是我目前遇到的错误:

搜索结果:

警告:mysql_fetch_array():提供的参数不是第 31 行 /hermes/bosweb25a/b409/ipg.bexgroupcouk/silverliningnetworks/Database/findme.php 中的有效 MySQL 结果资源

警告:mysql_num_rows():提供的参数不是第 43 行 /hermes/bosweb25a/b409/ipg.bexgroupcouk/silverliningnetworks/Database/findme.php 中的有效 MySQL 结果资源 抱歉,我们找不到符合您查询的条目...

搜索过:CARYS

【问题讨论】:

  • $field 在哪里定义(来自 SQL 查询)?
  • 请快速搜索您收到的错误消息,您将获得足够多的结果,至少可以为您指明调试问题的正确方向。
  • 生成的 SQL 语句是什么?

标签: php html mysql sql database


【解决方案1】:

SELECT * FROM ibn_table WHERE itransaction_id LIKE '%$find%'
注意 LIKE 和 '%$find%' 之间的空格

【讨论】:

  • 谢谢,修复了错误,但现在显示:抱歉,我们找不到与您的查询匹配的条目...有什么想法吗?
【解决方案2】:

我有 tri 给你看看吧..

            <html>
            <head><title>Searching for a student...</title>
            </head>
            <body bgcolor=#ffffff>

            <?php
            include "config.php";
            echo "<h2>Search Results:</h2><p>";

            if(isset($_POST['search']))
            {
            $find =$_POST['find'];
            //If they did not enter a search term we give them an error
            if ($find == "")
            {
            echo "<p>You forgot to enter a search term!!!";
            exit;
            }

            // Otherwise we connect to our Database


            // We perform a bit of filtering
            $find = strtoupper($find);
            $find = strip_tags($find);
            $find = trim ($find);

            //Now we search for our search term, in the field the user specified
            $iname = mysql_query("SELECT * FROM ibntable WHERE name LIKE'%$find%'")
             or die(mysql_error());

            //And we display the results
            while($result = mysql_fetch_array( $iname ))
            {
            echo "id :" .$result['fname'];
            echo "<br> ";
            echo "name :".$result['lname'];
            echo "<br>";
            echo "name :".$result['middlename'];
            echo "<br>";
            echo "<br>";
            }

            //This counts the number or results - and if there wasn't any it gives them a     little     message explaining that
            $anymatches = mysql_num_rows($iname);
            if ($anymatches == 0)
            {
            echo "Sorry, but we can not find an entry to match your query...<br><br>";
            }

            //And we remind them what they searched for
            echo "<b>Searched For:</b> " .$find;
            //}


            }
            ?> 


            </body>
            </html>

【讨论】:

    【解决方案3】:

    作为后续行动,我遇到了同样的错误(php 脚本显示为文本),我修复它的方法是检查我用于网页的网址(假设我是从我的本地计算机)。

    您应该通过“localhost/somewebpage.html”格式而不是“file:///C:/wamp/www/somewebpage.html”格式访问您的页面。如果您从系统托盘中的 WAMP 服务器菜单转到 www 文件夹并双击您尝试运行的 html 页面,它将打开到 'file:///C:/wamp/www/somewebpage.html ' 格式。

    很明显,解决这个问题的方法是打开浏览器并手动输入“localhost/somewebpage.html”来查看 webapge。以这种方式运行它会强制它通过可以处理 php.ini 的 Apache 服务器运行。以另一种方式运行它会强制页面完全从浏览器运行,并且您的网络浏览器不是网络服务器,因此它将 php 脚本作为纯文本处理并将其显示在您的页面上。

    【讨论】:

      猜你喜欢
      • 2010-11-24
      • 2015-08-12
      • 2019-02-10
      • 2017-11-09
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      相关资源
      最近更新 更多