Instagator
03-14-2007, 07:54 AM
Long Time reader - First time poster :)
Hello all, I made a PHP form to obtain information for a client (6 fields) and then put that information in a mysql database. It doesn't work and I cant figure out why. Basically when the submit button is pressed, the page refreshes and clears of all information. So it doesn't even reach the mysql server and for the life of me I can't figure it out.
Here is basically the code.
<html>
<head>
</head>
<body>
<?php>
$link = mysql_connect ( "localhost", "username" , "password" );
$db = "database";
if ( ! $link )
die( "couldn't connect to mysql");
print "<center> successfully connect to mysql <br>";
mysql_select_db( $db, $link )
or die ( " couldn't connect to $db: " .mysql_error() );
print "successfully slected database <br><br></center> " ;
if (isset ($line1) && isset ($line2) && isset($line3) && isset($line4) && isset($line5) && isset($line6)){
//check input here!
$dberror =" ";
$ret = add_to_db($line1, $line2, $line3, $line4, $line5, $line6);
if ( ! $ret )
print "error: $dberror<br>";
else
print "<center>thanks</center>";
} else{ write_form();
}
function add_to_db( $line1, $line2, $line3, $line4, $line5, $line6 ){
$query = "insert INTO info (line1, line2, line3, line4, line5, line6) values ('$line1','$line2','$line3','$line4','$line5','$line6)";
$link = mysql_connect ( "localhost", "username" , "password" );
if (! mysql_query($query, $link)){
$dberror = mysql_error();
};
return true;
}
function write_form(){
global $php_self;
print "<center>";
print "<form method=\"post\">\n";
print "<input type=\"text\" name=\"line1\">";
print "Please Enter Line 1 input<BR>";
print "<input type=\"text\" name=\"line2\">";
print "Please Enter Line 2 input<BR>";
print "<input type=\"text\" name=\"line3\">";
print "Please Enter Line 3 input<BR>";
print "<input type=\"text\" name=\"line4\">";
print "Please Enter Line 4 input<BR>";
print "<input type=\"text\" name=\"line5\">";
print "Please Enter Line 5 input<BR>";
print "<input type=\"text\" name=\"line6\">";
print "Please Enter Line 6 input<BR>";
print "<input type=\"submit\" value=\"submit\">\n</form>\n";
print "</center>";
}
?>;
<br><br><center>
<a href="index.php">click here to go back to the main menu</a><br></center>
</p>
</form></center>
</body>
</html>
Hello all, I made a PHP form to obtain information for a client (6 fields) and then put that information in a mysql database. It doesn't work and I cant figure out why. Basically when the submit button is pressed, the page refreshes and clears of all information. So it doesn't even reach the mysql server and for the life of me I can't figure it out.
Here is basically the code.
<html>
<head>
</head>
<body>
<?php>
$link = mysql_connect ( "localhost", "username" , "password" );
$db = "database";
if ( ! $link )
die( "couldn't connect to mysql");
print "<center> successfully connect to mysql <br>";
mysql_select_db( $db, $link )
or die ( " couldn't connect to $db: " .mysql_error() );
print "successfully slected database <br><br></center> " ;
if (isset ($line1) && isset ($line2) && isset($line3) && isset($line4) && isset($line5) && isset($line6)){
//check input here!
$dberror =" ";
$ret = add_to_db($line1, $line2, $line3, $line4, $line5, $line6);
if ( ! $ret )
print "error: $dberror<br>";
else
print "<center>thanks</center>";
} else{ write_form();
}
function add_to_db( $line1, $line2, $line3, $line4, $line5, $line6 ){
$query = "insert INTO info (line1, line2, line3, line4, line5, line6) values ('$line1','$line2','$line3','$line4','$line5','$line6)";
$link = mysql_connect ( "localhost", "username" , "password" );
if (! mysql_query($query, $link)){
$dberror = mysql_error();
};
return true;
}
function write_form(){
global $php_self;
print "<center>";
print "<form method=\"post\">\n";
print "<input type=\"text\" name=\"line1\">";
print "Please Enter Line 1 input<BR>";
print "<input type=\"text\" name=\"line2\">";
print "Please Enter Line 2 input<BR>";
print "<input type=\"text\" name=\"line3\">";
print "Please Enter Line 3 input<BR>";
print "<input type=\"text\" name=\"line4\">";
print "Please Enter Line 4 input<BR>";
print "<input type=\"text\" name=\"line5\">";
print "Please Enter Line 5 input<BR>";
print "<input type=\"text\" name=\"line6\">";
print "Please Enter Line 6 input<BR>";
print "<input type=\"submit\" value=\"submit\">\n</form>\n";
print "</center>";
}
?>;
<br><br><center>
<a href="index.php">click here to go back to the main menu</a><br></center>
</p>
</form></center>
</body>
</html>
