Ok, i asked a fellow PHP coder on this one and he couldnt' find what may be causing this either. Here is the code below if someone can take a look at it and see if they can tell me why it won't work. The variable name is correct and is loaded from config.php because i can put in a echo for it where the print "blahblah" stuff is at the beginnign there, but if i put it inside like the write form stuff, etc it won't work. If I hardcode in the name of the database and connection info it DOES work. Anyway, here's the code.
<?
require("config.php");
print "<html>";
print "<head>";
print "<title> Add Term to Database </title>";
print "</head>";
print "<body>";
if ( isset( $term ) && isset( $definition ) && isset( $submiter ) )
{
// user input needs to be checked here!!!
$dberror = "";
$ret = add_to_database( $term, $definition, $submiter, $dberror );
if ( ! $ret )
print "<font face='$errorfont' size='$errorsize' color='$errorcolor'>Error: $dberror</font><br>";
else
print "Term added!<br><br>";
print "<a href='add.php'> Add another</a><br><br>";
print "<center><b>DO NOT REFRESH THIS PAGE, IT WILL ADD THE TERM AGAIN!<br>";
print "This will be fixed later, possibly</center>";
}
else {
write_form();
}
function add_to_database( $term, $definition, $submiter, &$dberror )
{
if ( ! $link )
{
$dberror = "<font face='$errorfont' size='$errorsize' color='$errorcolor'>Couldn't connect to MySQL server</font>";
return false;
}
if ( ! mysql_select_db( "$databasename", $link ) )
{
$dberror = mysql_error();
return false;
}
$query = "INSERT INTO $databasetable ( term, defin, submiter )
values( '$term', '$definition', '$submiter' )";
if ( ! mysql_query( $query, $link ) )
{
$dberror = mysql_error();
return false;
}
return true;
}
function write_form()
{
global $PHP_SELF;
print "DATABASE: $databasename";
print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
print "<input type=\"text\" name=\"term\"> ";
print "Term to input<p>\n";
print "<input type=\"text\" name=\"submiter\"> ";
print "Submitted by<p>\n";
print "<TEXTAREA name=\"definition\" WRAP=virtual rows=4 cols=20>\n";
print "</textarea>";
print "Definition\n";
print "<br><br><input type=\"submit\" value=\"Add!\">\n</form>\n";
print "$databasename\n";
}
print "</body>";
print "</html>";
?>
There is no error outputted when loading the script, it shows the form. When submitting something it shows an error of No Database selected, or sometimes even simply Error:
Thanks,
Lee