Jump to content

Archived

This topic is now archived and is closed to further replies.

Grega

Php - MySql - iNSERT

Recommended Posts

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

Share this post


Link to post
Share on other sites
use quotes(" or ') in the VALUES clause, not backticks (`).

EDIT: also, change the single qoutes around ID and Date to `. And StudyLevel

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.