Jump to content

insert doesn't work - help please! ...and thank you.


perplexia

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>  

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

:D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.