Grega Posted July 31, 2006 Share Posted July 31, 2006 Hey guys,just a simple probelm. I can't figured out whats wrong with my insert into statement. i tried everything but it doesn't want to work. $query ="INSERT INTO `students` ('ID' , 'Date' , `FirstName` , `Surname` , `Phone` , `Mobile` , `UniversityEmail` , `Email` , `Nationality` , `Nationality2` , `Region` , `University` , `SubjectStudied` , `Specialisation` , `PresentYear` , `TotalYear` , `PlacementNI` , 'StudyLevel') VALUES ('','',`$FirstName`,`$Surname` ,`$Phone`,`$MobilePhone`,`$UniversityEmail` , `$Email' , `$Nationality`,'$Nationality2',`$Region`,`$University`,`$SubjectStudied`,`$Specialisation`,`$PresentYear`,`$TotalYears`,`$PlacementNI`,`$StudyLevel`)"; $result = mysql_query($query) or die(mysql_error());echo ($query);Are fields are complete and also databasase should accept all data.cheers Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/ Share on other sites More sharing options...
pixy Posted July 31, 2006 Share Posted July 31, 2006 Don't you want to echo $result? Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66516 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 use quotes(" or ') in the VALUES clause, not backticks (`).EDIT: also, change the single qoutes around ID and Date to `. And StudyLevel Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66520 Share on other sites More sharing options...
pixy Posted July 31, 2006 Share Posted July 31, 2006 Well backticks are for the actual SQL when you run a query in, say, phpmyadmin, but singlequotes are what you use in PHP for executing a query. Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66522 Share on other sites More sharing options...
Ferenc Posted July 31, 2006 Share Posted July 31, 2006 As far as I can tell Your code never executes the query.[code]$result = mysql_query($query) or die(mysql_error());[/code]change to [code]mysql_query($query) or die(mysql_error());[/code]If you need $result you will have to call the query in a if statement, or some other manner Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66524 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 pixy, backticks are for column and table names, and they're not required, but nice to have around columns if they're keywords or special characters. quotes are for values, especially strings.Ferenc, the query will execute if $result is there.. it's the backticks in the VALUES part that's causing the problem. Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66529 Share on other sites More sharing options...
Ferenc Posted July 31, 2006 Share Posted July 31, 2006 The backticks where addressed in posts previous to mine.Since the query was placed in the variable it cannot execute until called.[code]if (!$result) { die('Invalid query: ' . mysql_error());}[/code]My question to Grega, does the page display an error?If it does, the query is executed, and the backticks cause the problem. Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66537 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 [code]$result = mysql_query($query) or die(mysql_error());[/code]This WILL execute :). when mysql_query returns false, the die part will be called. "or" is evaluated after "=". if mysql_query is true, die won't be processed because it satisfy the "or" condition.however you do it, the problem lies in the query. fix the backticks first, and report any problems. Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66542 Share on other sites More sharing options...
Grega Posted August 1, 2006 Author Share Posted August 1, 2006 Yes the problem was in backticks. but now i'm getting error out of range for ID field which is in database marked as "auto_increment"Probably is another problem with ticks again. are they still used different one for numeric fields? Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-66898 Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 if it's auto increment and you put in an empty string, it will not have syntax error. what is the lenght of ID? do you have an ID in the table that has the max length? Quote Link to comment https://forums.phpfreaks.com/topic/16128-php-mysql-insert/#findComment-67176 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.