简体   繁体   中英

Cannot write to MS SQL database using PHP ODBC connection

Problem: I can successfully use a select query in PHP using odbc_exec, however, I can not execute an INSERT INTO or UPDATE query.

Resources: Windows Server 2003: IIS 6, SQL Server 2005.

Here is my code:

<?php
require('../_DSN/DSN_M.php');

$cnnOra = odbc_connect($strarrDSN['dsn'], $strarrDSN['username'], $strarrDSN['pswd']);
$res_slct = odbc_exec($cnnOra, " select * FROM person;") or die (odbc_errormsg());

odbc_result_all($res_sldr);

$sqlupd = "UPDATE person SET person_ame='Steve Woz' WHERE pk_person_ID = '32';"

$res_upd = odbc_exec($cnnora, $sqlupd) or die(odbc_errormsg());      
$res_cmt = odbc_commit($cnnora);

var_dump($res_upd);
var_dump($res_slct);
var_dump($res_cmt);

// close the connection 
odbc_close($cnnOra); 
?>

The output of the SELECT query displays and there is no errormsg or var_dump results on the page.

Here is the output from the SELECT query:

pk_person_ID fk_SSN    fk_qual1_ID fk_qual2_ID person_Name 
31           999999999 1           1           bobby buchier 
32           999999999 2           2           Frank Gifford 

Am I missing something here?

I found the solution for me. I am not sure why this works when my original code didn't.

I needed to use an odbc_prepare and then an odbc_exec. Here is the code that worked for me:

$q = "UPDATE person SET person_name = 'Steve Woz' WHERE pk_person_ID = '32'"; 
$res = odbc_prepare ($cnnOra, $q) or die (odbc_errormsg()); 
$exc = odbc_execute($res) or die (odbc_errormsg());

This also works on a INSERT INTO query. Much thanks to @andrewsi!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM