Reputation: 11
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>';
}
}
?>
Upvotes: 1
Views: 1025
Reputation: 23120
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/>';
}
}
?>
Upvotes: 0
Reputation: 419
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).
Upvotes: 0
Reputation: 23120
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.
Upvotes: 1