Page 1 of 1

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

Posted: Sun Jun 30, 2002 11:08 pm
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. :(

Posted: Sun Jun 30, 2002 11:22 pm
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)

Posted: Mon Jul 01, 2002 4:46 am
by martin
The function name for connecting to your db is:
mysql_pconnect

Posted: Mon Jul 01, 2002 4:51 am
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

Posted: Mon Jul 01, 2002 7:35 am
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