This should be so simple but it’s driving me crazy. I’m trying to insert some values into a database from some form fields, but only decimals are being saved. I’m very confused.
For example, this will echo out correct values from the form fields, so I know those values are being passed along…
echo "Trying to insert $posttime, $name, $agency, $message";
// The above correctly outputs this: Trying to insert 1344548784, Mike, Test, Test
But the SQL query below only works if I use numbers in the text fields, nothing else works. (I’m using phpmyadmin to confirm)
mysql_query("INSERT INTO mytable (id, from_name, from_agency, message) VALUES ($posttime, $name, $agency, $message)");
As a test, this query DOES work when I hard-code the values. I’m so confused…
mysql_query("INSERT INTO mytable (id, from_name, from_agency, message) VALUES (1344548784, 'Mike', 'Test', 'Test')");
Encapsulate your text fields with quotes when inserting data. Even though they are variables in PHP, you need to tell the database that they are strings – which is done with quotes.:
Also, the
mysql_*functions really are a bit dated, you should really look at moving to PDO or mysqli.