help..when I post a simple form to mysql, I get a blank page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gilbertwang
Forum Commoner
Posts: 32
Joined: Sun Jun 30, 2002 11:08 pm
Location: Calgary

help..when I post a simple form to mysql, I get a blank page

Post by gilbertwang »

Please help me to look at this code!

<?
if (!$txtname || !$txtaddress || !$txtemail)
{echo "You have not entered all the fields.<br>"
."Please go back and try again";
exit;
}

$txtname = addslashes($txtname);
$txtaddress = addslashes($txtaddress);
$txtemail = addslashes($txtemail);

@ $db = my_sqlpconnect("hostname", "username", "pass");
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}

mysql_select_db("gossippl_cuppa");

$query = "insert into question values
('".$txtname."','".$txtaddress."','".$txtemail."')";

$result = mysql_query($query);
if ($result)
echo mysql_affected_rows()."book inserted into database.";
?>

I did not receive any error and when I check the database on the server, it did not insert a new record. why didn't it give me any error if it did not work. :(
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

$query = "insert into question values
('".$txtname."','".$txtaddress."','".$txtemail."')";


you need to specify the columns.. like

insert into table (field, field2) values (value, value1)
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

The function name for connecting to your db is:
mysql_pconnect
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Good catch martin, although if you don't need a persistant connection to the database mysql_connect() would be the better function to use. Although in case that was just a typo, have you tried:

Code: Select all

$result = mysql_query($query) or die(mysql_error());

Mac
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

hob_goblin wrote:$query = "insert into question values
('".$txtname."','".$txtaddress."','".$txtemail."')";


you need to specify the columns.. like

insert into table (field, field2) values (value, value1)
Not strictly true, you only need to specify the columns if you are not inserting into all columns in your table, i.e. if you only have those 3 columns in your table then your insert will work.

You don't need to do all of that concatenation

$query = "insert into question values
('$txtname','$txtaddress','$txtemail')";

will work, because you use variable names within double quotes PHP knows to evaluate them.

Another thing to check is that if you have any not null fields defined, make sure all your variables have values for those fields.

Mike
Post Reply