Portal Home > Knowledgebase > Articles Database > PHP form to mysql database Help!
PHP form to mysql database Help!
Posted by 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.
$link = mysql_connect ( "localhost", "username" , "password" );
$db = "database";
if ( ! $link )
die( "couldn't connect to mysql");
print " successfully connect to mysql ";
mysql_select_db( $db, $link )
or die ( " couldn't connect to $db: " .mysql_error() );
print "successfully slected database
" ;
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 ";
else
print "thanks";
} 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 "";
print "\n";
print "";
}
?>;
click here to go back to the main menu
|
Posted by 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
|
Posted by 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....
|
Posted by mikey1090, 03-14-2007, 08:49 AM |
form method=\"post\" action=\"$php_self\"> write that
|
Posted by 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.
|
Posted by Instagator, 03-14-2007, 09:19 AM |
ok will try that when I get home
Thanks guys
|
Posted by 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
|
Posted by innova, 03-14-2007, 09:46 AM |
mmm I love spaghetti.
|
Posted by 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
|
Posted by mikey1090, 03-14-2007, 10:25 AM |
put an echo inside every block of code
such as
echo "if statement is working"';
|
Posted by Instagator, 03-14-2007, 11:12 AM |
Ok thanks!
|
Posted by Ks Jeppe, 03-14-2007, 11:35 AM |
See below
|
Posted by Ks Jeppe, 03-14-2007, 11:36 AM |
Not true sincehe has register globals on
Edit: Ok, forums hate me today
|
Posted by Instagator, 03-14-2007, 12:30 PM |
Ok.. you guys got me confused.. so Do I need to do the _post thing or not?
|
Posted by 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\">
|
Posted by Ks Jeppe, 03-14-2007, 04:01 PM |
So.. does it work or not?
|
Posted by 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
|
Posted by 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.
|
Posted by Jatinder, 03-14-2007, 10:07 PM |
Below is the fixed code. I have fixed some typos and some logical errors.
|
Posted by 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
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.
|
Posted by mikey1090, 03-15-2007, 03:49 AM |
glad to know it works
|
Posted by Burhan, 03-15-2007, 08:23 AM |
Curious as to why there is $php_self, when PHP already provides a $_SERVER['PHP_SELF']
|
Posted by 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.
|
Add to Favourites Print this Article
Also Read
Exim problem (Views: 595)