Jump to content

Debugging strategies


monkeykoder

Recommended Posts

I am just starting my first solo web project in a new language (php) and since I don't have my old boss to go to for code help I need to build a stronger set of debugging strategies or at least a set of debugging strategies that goes beyond echo $varthatmightbegivingmetrouble . 

 

Right now I'm working with this line of code right here:

$query = sprintf("INSERT INTO Users (UserName, Password) VALUES ('%s', '%s')", mysql_real_escape_string($UserName), mysql_real_escape_string($Password)); 
echo $query;

which is outputting:

"INSERT INTO Users (UserName, Password) VALUES ('', '')monkeykoder"

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

Well, the answer to your problem is pretty simple: $UserName and $Password are either not set or have values that result in empty strings. You need to check that you don't have a typo in the variable names and that the values are getting properly set.

 

As for debugging techniques, that is a complex conversation that doesn't fit into a forum post. But, I typically like to have a switch to turn debugging on/off. That way I can leave all my debugging code in place when my pages go to production and just turn the debugging mode off (you can even make it so you can turn on debug mode on a user-by-user basis if you have to trace down a problem only occuring in production). So, instead of using echo, I will create a function with the text to display in debug mode. You can even pass the line number to make it easier to trace down where the problem occured.

 

Example

//Set the debug mode. You can do this statically in the file
//Or dynamically by username, IP address, $_GET parameters, etc.
$_SESSION['debug'] = true;

function debug($msg, $lineNo)
{
    if($_SESSION['debug']!=true)
    {
        return false;
    }

    echo "Debug [{$lineNo}]: {$msg}<br />\n";
    return;
}

//Usage
$query = sprintf("INSERT INTO Users (UserName, Password) VALUES ('%s', '%s')",
                  mysql_real_escape_string($UserName),
                  mysql_real_escape_string($Password));
debug("Query: $query", __LINE__);

 

That is just a rudimentary example. But, by using a function to handle the debugging you can make the process more elaborate without having to change all the lines where you have debugging. For example, you can have the debug function store all the degug info and echo them all at the end of the page since echoing them at the point where they occur could cause problems with the HTML output. Or, you could echo the debug messages as HTML comments, write to a log file, etc. The possibilities are endless.

Link to comment
Share on other sites

Thanks for the first debugging tips.  If you don't mind looking...

 

Full source code:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<?php
$Message = "";

if(isset($_POST["submit"])){
	$conn = mysql_connect(localhost, KoderPagesUser, starwars);
	$UserName = $_POST["username"];
	$Password = $_POST["password"];
	$query = sprintf("SELECT * FROM Users WHERE UserName = '%s'", mysql_real_escape_string($UserName)); 
	//echo $query;
	//die(); 
	$resource = mysql_query($query, $conn);
	if(!mysql_fetch_assoc($resource))
	{ 
		if(!$conn){
			$query = sprintf("INSERT INTO Users (UserName, Password) VALUES ('%s', '%s')", mysql_real_escape_string($UserName), mysql_real_escape_string($Password)); 
			echo $query;
			die(); 
			$resource = mysql_query($query, $conn);
		}
		if(session_start()){
			$_SESSION["UName"] = $UserName;
			http_redirect("index.php");
		}	
	} else
	{
		$Message = "Sorry username already exists.";
	}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Cp1252">
<title>Insert title here</title>
</head>
<body>
<?php 

echo $Message;
?>
<form name="updateusers" method="post" action="adduser.php">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" name="submit" />
</form>
</body>
</html>

 

How the H*ll did monkeykoder get into the end of the echo $query?? :o

 

Oops skipped a line while copying...

Link to comment
Share on other sites

You should be developing and debugging your code on a system with error_reporting set to -1 and display_errors set to ON in your master php.ini so that all the php detected errors will be reported and displayed.

 

Based on the code you posted, your mysql_connect() is probably failing and every mysql_ statement after that is also failing, which would explain why any variable passed through mysql_real_escape_string() is empty.

Link to comment
Share on other sites

You should be developing and debugging your code on a system with error_reporting set to -1 and display_errors set to ON in your master php.ini so that all the php detected errors will be reported and displayed.

 

Based on the code you posted, your mysql_connect() is probably failing and every mysql_ statement after that is also failing, which would explain why any variable passed through mysql_real_escape_string() is empty.

:-[  Of course I forgot to check my connection...  Thanks for the reply.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.