Forum Moderators: coopster

Message Too Old, No Replies

Trying to send form info to DB

I am so new

         

ganderla

7:21 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I have a form that I am trying to send the info to a database. When I submit, I get to the thanks page, but the information is not in the database. Can someone please take a quick peek at my code?

<?

$title=$_POST['title'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$address1=$_POST['address1'];
$address2=$_POST['address2'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$degree=$_POST['degree'];
$interest=$_POST['interest'];
$checkbox=$_POST['checkbox'];

include("example_include.php");

$link=mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase") or die( "Unable to select database");

$query="INSERT INTO example (
clientid,
title,
fname,
lname,
address1,
address2,
city,
state,
zip,
phone,
email,
degree,
interest,
checkbox,
)

VALUES(
'',
'$title',
'$fname',
'$lname',
'$address1',
'$address2',
'$city',
'$state',
'$zip',
'$phone',
'$email',
'$degree',
'$interest',
'$checkbox',
)";

$result = mysql_query($query) or $errors[] = mysql_error();

header ("Location: [example.com...]

?>

dreamcatcher

7:43 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ganderla,

Are you getting error messages?

Try removing the two comma`s after 'checkbox' and '$checkbox'. You dont need a comma after the last value. See if that helps.

dc

ganderla

7:51 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That worked. Thank you so much.
I thought I was going crazy not getting it.
You just helped me finish my first real project.

ScottYardley

9:40 pm on Jan 14, 2005 (gmt 0)

10+ Year Member





header ("Location: http://example.com/thanks.html");


This is causing you to redirect instead of displaying the sql errors.

Since it is a sql error and not a php error the php is not stopping. Its going ahead with the redirecting.

Temporary rem out the redirect so you can see the sql errors. Better yet make your mysql_query echo the sql on die so that you see where your sql syntax is off.

something like this


$result = mysql_query($query) or die ('Query Failed<br>'.$sql.'<br>'.mysql_error().'<br>');

jusdrum

9:51 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



If you use PEAR's DB module, life is much easier. PEAR typically comes stock installed with PHP (see [pear.php.net...] for more details).

require_once("DB.php");

$dbtype = "mysql";
$dbserver = "localhost";
$dbuser = "myuser";
$dbpassword = "mypassword";
$dbdatabase = "mydatabase";
$dsn = "$dbtype://$dbuser:$dbpass@$dbserver/$dbdatabase";

$db =& DB::connect($dsn);

if (DB::isError($db))
{
print($db->getMessage());
exit;
}

$MyTable = "table";

# AutoExecute takes an array of $Name => $Value pairs and makes an insert for you...perfect for $_POST (if all the fields posted are in the db)
$Result = $db->autoExecute($MyTable,$_POST,DB_AUTOQUERY_INSERT);

I usually put all my form fields in an array by doing name="FormField[FieldName]" for each field, then passing $_POST['FormField'] into autoExecute, that way if I have some values in the form that aren't in the db, they won't get thrown in there and cause errors.

Check out PEAR's DB module, it makes everything so much easier and cleaner.

[pear.php.net...]

dreamcatcher

12:05 pm on Jan 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That worked. Thank you so much. I thought I was going crazy not getting it. You just helped me finish my first real project.

Cool. Sometimes its the little things you miss. :)

Good advice is also offered from the previous two posts.