Jump to content

Php - MySql - iNSERT


Grega

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.