943,824 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1339
  • PHP RSS
Mar 18th, 2008
0

Inserting data to a mysql table problems :(

Expand Post »
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...


PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BenWill is offline Offline
7 posts
since Mar 2008
Mar 18th, 2008
0

Re: Inserting data to a mysql table problems :(

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:


PHP Syntax (Toggle Plain Text)
  1. mysql_query($query) or die(mysql_error());


Matti Ressler
Suomedia
Reputation Points: 15
Solved Threads: 19
Junior Poster
Suomedia is offline Offline
154 posts
since Mar 2008
Mar 18th, 2008
0

Re: Inserting data to a mysql table problems :(

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.

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Mar 18th, 2008
0

Re: Inserting data to a mysql table problems :(

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BenWill is offline Offline
7 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Open source soul
Next Thread in PHP Forum Timeline: pass the javascript variable to php page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC