ludjer Posted October 7, 2007 Share Posted October 7, 2007 when i try run one sql query i get a error here is the sql query, it works in phpmyadmin when i run it INSERT INTO `mainp_com` ( `ID` , `newsid` , `userid` , `comment` , `time` , `date1` ) VALUES ( NULL , '9', '2', 'woot this is a 1337 test', '03:16:06 AM', 'October 7 2007' ); this is the out put i get, i have my own custom error printing A fatal MySQL error occured. Query: INSERT INTO `mainp_com` (`ID` , `newsid` , `userid` , `comment` , `time` , `date1`) VALUES (NULL, `9`, `2`, `i am the most l337 of them all`, `03:36:40 AM`, `October 7 2007`); Error: (1054) Unknown column '9' in 'field list' if this error happens again and you dont know what is wrong please email now for the php code <?php include "functions.php"; if (isset($_GET["nid"])) $nid = $_GET["nid"]; else die("error"); if (isset($_POST['submit'])){ $comment = $_POST["comment"]; if($comment == null) { die("error please input somthing into the comment box"); } $userid = $user->data["user_id"]; $time = date("h:i:s A"); $date = date("F j Y"); $comment = htmlspecialchars($comment); $sql = 'INSERT INTO `mainp_com` (`ID` , `newsid` , `userid` , `comment` , `time` , `date1`) VALUES (NULL, `'.$nid.'`, `'.$userid.'`, `'.$comment.'`, `'.$time.'`, `'.$date.'`);'; $qur = sql_safe($sql);//custome function mysql_query($qur)or die(sqlerr($qur,mysql_error(),mysql_errno())); }else{ die("error"); } ?> thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/ Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Share Posted October 7, 2007 Error: (1054) Unknown column '9' in 'field list' This kind of error normally suggests a typo, check your PHP to your database and make sure everything is spelt correctly. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363645 Share on other sites More sharing options...
ludjer Posted October 7, 2007 Author Share Posted October 7, 2007 there cant be a typo cause this sql query works when i run it using phpmyadmin INSERT INTO `mainp_com` ( `ID` , `newsid` , `userid` , `comment` , `time` , `date1` ) VALUES ( NULL , '9', '54', 'this works when i use phpmyadmin', '2:35 am', ' October 7 2007' ); and i copy it directly into my php code and edit the values Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363646 Share on other sites More sharing options...
KrisNz Posted October 7, 2007 Share Posted October 7, 2007 try making NULL lowercase, or removing it and the ID field from the query altogether. Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363647 Share on other sites More sharing options...
ludjer Posted October 7, 2007 Author Share Posted October 7, 2007 if you have a look it says Unknown column '9' in 'field list', this means that the error comes after the null it comes in with newsid here is the structure of my sql database CREATE TABLE `mainp_com` ( `ID` int(11) NOT NULL auto_increment, `newsid` int(11) NOT NULL, `userid` int(11) NOT NULL, `comment` mediumblob NOT NULL, `time` varchar(10) collate latin1_general_ci NOT NULL, `date1` varchar(20) collate latin1_general_ci NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=9 ; Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363656 Share on other sites More sharing options...
KrisNz Posted October 7, 2007 Share Posted October 7, 2007 what does the query look like after you run your custom sql_safe function? Is the one you run in myadmin copied from before you run that or after? And why would your set your ID column to null?? Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363664 Share on other sites More sharing options...
ludjer Posted October 7, 2007 Author Share Posted October 7, 2007 Before sqlsafe: INSERT INTO `mainp_com` (`newsid` , `userid` , `comment` , `time` , `date1`) VALUES (`9`, `2`, `i am the most l337 of them all`, `04:05:41 AM`, `October 7 2007`); After sqlsafe: INSERT INTO `mainp_com` (`newsid` , `userid` , `comment` , `time` , `date1`) VALUES (`9`, `2`, `i am the most l337 of them all`, `04:05:41 AM`, `October 7 2007`); A fatal MySQL error occured. Query: INSERT INTO `mainp_com` (`newsid` , `userid` , `comment` , `time` , `date1`) VALUES (`9`, `2`, `i am the most l337 of them all`, `04:05:41 AM`, `October 7 2007`); Error: (1054) Unknown column '9' in 'field list' if this error happens again and you dont know what is wrong please email And why would your set your ID column to null?? its a waste of space but you can do it if you want, also that is how phpmyadmin gives it to you. Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363669 Share on other sites More sharing options...
KrisNz Posted October 7, 2007 Share Posted October 7, 2007 Try it with no quotes around the field names and ' rather than ` around each value. INSERT INTO mainp_com (newsid , userid , comment , time , date1) VALUES ('9', '2', 'i am the most l337 of them all', '04:05:41 AM', 'October 7 2007') Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363676 Share on other sites More sharing options...
ludjer Posted October 7, 2007 Author Share Posted October 7, 2007 i found my problem if i run the $comments through the sqlsafe before putting it into the sql query anbd not run the query through the sqlsafe it works, sql safe gave this INSERT INTO mainp_com (newsid , userid , comment , time , date1) VALUES (\'9\', \'2\', \'i am the most l337 of them all\', \'04:05:41 AM\', \'October 7 2007\') and i needed this INSERT INTO mainp_com (newsid , userid , comment , time , date1) VALUES ('9', '2', 'i am the most l337 of them all', '04:05:41 AM', 'October 7 2007') thanks all Quote Link to comment https://forums.phpfreaks.com/topic/72138-solved-sql-error-when-i-use-php-to-run-it/#findComment-363677 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.