Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

php form -> mysql problem (please help!)

Codes here !

Moderators: macek, egami, gesf

php form -> mysql problem (please help!)

Postby SilverX » Sun Mar 23, 2003 12:32 pm

No matter what I try I can't seem to get the data from my form into my db. This is a slightly simplified version of what I'm doing:

Code: Select all

<?php

print_r($HTTP_POST_VARS);

$dbh = mysql_connect ("localhost", "ascendan_cat", "paws") or die ( 'I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("ascendan_pets") or die (mysql_error());

$result = mysql_query ("INSERT INTO pets (Name, Fav food) values ('$petname','$ffood')", $dbh);

?>

<form method="post" action="<?php echo $PHP_SELF?>">
Pet Name: <input type="text" name="petname"><br>
Favourite Food: <input type="text" name="ffood"><br>
<input type="submit" name="submit" value="Enter information">
</form>


print_r($HTTP_POST_VARS); (when it's on) shows the data is THERE. It's just not getting into the db. :(

Please help? I have plans for a neat script but I'm already stuck on this basic thing.

Many thanks in advance.
User avatar
SilverX
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Mar 23, 2003 12:22 pm

Postby Xerpher » Sun Mar 23, 2003 5:37 pm

That's because your using the ancient way of calling form variables which is both a security whole and no longer works... Try this:

Code: Select all
<?php
$dbh = mysql_connect ("localhost", "ascendan_cat", "paws") or die ( 'I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("ascendan_pets") or die (mysql_error());
$result = mysql_query ("INSERT INTO pets (Name, Fav food) values ('".$_POST['petname']."','".$_POST['ffood']."')", $dbh);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
Pet Name: <input type="text" name="petname"><br>
Favourite Food: <input type="text" name="ffood"><br>
<input type="submit" name="submit" value="Enter information">
</form>
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby SilverX » Mon Mar 24, 2003 4:54 am

Xerpher wrote:That's because your using the ancient way of calling form variables which is both a security whole and no longer works... Try this:


Thanks for your help; but it didn't work. Same problem. :\
User avatar
SilverX
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Mar 23, 2003 12:22 pm

Postby mammal » Wed Apr 02, 2003 2:16 am

Try this:

<?php

if ($ffood AND $petname) {

$db = mysql_connect ("localhost", "ascendan_cat", "paws");
$db_select = mysql_select_db ("ascendan_pets");
if (!$db){ echo "DB Connection Failure";}
if (!$db_select){ echo "DB Selection Failure";}

$query = "INSERT INTO pets (Name, Fav food) VALUES ('$petname', '$ffood')";

// echo $query;
/* Uncomment the echo command above to see the SQL query printed on the screen. If the variables are missing you may have global_variable turned ON there you can try $HTTP_POST_VARS[petname] AND $HTTP_POST_VARS[ffood] instead. */

if ($result = mysql_query ($query)) {
echo "Information Inserted";
} else {
echo "Database Error Occured!";
}
}
?>
<form method="post" action="<?=$PHP_SELF ?>">
Pet Name: <input type="text" name="petname"><br>
Favourite Food: <input type="text" name="ffood"><br>
<input type="submit" name="submit" value="Enter information">
</form>
User avatar
mammal
New php-forum User
New php-forum User
 
Posts: 37
Joined: Wed Apr 02, 2003 1:31 am
Location: Hythe, UK

Postby Redcircle » Wed Apr 02, 2003 5:57 pm

Try this code.
Code: Select all
<?php

$dbh = mysql_connect ("localhost", "ascendan_cat", "paws") or die ( 'I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("ascendan_pets") or die (mysql_error());

$result = mysql_query ("INSERT INTO pets (Name, Fav food) values ('".$_POST['petname']."','".$_POST['ffood']."')", $dbh);

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Pet Name: <input type="text" name="petname"><br>
Favourite Food: <input type="text" name="ffood"><br>
<input type="submit" name="submit" value="Enter information">
</form>
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.