以 PHP 脚本形式生成搜索 MySQL DB 的错误屏幕

发布于 2024-10-30 21:28:11 字数 2100 浏览 5 评论 0原文

我是 PHP 新手,但我想对 MySQL 做一些研究。我有以下代码:

<?php

mysql_connect ("localhost", "db_user01", "******")  or die (mysql_error());
mysql_select_db ("database_test");

$term = $_POST['term'];

$sql = mysql_query("select * from testdata where UserID like '$term'");

while ($row = mysql_fetch_array($sql)){
    echo '<br/> Id: '.$row['UserID'];
echo '<br/> name: '.$row['Cliente'];
echo '<br/> mail: '.$row['DDT'];
echo '<br/> phone: '.$row['Status'];
echo '<br/><br/>';
}
?>


<html>
<head>
<title>retrieving db info form</title>

</head>
<body>

<form action="http://webserver/searchdb.php" method="post">
<input type="text" name="term" />
<br />
<input type="submit" name="submit" value="Submit" id="submit" />
</form>
</body>
</html> 

此代码对表“testdata”中的数据库“database_test”执行搜索,查找字段“UserID”,并打印与该用户 ID 相关的数据。根据搜索的用户,该信息具有正确的 ID(“userID”)并且工作正常。不过,我还是遇到了一点麻烦。当用户写入错误的 UserID 时,我需要生成一个屏幕告诉他“ID 错误”,但是当发生这种情况时,脚本会生成一个空白屏幕(没有错误或任何内容)。我真的不知道如何打印或显示一条文本,说明用户输入了错误的用户 ID。

我再说一遍,一切正常,但当用户输入错误的用户 ID 时,我需要生成错误屏幕或向用户打印一条消息;否则他们会得到一个空白屏幕。 我对 PHP 非常陌生,所以请尽快帮助我!我会对此线程保持警惕!非常感谢!


更新:我正在发布带有最新更改的代码;它仍然不起作用。现在它甚至无法使用正确的用户 ID :( 请检查一下并告诉我错误在哪里。我所做的唯一修改是在搜索页面添加一个“后退”链接。

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

mysql_connect ("localhost", "database_test", "****")  or  die (mysql_error());
mysql_select_db ("db_contents");

$term = $_POST['term'];

$sql = mysql_query("select * from trackingmc where UserID like '$term'");
if (mysql_num_rows() == 0) {
echo 'wrong ID, check your id. <a href="http://www.myserver.com/search.html">Go back</a>';
}
else {
    while ($row = mysql_fetch_array($sql)){
echo '<br/> Id: '.$row['UserID'];
echo '<br/> name: '.$row['Cliente'];
echo '<br/> mail: '.$row['DDT'];
echo '<br/> Status: '.$row['Status'];
echo '<br/><br/>';
echo '<a href="http://www.myserver.com/search.html">Go back</a>';
    }
}
?>

I'm new with PHP, but I want to do some research on MySQL. I've got this code:

<?php

mysql_connect ("localhost", "db_user01", "******")  or die (mysql_error());
mysql_select_db ("database_test");

$term = $_POST['term'];

$sql = mysql_query("select * from testdata where UserID like '$term'");

while ($row = mysql_fetch_array($sql)){
    echo '<br/> Id: '.$row['UserID'];
echo '<br/> name: '.$row['Cliente'];
echo '<br/> mail: '.$row['DDT'];
echo '<br/> phone: '.$row['Status'];
echo '<br/><br/>';
}
?>


<html>
<head>
<title>retrieving db info form</title>

</head>
<body>

<form action="http://webserver/searchdb.php" method="post">
<input type="text" name="term" />
<br />
<input type="submit" name="submit" value="Submit" id="submit" />
</form>
</body>
</html> 

This code performs a search on the database "database_test" in table "testdata", for the field "UserID" and prints the data related to that user ID. Based on the user who searches, the info has the correct ID ("userID") and it works just fine. Still, I've got a little trouble. When a user writes a bad UserID, I need to generate a screen telling him "Wrong ID", but when this happens the script generates a blank screen (no errors or anything). I really don't know how to print or show a text saying that the user input a wrong user ID.

I repeat, everything works fine but I need to generate an error screen or print a message to the user when they input a wrong user ID; otherwise they get a blank screen.
I'm really new to PHP, so please, I need your help with this ASAP! I'll be alert on this thread! Thank you very much!


UPDATE: I'm posting my code with the latest changes; it still doesn't work. Now it doesn't even work with correct user ID :( Please check it out and tell me where the error is. The only modification I made is adding a "back" link to the search page.

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

mysql_connect ("localhost", "database_test", "****")  or  die (mysql_error());
mysql_select_db ("db_contents");

$term = $_POST['term'];

$sql = mysql_query("select * from trackingmc where UserID like '$term'");
if (mysql_num_rows() == 0) {
echo 'wrong ID, check your id. <a href="http://www.myserver.com/search.html">Go back</a>';
}
else {
    while ($row = mysql_fetch_array($sql)){
echo '<br/> Id: '.$row['UserID'];
echo '<br/> name: '.$row['Cliente'];
echo '<br/> mail: '.$row['DDT'];
echo '<br/> Status: '.$row['Status'];
echo '<br/><br/>';
echo '<a href="http://www.myserver.com/search.html">Go back</a>';
    }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-11-06 21:28:11

通常,在生产 Web 服务器上,出于安全原因,错误显示会被关闭。尝试

error_reporting(E_ALL);
ini_set('display_errors','On');

在脚本的开头写入:,然后查看是否显示错误。

您可能应该检查 mysql_error() 是否返回某些内容,然后使用 print 显示错误消息,而不是使用 die 函数。

Often, on production web servers, for security reasons, error displaying is turned off. Try writing:

error_reporting(E_ALL);
ini_set('display_errors','On');

at the beginning of your script, and see if it displays error.

Instead of using die function, probably you should check if mysql_error() returns something, and then use print to display an error message.

我为君王 2024-11-06 21:28:11

帮助调试的一个简单方法是在 mysql_query 语句下添加两行:

echo mysql_errono() . "<br />";
echo mysql_error() . "<br />";

我发现了很多阻止任何内容基本回显的问题,可以通过添加像上面这样的调试行来找到,以及:

echo "Something happens here!";
$var = func1( );
echo "Something has happened!";

如果有什么不好的地方发生在 func1() 中,然后您将看到第一个调试,但看不到第二个,然后知道将更多调试行添加到 func1() 中(或者,如果你不能这样做,那么你可以检查你传递给该函数的参数)。

An easy way to help debug is adding two lines under your mysql_query statement:

echo mysql_errono() . "<br />";
echo mysql_error() . "<br />";

I've found a lot of problems that prevent basic echoing of anything can be found by adding in debugging lines like the ones above, as well as:

echo "Something happens here!";
$var = func1( );
echo "Something has happened!";

Where if something bad happens in func1() then you'll see the first debug, but not the second, and will then know to add more debugging lines into func1() (or, if you can't do that, then you can check your parameters you're passing into that function).

人生戏 2024-11-06 21:28:11

实际上,当您执行查询时,您不会检查返回的行数。尝试这样的事情:

<?php

mysql_connect ("localhost", "db_user01", "******")  or die (mysql_error());
mysql_select_db ("database_test");

$term = $_POST['term'];

$sql = mysql_query("select * from testdata where UserID like '$term'");
if (mysql_num_rows($sql) == 0) {
    echo 'wrong ID, check your id.';
}
else {
    while ($row = mysql_fetch_array($sql)){
        echo '<br/> Id: '.$row['UserID'];
        echo '<br/> name: '.$row['Cliente'];
        echo '<br/> mail: '.$row['DDT'];
        echo '<br/> phone: '.$row['Status'];
        echo '<br/><br/>';
    }
}
?>

Actually, when you perform the query, you don't check the number of returned rows. Try something like that:

<?php

mysql_connect ("localhost", "db_user01", "******")  or die (mysql_error());
mysql_select_db ("database_test");

$term = $_POST['term'];

$sql = mysql_query("select * from testdata where UserID like '$term'");
if (mysql_num_rows($sql) == 0) {
    echo 'wrong ID, check your id.';
}
else {
    while ($row = mysql_fetch_array($sql)){
        echo '<br/> Id: '.$row['UserID'];
        echo '<br/> name: '.$row['Cliente'];
        echo '<br/> mail: '.$row['DDT'];
        echo '<br/> phone: '.$row['Status'];
        echo '<br/><br/>';
    }
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文