Tom Calland Tom Calland - 1 year ago 72
MySQL Question

Whitespace error with old MySQL code

Recently, I've been trying to code a forum from various old tutorials on the internet, However I've recently reached a problem - Although I've done exactly as the tutorial has said, I receive a whitespace error. I presume this may be because some MySQL commands may have changed. If anyone could assist me in telling me what is incorrect and how to fix it, Please do so!

25: $sql = "ìINSERT INTO sections(sect_name, sect_description)
26: VALUES('' . mysql_real_escape_string($_POST['sect_name']) . ì',
27: '' . mysql_real_escape_string($_POST['sect_description']) . ì')'";
28: $result = mysql_query($sql);

The error message is:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in [Name of directory] on line 26

Answer Source

You can do it easily:

 $sql = sprintf("INSERT INTO sections(sect_name, sect_description)
    VALUES('%s', '%s');"
 , mysql_real_escape_string($_POST['sect_name']) . i
 , mysql_real_escape_string($_POST['sect_description']) . i);

The '%s' are replaced with the variables from the sprintf function. That way it's easier to read than concatenating the whole string.