Question

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();
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top