How to save PHP HTTP_USER_AGENT in MySQL field

I have a simple PHP script feedback form that I would like to improve by adding the $ _SERVER [HTTP_USER_AGENT] data to the row in the database that I save.

I keep getting parsing errors when trying a simple insert, passing '$ _SERVER [HTTP_USER_AGENT]' as a typical string. Should I bind it somehow so that the characters used in this server variable do not cause such errors?

(The INSERT query works without this field, by the way.)

Thank.

+5
source share
3 answers

, ', .

User-Agent, PHP, , , , . , //, , SQL-. , (, mysql_real_escape_string(). , . , .

+6

mysql_query("
INSERT INTO
    db_table
VALUES (
    ...
    '" . mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']) . "'
    ...
)");

? ? ?

+2

, .
,

:

$agent = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
$sql   = "INSERT INTO `table` set useragent = '$agent'";
$res   = mysql_query($sql) or trigger_error(mysql_query.$sql);

, . .

0

All Articles