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...

$Registration_ID=$_POST['Registration_ID'];  // Set all variables as the input by the form
$Registration_Date=$_POST['Registration_Date']; 
$Academic_Year=$_POST['Academic_Year']; 
$Year_of_Study=$_POST['Year_of_Study']; 
$Resit_exam=$_POST['Resit_exam']; 
$Student_ID=$_POST['Student_ID']; 
$Module_ID=$_POST['Module_ID']; 


//query to insert the relevant data
$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')";
echo $query;
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.

Recommended Answers

All 3 Replies

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:

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

Matti Ressler
Suomedia

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.

$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')";

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.