mrhobbz Posted August 20, 2008 Share Posted August 20, 2008 I'm trying to run this query: $sql = "INSERT INTO i_usr (NULL, usrname, usrpass, usremail, usrhandle) VALUES (NULL, '$username, '$password, '$email', '$handle')"; This is the error it spits out: Error : MySQL - Database Query [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL usrname usrpass usremail usrhandle VALUES test 098f6bcd4621d373cade4e832627' at line 1 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/ Share on other sites More sharing options...
pocobueno1388 Posted August 20, 2008 Share Posted August 20, 2008 Take out the NULL. If you don't specify a field, it will enter a null automatically. So try this instead $sql = "INSERT INTO i_usr (usrname, usrpass, usremail, usrhandle) VALUES ('$username, '$password, '$email', '$handle')"; Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621307 Share on other sites More sharing options...
moselkady Posted August 20, 2008 Share Posted August 20, 2008 Try this one: $sql = "INSERT INTO i_usr VALUES (NULL, '$username, '$password, '$email', '$handle')"; Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621310 Share on other sites More sharing options...
mrhobbz Posted August 20, 2008 Author Share Posted August 20, 2008 Tried that, still nothing.. I'm passing it through this function: public function query($sql) { $sql = $this->clean_sql($sql); $this->act = @mysql_query($sql, $this->con); if(!$this->act) { $this->err('MySQL', 'Database Query'); } $this->affected_rows = mysql_affected_rows($this->con); } and it shoots the sql statement through this function: public function clean_sql($string) { $string = ereg_replace("[\'\")(;|`,]", "", $string); $string = mysql_real_escape_string(trim($string), $this->con); return $string; } Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621312 Share on other sites More sharing options...
mmoxley Posted August 20, 2008 Share Posted August 20, 2008 From my limited MySQL work I see a posible problem; you do not have quotations on your usernam and password. $sql = "INSERT INTO i_usr (usrname, usrpass, usremail, usrhandle) VALUES ('$username', '$password', '$email', '$handle')"; Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621314 Share on other sites More sharing options...
mrhobbz Posted August 20, 2008 Author Share Posted August 20, 2008 Good eye, I tried that and i get: Error : MySQL - Database Query [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'usrname usrpass usremail usrhandle VALUES test 098f6bcd4621d373cade4e832627b4f6 ' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621315 Share on other sites More sharing options...
wildteen88 Posted August 20, 2008 Share Posted August 20, 2008 You have no commas separating your fields/values. This is because your clean_sql function removes comma's (and other required characters) from your query. This is the offending line: $string = ereg_replace("[\'\")(;|`,]", "", $string); I'd recommend you remove that line. Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621321 Share on other sites More sharing options...
mrhobbz Posted August 20, 2008 Author Share Posted August 20, 2008 Tried that, just using the mysql_real_escape_string and I get: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'test\', \'098f6bcd4621d373cade4e832627b4f6\', \'[email protected]\', \'test\')' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621323 Share on other sites More sharing options...
wildteen88 Posted August 20, 2008 Share Posted August 20, 2008 You need to rethink how you escape data in your query. Currently you're passing the whole query to your query() method, which in turn passes the query to clean_sql() method which will escape all quotes in your query and not your queries values. You should escape all data before constructing the query itself. Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621327 Share on other sites More sharing options...
mrhobbz Posted August 20, 2008 Author Share Posted August 20, 2008 Its escaped before its stuck into the query string. Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621328 Share on other sites More sharing options...
wildteen88 Posted August 20, 2008 Share Posted August 20, 2008 Then why are you passing the whole query to your clean_sql() method? Change your query() method to public function query($sql) { $this->act = @mysql_query($sql, $this->con); if(!$this->act) { $this->err('MySQL', 'Database Query'); } $this->affected_rows = mysql_affected_rows($this->con); } Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621330 Share on other sites More sharing options...
mmoxley Posted August 20, 2008 Share Posted August 20, 2008 Look's like it is still seeing exta " ' ". try debugging by doing one item at a time to see what value is caussing the burp. $sql = "INSERT INTO i_usr (usrname) VALUES ('$username')"; then $sql = "INSERT INTO i_usr (usrpass) VALUES ('$password)"; if no problems there, you'r varibale as fine. If you do have a problem, you have to look at what your variables are. You might also want to try "$username" double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621331 Share on other sites More sharing options...
mrhobbz Posted August 20, 2008 Author Share Posted August 20, 2008 I got it figured it, removed that pointless expression and the trim and its working fine. Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/120572-solved-mysql-insert-query-error/#findComment-621335 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.