Having problems entering data into MySQL database

A

Anonymous

Guest
I am making a members area of my site. I can connect to MySQL but I cannot add users to it. Here is the code for my form handler that does the work. Only names have been changed.

Code:
<?
include("header.txt");

include("constantfile.file");

//connect to database
$con = mysql_connect ($g_databasehost,$g_dbuid,$g_dbpwd);

if ($con==NULL)
{
echo("301 Couldn't connect to MySQL\n\n");
exit(-1);
}

$db = mysql_select_db ($g_databasename,$con);

$t="INSERT INTO users (userid,username,password,FirstName,LastName,email)" . "VALUES ('', '$username', '$password', '$FirstName', '$LastName', '$email')";
$sql=sprintf ($t, mysql_escape_string ($username), mysql_escape_string ($password), mysql_escape_string ($FirstName), mysql_escape_string ($LastName), mysql_escape_string ($email));


//debug
//echo($sql);
mysql_query ($sql,$con);

//autoresponder to visitor
mail ($email,"Welcome to our Member Area!","Thank You for submiting your information.");
?>

I have been working on this problem for several days now and would appreciate any help.

Thanks,
Lee
 
This monstrosity seems to be the trouble:

guitchicken said:
PHP:
<?php
$t="INSERT INTO users (userid,username,password,FirstName,LastName,email)" . "VALUES ('', '$username', '$password', '$FirstName', '$LastName', '$email')";
$sql=sprintf ($t, mysql_escape_string ($username), mysql_escape_string ($password), mysql_escape_string ($FirstName), mysql_escape_string ($LastName), mysql_escape_string ($email));
?>

Eww. I'm not sure what you expected to accomplish with that sprintf(), but I can tell you it's not doing a damn thing. Try this:

PHP:
<?php
$sql = "INSERT INTO users (userid, username, password, FirstName, LastName, email) VALUES ('', '" . $username . "', '" . $password . "', '" . $FirstName . "', '" . $LastName . "', '" . $email . "')";

// If you have escape issues, you only need to use mysql_escape_string once
$sql = mysql_escape_string($sql);
?>
 
Thanks for the help. Now I have a new problem. In the section of the code I wrote there is now a parse error on line 24 which would be the line that says:

Code:
mysql_query ($sql,$con);

I don't know what is wrong but I would appreciate any help. I am new to php and have never tried a members area before.

Thanks,
Lee
 
If you're new to PHP, I highly recommend forgetting about MySQL for now until you have a very thorough grouding in the language.

Parse errors usually happen before the line that they're reported, and I can guess that your problem is probably a mismatched quotation mark. Go through your code and make sure every quotation mark has a mate. The easiest way to catch parse errors before they happen is by downloading a syntax-highlighting text editor. Look in this thread.
 
I fixed the parse error but I still can't get it to write to the database.
 
Back
Top