dh526 Posted May 7, 2009 Share Posted May 7, 2009 Hi, First of all, I'd like to say I'm new to PHP but I'm enjoying it a lot I've been doing it at uni so far but tried to set it up on my web server at home. I have a working install of PHP and I can connect to my database using a DSN (called db09) which allows me to read data from a very simple table in the database. However, the problems arise when I try to use INSERT INTO to insert data into the database from a form. Here is my code : --- THIS WORKS --- <html> <head> <title>PHP Database 1</title> </head> <body> <H1>PHP Example: Select all data from a database</H1> <?php $DSN="db09"; $conn= odbc_connect('db09','',''); $sql="Select * from tblCustomer"; $rs=odbc_exec($conn,$sql); echo ("<p>Customers details:"); echo ("<table border='1'>"); While (odbc_fetch_row($rs)) { $Surname=odbc_result($rs,"Surname"); echo ("<tr><td>Name:</td><td>$Surname</td>"); } odbc_close($conn); ?> </body> </html> ----------------------- THIS DOESN'T WORK - FORM. ------------------------- <html> <head> </head> <body> <p>Input a new customer record</p> <form method="post" action="register.php"> <p>Surname: <input type="text" name="surname" /> </p> <p>Forename: <input type="text" name="forename" /></p> <p>DOB: <input type="text" name="dob" /> </p> <p>Email: <input type="text" name="email" /> </p> <input type="submit"> </form> </body> </html> --------------------- AND HERE IS THE PHP ------------------- <html> <head> </head> <body> <?php $surname = $_POST ['surname']; $forename = $_POST ['forename']; $dob = $_POST ['dob']; $email = $_POST ['email']; $conn=odbc_connect('db09','',''); $sql="INSERT INTO "tblCustomer" ("Surname", "Forename", "DOB", "Email Address") VALUES ($surname','$forename', '$dob','$email')"; if (odbc_exec($conn,$sql)) { echo "Record added"; } odbc_close($conn); ?> </body> ------------------- And the table name and the field names are right ... I checked them lots any help would be much appriciated Thank you David Harrison Quote Link to comment https://forums.phpfreaks.com/topic/157254-solved-insert-into-not-working-with-ms-access/ Share on other sites More sharing options...
revraz Posted May 7, 2009 Share Posted May 7, 2009 I don't work much with ACCESS, but you can try to remove the double quotes from the table and field names, and you are also missing a single quote in front of $surname. Quote Link to comment https://forums.phpfreaks.com/topic/157254-solved-insert-into-not-working-with-ms-access/#findComment-828670 Share on other sites More sharing options...
dh526 Posted May 7, 2009 Author Share Posted May 7, 2009 Thank you for the response, but it still doesn't work I have tried quotes and no quotes every where... Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/157254-solved-insert-into-not-working-with-ms-access/#findComment-828674 Share on other sites More sharing options...
Mchl Posted May 7, 2009 Share Posted May 7, 2009 In general you have mixed up the quotes. See it colored $sql="INSERT INTO "tblCustomer" ("Surname", "Forename", "DOB", "Email Address") VALUES ($surname','$forename', '$dob','$email')"; (use or tags when posting your code in the forum. Apart from that Access needs [] around column names with spaces. Quote Link to comment https://forums.phpfreaks.com/topic/157254-solved-insert-into-not-working-with-ms-access/#findComment-828677 Share on other sites More sharing options...
dh526 Posted May 7, 2009 Author Share Posted May 7, 2009 Thank you!! It works !! And I'll use and in future thanks again Quote Link to comment https://forums.phpfreaks.com/topic/157254-solved-insert-into-not-working-with-ms-access/#findComment-828688 Share on other sites More sharing options...
mdm Posted May 21, 2009 Share Posted May 21, 2009 Can you please show the complete working code with the suggested corrections? TY Quote Link to comment https://forums.phpfreaks.com/topic/157254-solved-insert-into-not-working-with-ms-access/#findComment-839054 Share on other sites More sharing options...
mdm Posted May 21, 2009 Share Posted May 21, 2009 Nevermind, I've got it working now too; <?php $name = $_POST ['name']; $email = $_POST ['email']; $conn=odbc_connect('calrim','',''); $sql="INSERT INTO CONTACTS (name,email) VALUES ('$name', '$email')"; if (odbc_exec($conn,$sql)) { echo "Record added"; } odbc_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/157254-solved-insert-into-not-working-with-ms-access/#findComment-839088 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.