PDA

View Full Version : Database query help


EquinoxWorld
08-24-2013, 05:16 PM
Hello everyone,

It's been a while since I coded anything and I was wondering what's wrong with this code snippet. I'm trying to insert a username of a member into a certain column in a table in the database. I'm using this code:

$vbulletin->db->query_write("INSERT INTO 'table' ('column') VALUES ('".$vbulletin->userinfo['username']."')");

Every time I get a (1064) database error.

Am I missing anything here?? Thanks everyone.

kh99
08-24-2013, 07:28 PM
The quotes around the table name and column name should be backquotes and not apostrophes (single quotes). Also, you should use the escape_string() function on the username before including it, or else if a username has a special character it will cause an error. So maybe this:

$vbulletin->db->query_write("INSERT INTO `table` (`column`) VALUES ('".$vbulletin->db->escape_string($vbulletin->userinfo['username'])."')");

EquinoxWorld
08-24-2013, 11:27 PM
That worked perfectly thanks kh99. :)

Paul M
08-25-2013, 01:59 PM
You dont need the backticks, you could remove them completely.