PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
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):
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.
<?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>
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?
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.
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.