tom.hill Posted January 29, 2008 Share Posted January 29, 2008 Hey guys, I'm having a problem sending data to a table in a MySQL db, i have other tables and php pages set up that are almost identical and work perfectly but for some reason this one refuses to work. I wonder if anyone can spot a school boy error? thank you in advance for taking the time to read this. <?php // Check if session is not registered , redirect back to login page. include('includes/checklogin.php'); // Connect to server and select database include('includes/db_login.php'); // Data sent from form, validated using js on the form itself // mysql_real_escape function used to deny sql injection attacks $softwareid =strip_tags(mysql_real_escape_string($_POST['softwaretitle'])); $licencetypeid =strip_tags(mysql_real_escape_string($_POST['licencetype'])); $key =strip_tags(mysql_real_escape_string($_POST['key'])); $licences =strip_tags(mysql_real_escape_string($_POST['licences'])); // Insert data into mysql $query="INSERT INTO tkeys (softwareid, licencetypeid, key, licences) VALUES('$softwareid', '$licencetypeid', '$key', '$licences')"; $result=mysql_query($query); if (!$result) { die ("Could not query the database: <br />". mysql_error()); } // If successfully insert data into database, displays message "Successful". if($result) { echo "Successfuly added <b> key</b> - add another?"; echo "<br />"; include('addlicence.php'); } else { die ("Could not query the database: <br />". mysql_error()); } ?> the error im getting is: Could not query the database: 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 'key, licences) VALUES('92', '1', '9342j-nh923f-2n9f23-24g35', '3')' at line 1 but i cant find anything wrong with the syntax!! argh! and also if this helps: -- -------------------------------------------------------- -- -- Table structure for table `tkeys` -- CREATE TABLE IF NOT EXISTS `tkeys` ( `keyid` int(11) NOT NULL auto_increment, `softwareid` int(11) NOT NULL, `licencetypeid` int(11) NOT NULL, `key` varchar(100) default NULL, `licences` int(11) NOT NULL default '1', `isdeleted` tinyint(1) NOT NULL default '0', PRIMARY KEY (`keyid`), KEY `softwareid` (`softwareid`), KEY `licencetypeid` (`licencetypeid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -- Constraints for dumped tables -- -- -- Constraints for table `tkeys` -- ALTER TABLE `tkeys` ADD CONSTRAINT `tkeys_ibfk_1` FOREIGN KEY (`softwareid`) REFERENCES `tsoftware` (`softwareid`), ADD CONSTRAINT `tkeys_ibfk_2` FOREIGN KEY (`licencetypeid`) REFERENCES `tlicencetype` (`licencetypeid`); Quote Link to comment https://forums.phpfreaks.com/topic/88456-solved-php-to-mysql-problem/ Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 KEY is a mysql reserved word. Change it and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/88456-solved-php-to-mysql-problem/#findComment-452737 Share on other sites More sharing options...
tom.hill Posted January 29, 2008 Author Share Posted January 29, 2008 well i feel foolish!! thanks so much, quick fix!! apreciated Quote Link to comment https://forums.phpfreaks.com/topic/88456-solved-php-to-mysql-problem/#findComment-452878 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.