Jump to content

Recommended Posts

Hi, I can't figure out why it won't add the record to the database.  It's just a simple form to get name and email but when I hit submit I get the "or die" message. 

$Fname = $_GET['Fname'];
$eMail = $_GET['eMail'];
$submit = $_GET['submit'];

if ($submit == 1) 
{

$date = date("Y-m-d");

$sql="INSERT INTO `my_db`.`my_table` (
`usrID` ,
`usrName` ,
`usrEmail` ,
`usrDatereg`
)
VALUES (
NULL , '$Fname', '$eMail', '$date'
)";

$adduser=mysql_query($sql) or die ('Could not add you to mailing list');

$message = "Welcome to the mailing list! ";

}

 

It's definitely getting all the variables from the form, I tested by printing them as part of the $message variable but it just isn't adding.

 

Any thoughts?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/180824-not-adding-to-db/
Share on other sites

within the or die() put mysql_error() and post what it says.

 

It's usually not suggested to use back ticks unless you're using reserved words, in which case they're required. Also, if 'usrID' is an auto_increment field there is no need to include it in the query, it'll be inserted automatically.

Link to comment
https://forums.phpfreaks.com/topic/180824-not-adding-to-db/#findComment-953968
Share on other sites

So here's what I get now:

 

"Could not add you to mailing list

sql:INSERT INTO 'mailing' ('usrName','usrEmail','usrDatereg') VALUES (bob,bob@mail.com,2009-11-09)

error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''mailing' ('usrName','usrEmail','usrDatereg') VALUES (bob,bob@' at line 1". 

 

When I look at the manual I still can't figure out the problem.  The version of mysql is 4.1.22

 

My form is GET

 

I changed the back ticks to regular single quotes.

I added mysql_real_escape() to sanitize the data. 

I echoed $sql and all the data looks fine.

Here's the code as it stands now.

 

$Fname = mysql_real_escape_string($_GET['Fname']);
$eMail = mysql_real_escape_string($_GET['eMail']);
$submit = mysql_real_escape_string($_GET['submit']);

if ($submit == 1) 
{

$date = date("Y-m-d");

$sql="INSERT INTO 'mailing' ('usrName','usrEmail','usrDatereg')
VALUES ($Fname,$eMail,$date)";

$adduser=mysql_query($sql) or die ('Could not add you to mailing 

list<br/>'.'sql:'.$sql.'<br/>error:'.mysql_error());

$message = "Welcome to the mailing list! $Fname, $eMail, $date.";

}

Link to comment
https://forums.phpfreaks.com/topic/180824-not-adding-to-db/#findComment-954102
Share on other sites

Well you changed the sql:

$sql="INSERT INTO `my_db`.`my_table` (
`usrID` ,
`usrName` ,
`usrEmail` ,
`usrDatereg`
)
VALUES (
NULL , '$Fname', '$eMail', '$date'
)";

 

to

sql="INSERT INTO 'mailing' ('usrName','usrEmail','usrDatereg')
VALUES ($Fname,$eMail,$date)";

 

Which one is correct? Because I'd imagine the first one is failing because it isn't selecting the right database/table... and the second one because you're using single quotes where backticks would be appropriate (but not needed in this case) and missing single quotes where you need them - around the values.

 

sql="INSERT INTO mailing (usrName,usrEmail,usrDatereg)
VALUES ('$Fname','$eMail','$date')";

 

Link to comment
https://forums.phpfreaks.com/topic/180824-not-adding-to-db/#findComment-954257
Share on other sites

Upon further investigation I have found that I'm not connecting to the db.  I know that I have the right dbname, username and password but the error message says "access denied."  Here's the connection code:

 

$db_user="user";
$db_pword="password";
$db_host="localhost";
$db_name="database";

$connection = mysql_connect($db_host, $db_user, $db_pword)
or die ("could not connect to server");

$db = mysql_select_db($db_name, $connection)
or die ("could not connect to database".mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/180824-not-adding-to-db/#findComment-954505
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.