perplexia Posted July 31, 2012 Author Share Posted July 31, 2012 I did a var_dump on $insert and I got: string 'INSERT INTO tblPurchasesHeader (Vendor, InvoiceDate,Invoice,TotalAmount,TaxAmount) VALUES (11','11','11','11','11')' (length=115) this is good, right? I'm using WAMP and win7, not windows Server. Could that be an issue? Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365596 Share on other sites More sharing options...
hakimserwa Posted July 31, 2012 Share Posted July 31, 2012 try echoing out the valuables before the insert and see if they hold any value. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365601 Share on other sites More sharing options...
Jessica Posted July 31, 2012 Share Posted July 31, 2012 Is COM a custom class that wraps around ADODB? http://adodb.sourceforge.net/ Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365673 Share on other sites More sharing options...
DavidAM Posted July 31, 2012 Share Posted July 31, 2012 $conn is defined in connect.php as $conn = new COM ("ADODB.Connection") or die("Cannot start ADO"); COM is the "Component Object Module" created by M$ to allow access to Active-X controls in diverse environments. The $conn = new COM (...) statement creates a new connection object. That object is not actually connected to anything. You need to check connect.php for a $con->Connect(...) method call after creating the object. If that is not present, you need to add one. If it is present, check to see if it was successful. If it was, either you closed it somewhere else, or some (significant) error caused the connection to drop. I don't know (remember) the specifics for the connect call, or what the method signature is for checking to see if the connection is open. Hopefully, though, this should point you in the right direction. Since the root of the problem is not the INSERT statement, you are probably looking in the wrong place. In short, verify that you have a connection to the database. Then find out where it is getting closed, and why, and fix that. Then your input statement should work. If you have other database access (i.e. SELECTs) in this same process, and those are succeeding, then you can start there to look for reasons that the connection is closed. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365803 Share on other sites More sharing options...
perplexia Posted August 1, 2012 Author Share Posted August 1, 2012 verify that you have a connection to the database. Then find out where it is getting closed, and why, and fix that. Yes, I think you're exactly right with that assessment. The problem has to do with the connection, not the variables. I have verified that there is a connection, because I have another php file that selects all the data and echos it, and it works fine. I don't understand why the connection is lost whenever I try to insert. I got rid of the variables and put the connection script in the file rather than use an include. So here's what I've got at this point ...and it still gives me a closed connection error. <html> <head> <title>insert</title> </head> <body> <?php //Specify the path to the database //Note: this is an absolute path $databaseLocation = "c:\\wamp\\www\\Jim_Langeveld\\Maintenance1.mdb"; //Dreate an instance of the ADO connection object $conn = new COM ("ADODB.Connection") or die("Cannot start ADO"); //Define connection string, specify database driver $connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=$databaseLocation;"; //get the main menu and display it at the top of the page include('menu.html'); //insert the data $insert = "INSERT INTO tblPurchasesHeader (Vendor, InvoiceDate,Invoice,TotalAmount,TaxAmount) VALUES (".$_POST['vendor']."','".$_POST['date']."','".$_POST['invoice']."','".$_POST['total']."','".$_POST['total']."')"; //Insert the new data into the database $conn->Execute($insert); echo "The information has been submitted."; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365848 Share on other sites More sharing options...
jcbones Posted August 1, 2012 Share Posted August 1, 2012 You need to open the connection with: $conn->Open($connStr); Although, you should be including a username and password in there. COM Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365870 Share on other sites More sharing options...
Skylight_lady Posted August 1, 2012 Share Posted August 1, 2012 You say this is your last code: $insert = "INSERT INTO tblPurchasesHeader (Vendor, InvoiceDate,Invoice,TotalAmount,TaxAmount) VALUES (".$_POST['vendor']."' Why on earth did you not add ' in the very first $_post in the value? You were told to do so and it has to be there. That's exactly what that error means. Like: '".$_POST['vendor']."' If you still get an error or it's not inserting then always echo your query so you know where the problem is. Please close this thread as your going back to your original problem without any changes made. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365872 Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 While you are correct about the missing single quote in the query, Skylight_lady, I don't think you're correct about the meaning of the error message. As jcbones already have stated, perplexia still haven't opened the connection. Just defined the connection string, without using it. perplexia: You really should read up on input validation and output escaping, as both are woefully lacking in your script. Leaving you open to everyone and anyone who wish to attack your site. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365884 Share on other sites More sharing options...
perplexia Posted August 1, 2012 Author Share Posted August 1, 2012 Why on earth did you not add ' in the very first $_post in the value? It was a typo, and not intentional. Sometimes those things happen. Sorry. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365890 Share on other sites More sharing options...
DavidAM Posted August 1, 2012 Share Posted August 1, 2012 I really wish you people would read the error message in the very first post!! ... Operation is not allowed when the object is closed. ... Whether or not the query is valid, is of no consequence at this point! The OP does not have an open database connection. ... That's exactly what that error means. ... Are you kidding?! The error says "object is closed". It does not say "error in query" Please close this thread as your going back to your original problem without any changes made. That's ridiculous. Everyone has been giving advice that is not related to the problem. It does not matter how perfectly he/she builds the query, or how many different ways he/she writes it, if there is no database connection, he/she will continue to get the error because "the object is closed". @perplexia I applaud your stick-to-it-ive-ness (or whatever). Your action by moving the code shows a real desire to fix the problem. You just left out one step, as jcbones pointed out (which he originally did at Reply #18) but was probably missed in all the static about your incorrect query. Here is the code you posted most recently --- notice my comment ### COMMENT ###: $databaseLocation = "c:\\wamp\\www\\Jim_Langeveld\\Maintenance1.mdb"; //Dreate an instance of the ADO connection object $conn = new COM ("ADODB.Connection") or die("Cannot start ADO"); //Define connection string, specify database driver $connStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=$databaseLocation;"; ### COMMENT ### ### YOU NEED TO NOW USE THE connect string IN A CALL TO $conn->Open(); ### COMMENT ### //get the main menu and display it at the top of the page include('menu.html'); //insert the data $insert = "INSERT INTO tblPurchasesHeader (Vendor, InvoiceDate,Invoice,TotalAmount,TaxAmount) VALUES (".$_POST['vendor']."','".$_POST['date']."','".$_POST['invoice']."','".$_POST['total']."','".$_POST['total']."')"; //Insert the new data into the database $conn->Execute($insert); You say you have another script that is accessing the database. Have a look at it, you should be able to find a Connect() or Open() method call. (I actually think it is "Connect" but I haven't used ADODB in code in a while, and don't have any of those projects available to review right now. Note: I am not saying that there are not problems with your code. As ChristianF has posted while I was composing this message, you need, (must) sanitize your inputs before putting them into an SQL string. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365891 Share on other sites More sharing options...
perplexia Posted August 1, 2012 Author Share Posted August 1, 2012 As jcbones already have stated, perplexia still haven't opened the connection. Ah, now I think we're getting somewhere with this. Thanks! perplexia: You really should read up on input validation and output escaping... Thanks for the suggestion. I'll do that, but currently my first priority is to get this insert function to work Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365896 Share on other sites More sharing options...
Christian F. Posted August 1, 2012 Share Posted August 1, 2012 You're welcome, hope you get it sorted out. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365900 Share on other sites More sharing options...
perplexia Posted August 1, 2012 Author Share Posted August 1, 2012 You need to open the connection with: $conn->Open($connStr); It works! jcbones and DavidAM THANK YOU! This was the solution, and DavidAM, I should have seen it in your earlier post. Thanks for seeing through the static and steering the conversation in the right direction. You guys are the best! Thanks to everyone who made an honest effort to help get this issue resolved. Quote Link to comment https://forums.phpfreaks.com/topic/265871-insert-doesnt-work-help-please-and-thank-you/page/2/#findComment-1365903 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.