-2

I am attempting to store an email address into a database. The column is set to varchar, 200 length, utf8-unicode-ci. Whenever I try to insert data that has an "@" in it, nothing happens. It does not return an error and it nothing else submits. It's like nothing happened. It does work when I just use letters and numbers. Is there something i'm missing? I've stored emails addresses before, but I don't recall doing anything special.

Thank you.

db table structure

the code for inserting the data.

$username = $_POST["user"];
$password = $_POST["pass"];
$email = $_POST["email"]; 
$friendCode = $_POST["friendCode"];
$rating = 0;

try{
    $dbh = new PDO('mysql:host=localhost;dbname=5iv', "kmessner", "##########6");
    $dbh->query('INSERT INTO users (username,password,email,friendcode,rating) VALUE ('.$username.','.$password.','.$email.','.$friendCode.','.$rating.')');

$dbh = null;
}
catch (PDOException $e){
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Kyle Messner
  • 111
  • 1
  • 1
  • 6
  • 1
    no sql, no code, no db details (though phpmyadmin implies mysql), ... no help. – Marc B Nov 29 '13 at 21:52
  • Like someone used to say "No ticky, no funny". – Funk Forty Niner Nov 29 '13 at 21:56
  • It's one of those "Guess & Win" @MarcB (I wonder what's today's prize) <=? – Funk Forty Niner Nov 29 '13 at 22:02
  • Kyle, it could be a number of things. Double-check what your column is set as. Another thing could be that you might have your query mixed up. Show us your code, table structure and anything you can think of that could help us "help you". We don't know what you have; you're there and we're not. ;-) – Funk Forty Niner Nov 29 '13 at 22:06
  • All your DB columns are in uppercase, and you're using lowercase values `username,password,email,friendcode,rating` could be it. Make all your values in uppercase, or change your DB columns to lowercase. – Funk Forty Niner Nov 29 '13 at 23:03
  • Plus use double quotes `$dbh->query("INSERT INTO users (username,password,email,friendcode,rating) VALUE ('.$username.','.$password.','.$email.','.$friendCode.','.$rating.')");` to wrap with. – Funk Forty Niner Nov 29 '13 at 23:04
  • It works when I don't use a symbol ('@') so I don't think the case of the columns matters. I changed it to double quotes, but it didn't make a difference either. Thank you for the response. – Kyle Messner Nov 29 '13 at 23:12

1 Answers1

1

The code supplied doesn't work at all for me - @ or no @, and was silently failing. Echoing out the query and putting it into PhpMyAdmin threw up an error (turned out the query was malformed, and data being inserted was not quoted at all).

I've tried this on a MAMP installation running PHP 5.4.

Changing this:

$dbh->query("INSERT INTO users (username,password,email,friendcode,rating) 
VALUE ('.$username.','.$password.','.$email.','.$friendCode.','.$rating.'));

To this (note the double quotes, and removing '.' works:

$dbh->query("INSERT INTO users (username,password,email,friendcode,rating) 
VALUES ('$username','$password','$email','$friendCode','$rating')");

Better still, don't concatenate raw post data into the query - try this instead:

$stmt = $dbh->prepare("INSERT INTO users (username,password,email,friendcode,rating) 
VALUES (?,?,?,?,?)");
$stmt->execute(array($username, $password, $email, $friendCode, $rating));

One other thing - your columns (as shown by your screenshot) are ascii_general_ci, not utf8

Hope this helps.

Panoctopus
  • 41
  • 3