2

I have a site on host gator. I can connect with my pdo statement but the statement for the insert doesnt seem to work. Right now I have defined the values but i plan to use variabled pulled from a $_POST from a form on the previous page.

<?php

/*** mysql hostname ***/
$hostname = 'xxx.xxx.xxx.xxx';

/*** mysql username ***/
$username = 'pressgym_admin';

/*** mysql password ***/
$password = '*******';  <-started out on purpose

try {
$dbh = new PDO("mysql:host=$hostname;dbname=pressgym_press", $username, $password);
/*** echo a message saying we have connected ***/
$qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES (?, ?, ?, ?');
$qry->execute(array('Brandon', '[email protected]', 'test message', '3.12.12'));

echo 'entry succesfull';

}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

   describe contact;
   Name varchar(255)    NO  PRI     
   EmailAddress varchar(255)    NO          
   Message  longtext    NO          
   Date varchar(255)    YES 
4
  • 3
    Enable PDO Exceptions with $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Commented Apr 24, 2012 at 17:19
  • 3
    I would change the host name to something fake. If that's a real IP, you're just asking for abuse.... Commented Apr 24, 2012 at 17:20
  • The only proper answer for some reason added as a comment. Commented May 4, 2012 at 4:39
  • @MikeB 's comment fixed the issue I had. Disabling exceptions by default seems like an awful design decision. It creates a scenario where PDO is the cryptic wife ("No, it's fine.") and PHP is the oblivious husband ("Fine? Well all right! Onto the next thing..."). Commented Jan 20, 2015 at 4:01

2 Answers 2

5

The SQL syntax in your prepare command contains errors:

qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES (?, ?, ?), ?');

should be

qry = $dbh->prepare('INSERT INTO contact (Name, `Email Address`, Message, Date) VALUES (?, ?, ?, ?)');
Sign up to request clarification or add additional context in comments.

13 Comments

wow you were quick ;) i saw that after i posted it, still doesnt work
@BrandonBraner: I've updated my answer to reflect that the identifier for the Email Address column needs to be quoted in order to handle the space it contains.
aaw my bad. i didn't notice the space, and assumed it to be another column
@IbrahimAzharArmar: Credit where it's due, I only noticed it after reading your answer! +1
+1 for the answer. and @Brandon Braner, try not to use space ( ) in column names.
|
2

you have a syntax error. the following line

$qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES(?, ?, ?), ?');

should be

$qry = $dbh->prepare('INSERT INTO contact (Name, Email Address, Message, Date) VALUES (?, ?, ?, ?)');

Update:

your column name Email Address contains a space escape it by using proper quote identifier like

INSERT INTO contact (Name, `Email Address`, Message, Date) VALUES (?, ?, ?, ?)'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.