Inserting data to a mysql table problems :(

Reply

Join Date: Mar 2008
Posts: 7
Reputation: BenWill is an unknown quantity at this point 
Solved Threads: 0
BenWill BenWill is offline Offline
Newbie Poster

Inserting data to a mysql table problems :(

 
0
  #1
Mar 18th, 2008
Hey all, first post here and im also very new to PHP, but been having major headaches over one query.

The query simply inserts some data into a table, which i've done in myphpadmin, the myphpadmin looks like this:

Registration_ID int(10) No auto_increment
Registration_Date date No
Academic_Year int(2) No
Year_of_Study int(2) No
Resit_exam int(5) No
Student_ID int(9) No
Module_ID varchar(10) latin1_swedish_ci No

(The no's being: NOT NULL)..

However on inserting the data into a query (Echo'ing the sql query with some valid data.. I get the following):

INSERT INTO Registration (Registration_ID, Registration_Date, Academic_Year, Year_of_Study, Resit_exam, Student_ID, Module_ID) VALUES ('200000', '2007-01-01', '02', '02', '1' '200038989', 'COMP222')Error, insert query failed


-- On the form, I hid the reg id field, and have tried it with a value, and without, and im getting to the same stage, I tried manually inputting the data above via myphpadmin and it worked completely fine.. So im at a loss to whats wrong. The form obviously works fine, as it shows all the correct data thats input, even from the dropdown lists, and the radio button, all the names match up correctly.



Heres some of the code, including the query itself...


  1. $Registration_ID=$_POST['Registration_ID']; // Set all variables as the input by the form
  2. $Registration_Date=$_POST['Registration_Date'];
  3. $Academic_Year=$_POST['Academic_Year'];
  4. $Year_of_Study=$_POST['Year_of_Study'];
  5. $Resit_exam=$_POST['Resit_exam'];
  6. $Student_ID=$_POST['Student_ID'];
  7. $Module_ID=$_POST['Module_ID'];
  8.  
  9.  
  10. //query to insert the relevant data
  11. $query = "INSERT INTO Registration (Registration_ID, Registration_Date, Academic_Year, Year_of_Study, Resit_exam, Student_ID, Module_ID) VALUES ('$Registration_ID', '$Registration_Date', '$Academic_Year', '$Year_of_Study', '$Resit_exam' '$Student_ID', '$Module_ID')";
  12. echo $query;
  13. mysql_query($query) or die('Error, insert query failed'); // run the query


Any advice would be greatly appreciated, I've added things to different tables in same style without problems, just seem to be having big problems when an autonumber is involved, which I would want to self-increment, without the user involvement..

Cheers Ben.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 154
Reputation: Suomedia is an unknown quantity at this point 
Solved Threads: 19
Suomedia Suomedia is offline Offline
Junior Poster

Re: Inserting data to a mysql table problems :(

 
0
  #2
Mar 18th, 2008
You shouldn't be trying to insert a Registration_ID since its an auto_increment column.

Its a good habit to use mysql_error() so you can see the actual error:


  1. mysql_query($query) or die(mysql_error());


Matti Ressler
Suomedia
If you want your dreams to come true, the first thing you must do is to wake up....
Suomedia - Dynamic Content Management
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Inserting data to a mysql table problems :(

 
0
  #3
Mar 18th, 2008
you must not write values in inserting a field if it is auto increment.Just leave it blank and mysql will handle it for you.

  1. $query = "INSERT INTO Registration (Registration_ID, Registration_Date, Academic_Year, Year_of_Study, Resit_exam, Student_ID, Module_ID) VALUES ('', '$Registration_Date', '$Academic_Year', '$Year_of_Study', '$Resit_exam' '$Student_ID', '$Module_ID')";
Last edited by ryan_vietnow; Mar 18th, 2008 at 8:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: BenWill is an unknown quantity at this point 
Solved Threads: 0
BenWill BenWill is offline Offline
Newbie Poster

Re: Inserting data to a mysql table problems :(

 
0
  #4
Mar 18th, 2008
Thanks alot guys After removing the $Registration FROM THE VALUES to be added, and also the Registration_ID from the insert into, it all worked fine

Appreciate it
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC