monkeykoder Posted February 23, 2011 Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228615-debugging-strategies/ Share on other sites More sharing options...
BlueSkyIS Posted February 23, 2011 Share Posted February 23, 2011 debugging strategy IS echo $varthatmightbegivingmetrouble for instance, apparently $UserName and $Password are not set. echo them and check their values. if they aren't set, look somewhere above the code you posted to find out why not. Quote Link to comment https://forums.phpfreaks.com/topic/228615-debugging-strategies/#findComment-1178725 Share on other sites More sharing options...
Psycho Posted February 23, 2011 Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228615-debugging-strategies/#findComment-1178728 Share on other sites More sharing options...
Muddy_Funster Posted February 23, 2011 Share Posted February 23, 2011 How the H*ll did monkeykoder get into the end of the echo $query?? Quote Link to comment https://forums.phpfreaks.com/topic/228615-debugging-strategies/#findComment-1178732 Share on other sites More sharing options...
monkeykoder Posted February 23, 2011 Author Share Posted February 23, 2011 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?? Oops skipped a line while copying... Quote Link to comment https://forums.phpfreaks.com/topic/228615-debugging-strategies/#findComment-1178735 Share on other sites More sharing options...
PFMaBiSmAd Posted February 23, 2011 Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228615-debugging-strategies/#findComment-1178752 Share on other sites More sharing options...
monkeykoder Posted February 23, 2011 Author Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228615-debugging-strategies/#findComment-1178755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.