Forum Moderators: coopster
I have created a registration php and it seems to have a problem. It connects to the MySQL server fine but doesnt inserts the information i typed in the form. Im planning on placing different information in the same form to different tables.
Here is my code:
<?PHP
$hostname="localhost";
$database="My Database";
$username="My User Name";
$dbpassword="My Password";
$title=$_POST['title'];
$firstname=$_POST['firstname'];
$lastname= $_POST['lastname'];
$dob=$_POST['dob'];
$nationality = $_POST['nationality'];
$gender=$_POST['gender'];
$addressline1=$_POST['addressline1'];
$addressline2=$_POST['addressline2'];
$city=$_POST['city'];
$state=$_POST['state'];
$postalcode=$_POST['postalcode'];
$country=$_POST['country'];
$mobile =$_POST['mobile'];
$email=$_POST['email'];
$presentuni=$_POST['presentuni'];
$levelcourse1=$_POST['levelcourse1'];
$titlecourse1=$_POST['titlecourse1'];
$previousuni=$_POST['previousuni'];
$levelcourse2=$_POST['levelcourse2'];
$titlecourse2=$_POST['titlecourse2'];
$experience=$_POST['experience'];
mysql_connect($hostname,$username,$dbpassword);
@mysql_select_db($database)or die("Unable to select database");
$query= "INSERT INTO Personal Information VALUES('','$title', '$firstname', '$lastname', '$dob', '$nationality', '$gender')";
mysql_query($query) or die( "Error: " . mysql_error() );
$query2= "INSERT INTO Contacts VALUES('LAST_INSERT_ID()', '$addressline1', '$addressline2', '$city', '$state', '$postalcode', '$country', '$mobile', '$email')";
mysql_query($query2) or die( "Error: " . mysql_error() );
$query3= "INSERT INTO Academic Records VALUES('LAST_INSERT_ID()', '$presentuni', '$levelcourse1', '$titlecourse1', '$previousuni', '$levelcourse2', '$titlecourse2')";
mysql_query($query3) or die( "Error: " . mysql_error() );
$query4 = "INSERT INTO Work Experience VALUES('LAST_INSERT_ID()', '$experience')";
mysql_query($query4) or die( "Error: " . mysql_error() );
mysql_close();
?>
This is the error i get:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Information VALUES('','', '', '', '', '', '')' at line 1
Can you find what i'm doing wrong? PLEASE HELP... I HAVE TRIED EVERYTHING... being working on it for days now.
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Information VALUES('','Mr', 'mark', 'moses', '04/11/1989', 'British', 'Male')' at line 1
In the database, All the values were set to VARCHAR.
$query= "INSERT INTO Personal Information ('title', 'firstname', 'lastname', 'dob', 'nationality', 'gender')
VALUES('$title', '$firstname', '$lastname', '$dob', '$nationality', '$gender')";
mysql_query($query) or die( "Error: " . mysql_error() );
$query2= "INSERT INTO Contacts ('addressline1', 'addressline2', 'city', 'state', 'postalcode', 'country', 'mobile', 'email')
VALUES('LAST_INSERT_ID()', '$addressline1', '$addressline2', '$city', '$state', '$postalcode', '$country', '$mobile', '$email')";
mysql_query($query2) or die( "Error: " . mysql_error() );
$query3= "INSERT INTO Academic Records ('presentuni', 'levelcourse1', 'titlecourse1', 'previousuni', 'levelcourse2', 'titlecourse2' ) VALUES('LAST_INSERT_ID()', '$presentuni', '$levelcourse1', '$titlecourse1', '$previousuni', '$levelcourse2', '$titlecourse2')";
mysql_query($query3) or die( "Error: " . mysql_error() );
$query4 = "INSERT INTO Work Experience ('experience') VALUES('LAST_INSERT_ID()', '$experience')";
mysql_query($query4) or die( "Error: " . mysql_error() );
The better solution would be to change the table name to Personal_Information or PersonalInformation.
INSERT INTO Personal_Information ('title', 'firstname', 'lastname', 'dob', 'nationality', 'gender') VALUES('Mr', 'Buki', 'moses', '04/11/1989', 'British', 'Male')
Here is my code:
$query= "INSERT INTO Personal_Information ('title', 'firstname', 'lastname', 'dob', 'nationality', 'gender')
VALUES('$title', '$firstname', '$lastname', '$dob', '$nationality', '$gender')";
echo '<p>',$query;
die;
$query2= "INSERT INTO Contacts ('addressline1', 'addressline2', 'city', 'state', 'postalcode', 'country', 'mobile', 'email')
VALUES('LAST_INSERT_ID()', '$addressline1', '$addressline2', '$city', '$state', '$postalcode', '$country', '$mobile', '$email')";
mysql_query($query2) or die( "Error: " . mysql_error() );
$query3= "INSERT INTO 'Academic_Records' ('presentuni', 'levelcourse1', 'titlecourse1', 'previousuni', 'levelcourse2', 'titlecourse2' ) VALUES('LAST_INSERT_ID()', '$presentuni', '$levelcourse1', '$titlecourse1', '$previousuni', '$levelcourse2', '$titlecourse2')";
mysql_query($query3) or die( "Error: " . mysql_error() );
$query4 = "INSERT INTO 'Work_Experience' ('experience') VALUES('LAST_INSERT_ID()', '$experience')";
mysql_query($query4) or die( "Error: " . mysql_error() );
The information i typed into the form isnt going to the MySQL server.
Its now giving another error message:
Error: Column count doesn't match value count at row 1
Here is the new code:
$query= "INSERT INTO Personal_Information (user_id, title, firstname, lastname, dob, nationality, gender)
VALUES('', '$title', '$firstname', '$lastname', '$dob', '$nationality', '$gender')";
mysql_query($query) or die( "Error: " . mysql_error() );
$query2= "INSERT INTO Contacts (addressline1, addressline2, city, state, postalcode, country, mobile, email)
VALUES('LAST_INSERT_ID()', '$addressline1', '$addressline2', '$city', '$state', '$postalcode', '$country', '$mobile', '$email')";
mysql_query($query2) or die( "Error: " . mysql_error() );
$query3= "INSERT INTO Academic_Records (presentuni, levelcourse1, titlecourse1, previousuni, levelcourse2, titlecourse2 ) VALUES('LAST_INSERT_ID()', '$presentuni', '$levelcourse1', '$titlecourse1', '$previousuni', '$levelcourse2', '$titlecourse2')";
mysql_query($query3) or die( "Error: " . mysql_error() );
$query4 = "INSERT INTO Work_Experience (experience) VALUES('LAST_INSERT_ID()', '$experience')";
mysql_query($query4) or die( "Error: " . mysql_error() );
mysql_close();
i have tried it with and without the "user_id" which is a primary key and set to auto-increment. It still gives the same error stated above....
Please hellppp... Sorry for being a pain guys
expand you or die statements to give you something you can use. If you have many or die statements on the same page and they all spit out i the same format you will waste a lot of time
mysql_query($query) or die ("<p>died: $q<br>" . mysql_error());
if you include the query in each or die you will get the actual query that was sent and the error it caused, very useful
you are using LAST_INSERT_ID() all over, do any of those other tables have ids? if so you would be using that id and if you meant to carry the user_id through out you could be causing a bug.
directly after your first insert, why not use something like
$myawesomeid = mysql_insert_id();
then use that var though out
do you have any idea which query is dying?
Are you now saying i should:
*change the name of the user_id in other tables?
*use $id = mysql_insert_id(); after the INSERT INTO personal_information?
I have no idea which query is dying... it looks fine with me... i have even Googled it and I have done everything in the tutorials.
which is why I mentioned you should expand your 'or die's so you can figure it out
>> change the name of the user_id in other tables?
no
>> use $id = mysql_insert_id(); after the INSERT INTO personal_information?
yes and then use that variable in all your subsequent queries
>> Is it possible to link the user to a new page after they filled out the form?
yes, you could forward them using the header function as long as there is no output from your script
YOU KNOW THAT FEELING WHEN YOU GOT SOMETHING AFTER SOO LONG YOU HAVE WORKED HARD FOR IT?
yep i'm having that feeling now... Probably go for a night out now loool ... Thanks to you wonderfull people...
llool i actually feel proud of myself now... after 2 days of feeling like an idoit lool...
Thank you!