Web Hosting Talk







View Full Version : PHP form to mysql database Help!


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>

mikey1090
03-14-2007, 08:11 AM
you need to add an action property to your form for a start, next try put the functions at the top of the script - before they are called

then check if register globals is on or off

Instagator
03-14-2007, 08:18 AM
Well Globals are on .. And the action is $php_self ...

It should work.. I had it working before but not anymore...
Sorry, I'm a newbie when it comes to php.. forms i have never gotten down even in html....

mikey1090
03-14-2007, 08:49 AM
<form method=\"post\" action=\"$php_self\"> write that

mitchlrm
03-14-2007, 09:17 AM
You haven't referred to the form variables correctly.

Instead of isset($line1) use isset($_post["line1"])

You don't need action in the form

As a general tip, put some echo statements into to help debug the code.

Instagator
03-14-2007, 09:19 AM
ok will try that when I get home
Thanks guys

mikey1090
03-14-2007, 09:27 AM
yeah

when i code, i use echo or die lines everywhere, helps you to know which logical path is being processed, and which bits are working properly

innova
03-14-2007, 09:46 AM
mmm I love spaghetti.

Instagator
03-14-2007, 10:01 AM
So how would you guys use echo in various places to determine where its failing?
Sorry, I'm a total newb when it comes to coding but learning :)

mikey1090
03-14-2007, 10:25 AM
put an echo inside every block of code

such as

echo "if statement is working"';

Instagator
03-14-2007, 11:12 AM
Ok thanks!

Ks Jeppe
03-14-2007, 11:35 AM
See below :P

Ks Jeppe
03-14-2007, 11:36 AM
You haven't referred to the form variables correctly.

Instead of isset($line1) use isset($_post["line1"])

You don't need action in the form

As a general tip, put some echo statements into to help debug the code. Not true sincehe has register globals on :)

Edit: Ok, forums hate me today :S

Instagator
03-14-2007, 12:30 PM
Ok.. you guys got me confused.. so Do I need to do the _post thing or not?

Instagator
03-14-2007, 01:21 PM
Ok.. I'll remove global and do the following:

Instead of isset($line1) use isset($_post["line1"])

and

form method=\"post\" action=\"$php_self\">

Ks Jeppe
03-14-2007, 04:01 PM
So.. does it work or not?

Instagator
03-14-2007, 04:07 PM
nope.. i tried it.. it didn't.. it almost as it refreshes itself onto a new screen..

I changed both the isset and the action .. same exact result

Instagator
03-14-2007, 04:11 PM
looks like its getting stuck at the add_to_db function... but it doesn't go through that function at all cuz i placed echo' everywhere.

Jatinder
03-14-2007, 10:07 PM
Below is the fixed code. I have fixed some typos and some logical errors.


<html>
<head>
</head>
<body>
<?php
$link = mysql_connect ( "localhost", "jatinder" , "deemon" );
$db = "temp";
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 (!empty($_POST['line1']) && !empty ($_POST['line2']) && !empty($_POST['line3']) && !empty($_POST['line4']) && !empty($_POST['line5']) && !empty($_POST['line6'])){
//check input here!
$dberror =" ";
$ret = add_to_db($_POST['line1'], $_POST['line2'], $_POST['line3'], $_POST['line4'], $_POST['line5'], $_POST['line6']);
if ($ret)
print "<center>thanks</center>";
}
else{
write_form();
}

function add_to_db( $line1, $line2, $line3, $line4, $line5, $line6 ){
global $link;

$query = "INSERT INTO info (line1, line2, line3, line4, line5, line6) values ('$line1','$line2','$line3','$line4','$line5','$line6')";
if (! mysql_query($query, $link)){
echo mysql_error();
return false;
};
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>

Instagator
03-15-2007, 12:35 AM
ok.. Thanks
I found the problem in the original code and that was my error. the original coded post does work but global variables have to be turned on. (needless to say the production server did not have this enabled)

The above script posted complete doesn't require the global stuff and now I see how its suppose to be coded.

so both work.. but I'll stick to the one posted above :P

Thanks goes out to all you peeps that helped me out. i really appreciate it.. I'm sure I'll be asking for more assistance in the near future

Thanks again guys.. really helped me greatly.

mikey1090
03-15-2007, 03:49 AM
glad to know it works:)

Burhan
03-15-2007, 08:23 AM
Curious as to why there is $php_self, when PHP already provides a $_SERVER['PHP_SELF'] :)

Extreme43
03-17-2007, 06:58 AM
Ok, firstly to debug your code - run everything locally and use error_reporting to determine the problem and where abouts the problems is. Check out xampp for a fast setup. Trial and error using echo statements everywhere is just a huge waste of time.

Secondly, i do not know why this has not been mentioned... but you need to secure every single bit of data that is inserted from the public. Your database could be easily compromised the way it is now.

And just a tip, in your situation i would use echo with single quotes as it will run much faster. Double quotes will make PHP search for any variables within the string, where as single quotes will not do any checks for variables. Also, using multiple print functions will also slow down your script as PHP needs to call that function each time. Use one echo() function containing the whole lot at once, you will produce much faster results.

Best of Luck.