Need help on how to debug php code when nothing happens and a solution would be nice!

Hello,

I am still in my PHP infancy and have glared at my laptop screen bewildered for the past 50 minutes, frantically moving chunks of code around in a vein attempt to find out what is wrong with my script.

I am currently working my way through the build your database driven website on chapter 6.

The task is to create a multipurpose page that can manage my joke categories.

So far I can add and delete categories and view my content that needs to be edited. But every time I hit the edit button, I return to my category “home screen” which is else block of code with no updated records and no error messages. GGGRRRRR

I have done a similar exercise yesterday with “authors” but my new code isn’t comparable to my previous working demo as I am trying to refactor whilst learning.

Essentially I need 1 piece of information (how would a well rounded php programmer debug this script) and being cheeky if someone could give me a hint as to where my code is falling over that would be a double wammy:


<html>
    <head>
        <title>Joke Category management</title>
    </head>
    <body>
        <h1>Joke Category management</h1>
        <?php
        $dbcon = @mysql_connect(secret sshhhh);
        if (!$dbcon) {
            exit('<p>Unable to connect to database server at this time.</p>');    
        }
        if (!@mysql_select_db('ijdb')) {
            exit('<p>Unable to connect to database at this time.</p>');        
        }        
            if (isset($_GET['addCat'])):
        ?>
        <h2>Add new category:</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <p><label><input type="text" name="catName" /></label></p>
            <p><input type="submit" value="Add category" /></p>
        </form>
        <p><a href="/db-sites/ch6/cats.php">Return to category home</a></p>
        <?php elseif(!empty($_REQUEST['catName'])):
        $catName = $_REQUEST['catName'];
        if (isset($_REQUEST['catName'])) {
            $addCatSql = "insert into category set name='$catName'";
                if (@mysql_query($addCatSql)) {
                    echo '<p>Added category to database.</p>';
                    echo "<p><a href=". "/db-sites/ch6/cats.php " . ">Return to category home</a></p>";
                } else {
                    exit('<p>Unable to add category' . mysql_error() . '</p>');
                }
        }
        ?>
        <?php elseif(!empty($_REQUEST['delCat'])):
        $delCat = $_REQUEST['delCat'];
        $delSql1 = @mysql_query("delete from jokecategory where categoryid='$delCat'");
        $delSql2 = @mysql_query("delete from category where id='$delCat'");
            if ($delSql1 and $delSql2) {
                echo "<p>Joke category has been deleted.";
                echo "<p><a href=". "/db-sites/ch6/cats.php " . ">Return to category home</a></p>";    
            } else {
                exit('<p>Unable to delete category' . mysql_error() . '</p>');    
            }
        ?>
        <?php elseif(!empty($_REQUEST['editCat'])):
        $catId = $_REQUEST['editCat'];
        $updCat = @mysql_query("select name from category where id='$catId'");
        if (!$updCat){
            exit('<p>Unable to run query at this time.' . mysql_error() . '</p>');                
        }
        $updCat = mysql_fetch_array($updCat);
        $updCatName = $updCat['name'];
        $updCatName = htmlspecialchars($updCatName);
        if (!empty($_REQUEST['editCat']) and !empty($_REQUEST['catId'])) {
            $updNm = $_REQUEST['catName1'];
            $updCat = @mysql_query("update category set name='$updNm' where id='$catId'");
                if ($updCat) {
                    echo "<p>Joke category has been updated.";
                    echo "<p><a href=". "/db-sites/ch6/cats.php" . "</p>";    
                } else {
                    exit('<p>Unable to run query at this time.' . mysql_error() . '</p>');                    
                }            
        }        
        ?>
        <h2>Edit category:</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <p><label><input type="text" name="catName1" value="<?php echo $updCatName; ?>" /></label></p>
            <input type="hidden" name="catId" value="<?php echo $catId ?>" />
            <p><input type="submit" value="Edit category" /></p>
        </form>
        <p><a href="/db-sites/ch6/cats.php">Return to category home</a></p>
        <?php else:
        $listCats = @mysql_query("select * from category");
        if (!$listCats) {
            exit('<p>Unable to query database at this time:' . mysql_error() . '</p>');    
        }
        ?>
        <table>
            <tr>
                <td>Category name</td>
                <td>Edit</td>
                <td>Delete</td>
            </tr>
        <?php
        while($catData = mysql_fetch_array($listCats)) {
            echo "<tr><td>" . $catData['name'] . "</td>" .
                 "<td>" . "<a href=" . $_SERVER['PHP_SELF'] . "?editCat=" . $catData['id'] . ">" . "Edit" . "</a>" . "</td>" .
                 "<td>" . "<a href=" . $_SERVER['PHP_SELF'] . "?delCat=" . $catData['id'] . ">" . "Delete" . "</a>" . "</td></tr>";    
        }
        ?>
        </table>
            <p><a href="<?php echo $_SERVER['PHP_SELF'] . '?addCat=1'; ?>">Add a new category</a></p>
            <p><a href="/db-sites/ch6/">Return to home</a></p>
        <?php endif; ?>
    </body>
</html>

Any help would be greatly appreciated.

First I would remove the “@” sign before all mysql functions… Its very difficult to debug if error messages are ignored…

Then I would add a “or die(mysql_error())” after all mysql_query()

If the errors still exists, and is still not apparent… then I would start adding small “echo” messages inside if else code blocks, to see if the expected codeblok is executed.

If they are not executed, then go back and triple check variablenames… sometimes typos sneak in at the strangest places.

If everything else fails, then get a program with debugging capabilities (like eclipse). (would actually recommend this from the start, but its important to know a few quick manual debugging tricks)

oh, and I almost forgot another imporant piece of the puzzle.

Set error reporting to E_ALL in the very top of your document.

<?php error_reporting(E_ALL); ?>
<html>
<head>
<title>Joke Category management</title>
</head>
<body>