Hi all
I have developed this code (for testing purposes) to enable a user to enter info into a form and by pressing the submit button, storing the new info in the relevant table. Though no errors come up no new data is added to the table. I can't see where the problem is.
I'am using PHP 4.3.1 + MySQL 3.23
Any feedback would be much appreciated. The code is below. Thanks,
<html>
<head>
<title>Adding user input to a database</title>
</head>
<body>
<?php
if ( isset( $name ) && isset( $address ) && isset( $website ) && isset( $transporttype ) )
{
// check user input here!
$dberror = "";
$ret = add_to_database( $name, $address, $website, $transporttype, $dberror );
if ( ! $ret )
print "Error: $dberror<BR>";
else
print "Thank you very much";
}
else {
write_form();
}
function add_to_database( $name, $address, $website, $transporttype, &$dberror )
{
$db = "transport";
$link = mysql_connect( "localhost", "root", "billabong" );
if ( ! $link )
{
$dberror = "Couldn't connect to MySQL server";
return false;
}
if ( ! mysql_select_db( $db, $link ) )
{
$dberror = mysql_error();
return false;
}
$query = "INSERT INTO transportorg ( name, address, website, transporttype )
values( '$name', '$address', '$website', '$transporttype' )";
if ( ! mysql_query( $query, $link ) )
{
$dberror = mysql_error();
return false;
}
return true;
}
function write_form()
{
global $submit_trans;
print "<form action=\"$submit_trans\" method=\"POST\">\n";
print "Transport Organisation";
print "<input type=\"text\" name=\"name\"><br> ";
print "Address";
print "<input TYPE=\"text\" name=\"address\"><br> ";
print "Web Site";
print "<input TYPE=\"text\" name=\"website\"><br> ";
print "Transport Type";
print "<select name=\"transporttype\">";
print "\t<option value=\"Bus-Coach\"> Bus-Coach\n";
print "\t<option value=\"Air\"> Air\n";
print "\t<option value=\"Rail\"> Rail\n";
print "</select>";
print "<input type=\"submit\" value=\"submit!\"></form>";
}
?>
</body>
</html>