Click to See Complete Forum and Search --> : Insert using ODBC connection to access
notsobright
10-31-2003, 09:53 AM
I am trying to insert data into a database but keep getting the error must be an updateable query, my database is not write protected and is in my inetpub directory not the root whether I use execute, exec, do or prepare I get the same problem is it my code or am I doing something wrong with my database here is my code:
<?php
// connect to ODBC
if ( !( $database = odbc_connect( "nicdb3", "","")))
die( "Could not connect to database" );
$from = 'steve';
$to = 'bright';
$query = "INSERT INTO mytable (jor_from, jor_to) VALUES($from, $to)";
/* check for errors */
if (!odbc_exec($database, $query))
{
/* error */
echo "Whoops";
}
?>
<?php
odbc_close( $database);
?>
Khalid Ali
10-31-2003, 05:04 PM
What is
nicdb3
is it the name of your database?
If true then on windows,you need to create a DSN(data source name)
using
DSN administrator..and then use odbc to connect to that DSN name.
notsobright
10-31-2003, 08:46 PM
I have set up a User DSN and a System DSN for this database "nicdb3.mdb" still unable to insert data I can extract data using the "select" statement just fine if I use "execute" with a "prepare" statement I get:
Warning: odbc_execute(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Operation must use an updateable query., SQL state S1000 in SQLExecute in e:\inetpub\wwwroot\databaseinsert2.php on line 26
Whoops
If I use "exec" or "do" I get:
Warning: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2., SQL state 07001 in SQLExecDirect in e:\inetpub\wwwroot\databaseinsert1.php on line 21
Whoops
This is the two different code I have tried:
<?php
// connect to ODBC
if ( !( $database = odbc_connect( "nicdb3", "","")))
die( "Could not connect to database" );
$from = 'steve';
$to = 'bright';
$query = "INSERT INTO mytable (jor_from, jor_to) VALUES($from, $to)";
/* check for errors */
if (!odbc_exec($database, $query))
{
/* error */
echo "Whoops";
}
?>
<?php
odbc_close( $database);
?>
<?php
// connect to ODBC
if ( !( $database = odbc_connect( "nicdb3", "","")))
die( "Could not connect to database" );
$from = 'steve';
$to = 'bright';
// $query = "INSERT INTO mytable (jor_from, jor_to) VALUES($from, $to)";
/* run insert */
$stmt = odbc_prepare($database, "INSERT INTO mytable (jor_from, jor_to) VALUES('$from', '$to');" );
/* check for errors */
if (!odbc_execute( $stmt))
{
/* error */
echo "Whoops";
}
?>
<?php
odbc_close( $database);
?>
Thanks for your help so far
Khalid Ali
11-01-2003, 05:46 PM
is there a type mismatch in the following params
$query = "INSERT INTO mytable (jor_from, jor_to) VALUES($from, $to)";\
$from and $to the way you have wrtten seem to be integral values, for strings they should be in qoutes,
second...important thing is that does php support access connections with ODBC
notsobright
11-01-2003, 06:25 PM
Thanks for your help after no sleep last night I finally nutted it out
this works well
<?php
//global variables
$projectID = $_POST["projectID"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$jobdescription = $_POST["jobdescription"];
$qualifications = $_POST["qualifications"];
$loginname = $_POST["loginname"];
$password = $_POST["password"];
$picture = $_POST["picture"];
// connect to ODBC
if ( !( $database = odbc_connect( "nicdb3", "","")))
die( "Could not connect to database" );
/* run insert */
$stmt = odbc_prepare($database, "INSERT INTO people (projectID, firstname, lastname,
jobdescription, qualifications, loginname, password, picture)
VALUES('$projectID','$firstname', '$lastname', '$jobdescription', '$qualifications',
'$loginname', '$password', '$picture');" );
/* check for errors */
if (!odbc_execute( $stmt))
{
/* error */
echo "Whoops";
}
?>
Thanks again
Khalid Ali
11-01-2003, 07:17 PM
Originally posted by Khalid Ali
is there a type mismatch in the following params
$query = "INSERT INTO mytable (jor_from, jor_to) VALUES($from, $to)";\
$from and $to the way you have wrtten seem to be integral values, for strings they should be in qoutes,
:D
Great..I guess I was right on in pointing the error out..