Moving on, lets say we have a form that’s not working correctly. For some reason the data isn’t making it to
the database when the user hits submit. Once we’ve determined that the database is fine, we will need to
check our variables. In order to print these variables to the screen, we will test first that it’s ok, and
if so we’ll use a print_r on the form data.
the database when the user hits submit. Once we’ve determined that the database is fine, we will need to
check our variables. In order to print these variables to the screen, we will test first that it’s ok, and
if so we’ll use a print_r on the form data.
<?php
/* test that debugging is ok and then print the variables */
if ( $debug_ok == 1 ) print_r( $_POST );
?>
The above code should be placed within the target page, where you want the data to appear. This will
send all the values submitted via form, to the screen. Note: if you’re using the GET method in the form,
you’ll want to use $_GET in your print_r() function.
send all the values submitted via form, to the screen. Note: if you’re using the GET method in the form,
you’ll want to use $_GET in your print_r() function.
Another great way to use this is for echoing your SQL query, rather than sending it to the database:
<?php
$sql
= "INSERT INTO wherever (field1,field2) VALUES ('$field1','$field2')";
if ( $debug_ok == 1 ) echo $sql;
else mysql_query( $sql );
?>
This will help avoid a database cleanup once you’re finished debugging. Of course, you can use both
of the debug snippets simultaneously to get the maximum effect.
of the debug snippets simultaneously to get the maximum effect.