Page 1 of 1

Script not generating my html code as expected

Posted: Fri Feb 08, 2008 3:23 pm
by timski72
Hi Everyone,

I've tried writing a script but having trouble getting it to work. I want a user to be able to fill in some text fields and from these fields add an entry in mysql database. The first time I call the script I expect the $POST[addverb] to be blank and therefore it should jump to the code where it generates my html form where a user can enter some data. This isn't happening though. When I call it I just get a blank page. Any ideas why that might be? I've pasted the script below.
Thanks,
Tim.

Code: Select all

 
<?php
 
if ($_POST[addverb] != "add")
{
    // haven't seen the form so show it
    $displayBlock = "
    <h1>Add an entry</h1>
    <form method=\"post\" action\"$_SERVER[PHP_SELF]\"
    
    <p><strong>Verb ID</strong><br></p>
    <input type=\"text\" name=\"verb_id\" size=2>
    
    <p><strong>Tense ID</strong><br>
    <input type=\"text\" name=\"tense_id\" size=2></p>
    
    <p><strong>1st pers sing</strong><br>
    <input type=\"text\" name=\"f_sng\" size=30 maxlength=255></p>
    
    <p><strong>2nd pers sing</strong><br>
    <input type=\"text\" name=\"s_sng\" size=30 maxlength=255></p>
    
    <p><strong>3rd pers sing</strong><br>
    <input type=\"text\" name=\"t_sng\" size=30 maxlength=255></p>
    
    <p><strong>1st pers plr</strong><br>
    <input type=\"text\" name=\"f_plr\" size=30 maxlength=255></p>
    
    <p><strong>2nd pers plr</strong><br>
    <input type=\"text\" name=\"s_plr\" size=30 maxlength=255></p>
    
    <p><strong>3rd pers plr</strong><br>
    <input type=\"text\" name=\"t_plr\" size=30 maxlength=255></p>
    
    <input type=\"hidden\" name=\"addverb\" value=\"add\">  
    
    <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
    </form>";
}
else if ($_POST[addverb] == "add")
{
    // time to add to tables, so check for required fields
    if (($_POST[verb_id] == " ") || ($_POST(tense_id)))
    {
        header("1st pers sing and tense: addentry.php");
        exit;
    }
    // connect to database
    $connection = mysql_connect("localhost", "guest","guest") or die(mysql_error());
    mysql_select_db("greekverbs", $connection) or die(mysql_error());
    
    if (($_POST[verb_id]) || ($_POST[tense_id]) ||  ($_POST[f_sng]) || ($_POST[s_sng]) || ($_POST[t_sng]) || ($_POST[f_plr]) || ($_POST[s_plr]) || ($_POST[t_plr]))
    {
        $add_verb = "insert into conjugats values ('', '$_POST[verb_id]', '$_POST[tense_id]','$_POST[f_sng]', '$_POST[s_sng])'$_POST[t_sng]', $_POST[f_plr]', $_POST[s_plr]','$_POST[t_plr]')";
        mysql_query($add_verb) or die(mysql());
    }
    $displayBlock = "<h1>Entry added</h1>
    <p>Your entry has been added. Would you like to <a href=\"addentry.php\">add another</a>?</p>";
    
}
?>
<html>
<head>
<title>Add an Entry</title>
</head>
<body>
<?php echo $display_block;?>
</body>
</html>
 

Re: Script not generating my html code as expected

Posted: Fri Feb 08, 2008 3:44 pm
by Christopher
You code has a simple if/else that is coded in a complex way. You might want to reorganize your code something like this (I have not check if this code runs):

Code: Select all

 
<?php
 
$show_form = true;
if ($_POST[addverb] == "add")
{
    // time to add to tables, so check for required fields
    if (($_POST[verb_id] == " ") || ($_POST(tense_id)))
    {
        // connect to database
        $connection = mysql_connect("localhost", "guest","guest") or die(mysql_error());
        mysql_select_db("greekverbs", $connection) or die(mysql_error());
    
        if (($_POST[verb_id]) || ($_POST[tense_id]) ||  ($_POST[f_sng]) || ($_POST[s_sng]) || ($_POST[t_sng]) || ($_POST[f_plr]) || ($_POST[s_plr]) || ($_POST[t_plr]))
        {
            $add_verb = "insert into conjugats values ('', '$_POST[verb_id]', '$_POST[tense_id]','$_POST[f_sng]', '$_POST[s_sng])'$_POST[t_sng]', $_POST[f_plr]', $_POST[s_plr]','$_POST[t_plr]')";
            if (! mysql_query($add_verb)) {
                 $show_form = false;
                 $displayBlock = "<h1>Entry added</h1>
<p>Your entry has been added. Would you like to <a href=\"addentry.php\">add another</a>?</p>";
            }
        }
    }
    
}
 
if ($show_form)
{
    // haven't seen the form so show it
    $displayBlock = "
    <h1>Add an entry</h1>
    <form method=\"post\" action\"$_SERVER[PHP_SELF]\"
    
    <p><strong>Verb ID</strong><br></p>
    <input type=\"text\" name=\"verb_id\" size=2>
    
    <p><strong>Tense ID</strong><br>
    <input type=\"text\" name=\"tense_id\" size=2></p>
    
    <p><strong>1st pers sing</strong><br>
    <input type=\"text\" name=\"f_sng\" size=30 maxlength=255></p>
    
    <p><strong>2nd pers sing</strong><br>
    <input type=\"text\" name=\"s_sng\" size=30 maxlength=255></p>
    
    <p><strong>3rd pers sing</strong><br>
    <input type=\"text\" name=\"t_sng\" size=30 maxlength=255></p>
    
    <p><strong>1st pers plr</strong><br>
    <input type=\"text\" name=\"f_plr\" size=30 maxlength=255></p>
    
    <p><strong>2nd pers plr</strong><br>
    <input type=\"text\" name=\"s_plr\" size=30 maxlength=255></p>
    
    <p><strong>3rd pers plr</strong><br>
    <input type=\"text\" name=\"t_plr\" size=30 maxlength=255></p>
    
    <input type=\"hidden\" name=\"addverb\" value=\"add\">  
    
    <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
    </form>";
}
?>
<html>
<head>
<title>Add an Entry</title>
</head>
<body>
<?php echo $display_block;?>
</body>
</html>
 

Re: Script not generating my html code as expected

Posted: Fri Feb 08, 2008 4:29 pm
by RobertGonzalez
You had all kinds of syntax errors in there, like not quoting post array indeces, checking a variable without using isset() or empty(), trying to reference a post var with $_POST(varname) and not echoing out the variables you set.

Try something like this and see if it works:

Code: Select all

<?php
if (isset($_POST['addverb']) && $_POST['addverb'] == "add") {
    // time to add to tables, so check for required fields
    if ( empty($_POST['verb_id']) || isset($_POST['tense_id']) ) {
        // The header here was totally going to go wobbly so I replaced it with die()
        die("1st pers sing and tense: addentry.php");
    }
    
    // connect to database
    $connection = mysql_connect("localhost", "guest","guest") or die(mysql_error());
    mysql_select_db("greekverbs", $connection) or die(mysql_error());
   
    // This entire block needs to be modifed
    // All of these will be set when the form is posted. They may be empty, but they are all set
    // That means that you are basically checking isset(), but you are doing it wrong.
    //
    // I will leave fixing this part up to you
    if (($_POST['verb_id']) || ($_POST['tense_id']) ||  ($_POST['f_sng']) || ($_POST['s_sng']) || ($_POST['t_sng']) || ($_POST['f_plr']) || ($_POST['s_plr']) || ($_POST['t_plr']))
    {
        $add_verb = "insert into conjugats values ('', '$_POST[verb_id]', '$_POST[tense_id]','$_POST[f_sng]', '$_POST[s_sng])'$_POST[t_sng]', $_POST[f_plr]', $_POST[s_plr]','$_POST[t_plr]')";
        mysql_query($add_verb) or die(mysql());
    }
    $display_block = "<h1>Entry added</h1>
    <p>Your entry has been added. Would you like to <a href=\"addentry.php\">add another</a>?</p>";
} else {
// Try to keep away from PHP_SELF
$thispage = basename(__FILE__);
 
$display_block = <<<DISPLAYBLOCK
    <h1>Add an entry</h1>
    <form method="post" action="$thispage">
   
    <p><strong>Verb ID</strong><br></p>
    <input type="text" name="verb_id" size=2>
   
    <p><strong>Tense ID</strong><br>
    <input type="text" name="tense_id" size=2></p>
   
    <p><strong>1st pers sing</strong><br>
    <input type="text" name="f_sng" size=30 maxlength=255></p>
   
    <p><strong>2nd pers sing</strong><br>
    <input type="text" name="s_sng" size=30 maxlength=255></p>
   
    <p><strong>3rd pers sing</strong><br>
    <input type="text" name="t_sng" size=30 maxlength=255></p>
   
    <p><strong>1st pers plr</strong><br>
    <input type="text" name="f_plr" size=30 maxlength=255></p>
   
    <p><strong>2nd pers plr</strong><br>
    <input type="text" name="s_plr" size=30 maxlength=255></p>
   
    <p><strong>3rd pers plr</strong><br>
    <input type="text" name="t_plr" size=30 maxlength=255></p>
   
    <input type="hidden" name="addverb" value="add"> 
   
    <p><input type="submit" name="submit" value="Add Entry"></p>
    </form>
DISPLAYBLOCK;
}
?>
<html>
<head>
<title>Add an Entry</title>
</head>
<body>
<?php echo $display_block;?>
</body>
</html>

Re: Script not generating my html code as expected

Posted: Sat Feb 09, 2008 3:25 pm
by timski72
Thanks for your suggestions. I've implemented some of them already and am having more success now. My html page is now being generated. I seem to be having problems with the part of the script I've pasted below though. When I step through the code it executes the mysql command and then displays $displayBlock to screen, which says I my record has been added. So it looks like it has added the record in my database but it hasn't. It looks like some error has occurred but no errors are being reported. Shouldn't the die(mysql()); output an error if there is an error? Any ideas?

Code: Select all

 
{ 
$add_verb = "insert into conjugats values ('$_POST[verb_id]', '$_POST[tense_id]' , '$_POST[f_sng]' , '$_POST[s_sng])' , '$_POST[t_sng]' , '$_POST[f_plr]' , '$_POST[s_plr]' , '$_POST[t_plr]')";
mysql_real_escape_string($add_verb) or die(mysql());
}
$displayBlock = "<h1>Entry added</h1>
<p>Your entry has been added. Would you like to <a href=\"addverb.php\">add another</a>?</p>";
 
Thanks,
Tim.

Re: Script not generating my html code as expected

Posted: Sat Feb 09, 2008 4:15 pm
by Christopher
First, you were not doing a query. Second, you need to filter all values that come from the Request, and escape all values that go to the database.

Code: Select all

 
{ 
// filter vars
$verb_id = intval($_POST['verb_id']);
 
// escape vars
$verb_id = mysql_real_escape_string($verb_id);
$add_verb = "insert into conjugats values ('$verb_id', '$_POST[tense_id]' , '$_POST[f_sng]' , '$_POST[s_sng])' , '$_POST[t_sng]' , '$_POST[f_plr]' , '$_POST[s_plr]' , '$_POST[t_plr]')";
mysql_query($add_verb) or die(mysql());
}
$displayBlock = "<h1>Entry added</h1>
<p>Your entry has been added. Would you like to <a href=\"addverb.php\">add another</a>?</p>";
 

Re: Script not generating my html code as expected

Posted: Sat Feb 09, 2008 4:27 pm
by califdon
At the risk of insulting you, I have to say that you seem to not have even a beginner's concept of how PHP or MySQL works. Before you try to do something slightly complicated, you need to work with very basic scripts, less than a page, that do some rudimentary things like connect to a database, do a SELECT query, etc. Otherwise you're just going to stumble from one error to the next without ever understanding what you're doing.

Re: Script not generating my html code as expected

Posted: Sun Feb 10, 2008 4:44 pm
by timski72
You're right califdon, I'm trying to run before I can walk.... or even crawl. Nevertheless, thanks to the advice and patience of you guys, I managed to get my script working. I'll take your advice and work on the basics more. Thanks for all your help.
Tim.

Re: Script not generating my html code as expected

Posted: Sun Feb 10, 2008 5:08 pm
by Christopher
Keep at it and keep asking questions. :)