Jump to content


Photo

Php - MySql - iNSERT


  • Please log in to reply
9 replies to this topic

#1 Grega

Grega
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 July 2006 - 06:12 PM

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


#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 31 July 2006 - 06:22 PM

Don't you want to echo $result?

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 July 2006 - 06:24 PM

use quotes(" or ') in the VALUES clause, not backticks (`).

EDIT: also, change the single qoutes around ID and Date to `. And StudyLevel
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 31 July 2006 - 06:26 PM

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.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#5 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 31 July 2006 - 06:28 PM

As far as I can tell Your code never executes the query.

$result = mysql_query($query) or die(mysql_error());

change to
mysql_query($query) or die(mysql_error());

If you need $result you will have to call the query in a if statement, or some other manner
Everything you want to know is here.

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 July 2006 - 06:38 PM

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 31 July 2006 - 06:58 PM

The backticks where addressed in posts previous to mine.

Since the query was placed in the variable it cannot execute until called.
if (!$result) {
   die('Invalid query: ' . mysql_error());
}

My question to Grega, does the page display an error?

If it does, the query is executed, and the backticks cause the problem.
Everything you want to know is here.

#8 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 July 2006 - 07:05 PM

$result = mysql_query($query) or die(mysql_error());

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#9 Grega

Grega
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 August 2006 - 09:30 AM

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?

#10 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 05:20 PM

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?
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users