PHP致命错误:未捕获com_异常-插入VFP9数据库
PHP致命错误:未捕获com_异常:源:ADODB.ConnectionPHP致命错误:未捕获com_异常-插入VFP9数据库,php,foxpro,Php,Foxpro,PHP致命错误:未捕获com_异常:源:ADODB.Connection说明:参数类型错误、超出可接受范围或相互冲突。在C:\inetpub\wwwroot\sonreceive\api\create\u test.php:66中 堆栈跟踪: C:\inetpub\wwwroot\sonreceive\api\create\u test.php(66):com->prepare('INSERT-INTO-SON…')) C:\inetpub\wwwroot\sonreceive\api\crea
说明:参数类型错误、超出可接受范围或相互冲突。在C:\inetpub\wwwroot\sonreceive\api\create\u test.php:66中 堆栈跟踪:
错误实际上是由以下原因引起的:
在字符字段的INSERT语句的VALUES子句中,不将php变量括在引号中
数字字段的情况正好相反
请注意,提供给INSERT语句的值来自解码的JSON字符串。请参阅下面的工作代码
?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// get database connection
$conn = new COM("ADODB.Connection");
$conn->Open("Provider=VFPOLEDB.1;Data Source=C:\inetpub\wwwroot\sonservices\RECEIPT.DBC;Password='';Collating Sequence=MACHINE");
class Payment{
// object properties
public $refnum;
public $paydate;
public $custname;
public $custemail;
public $demandno;
public $demanddate;
public $amount;
public $recpdesc;
public $paybank;
public $payref;
// create payment
function create(){
global $conn;
// convert to string values
$this->refnum=strval($this->refnum);
$this->paydate=strval($this->paydate);
$this->custname=strval($this->custname);
$this->custemail=strval($this->custemail);
$this->demandno=strval($this->demandno);
$this->demanddate=strval($this->demanddate);
$this->amount=strval($this->amount);
$this->recpdesc=strval($this->recpdesc);
$this->paybank=strval($this->paybank);
$this->payref=strval($this->payref);
// query to insert record
$query = "INSERT INTO SON2100
VALUES ('', '$this->refnum', '$this->paydate', '', '', '$this->custname', '$this->custemail', '$this->demandno', '$this->demanddate', $this->amount, '$this->recpdesc', '$this->paybank', '$this->payref', '')";
// execute query
if($conn->execute($query)){
return true;
}
return false;
}
}
// prepare payment object
$pymt = New Payment();
// get posted data
$data = json_decode(file_get_contents("php://input"));
// set payment property values and sanitize
$pymt->refnum = htmlspecialchars(strip_tags($data->refnum));
$pymt->paydate = htmlspecialchars(strip_tags($data->paydate));
$pymt->custname = htmlspecialchars(strip_tags($data->custname));
$pymt->custemail = htmlspecialchars(strip_tags($data->custemail));
$pymt->demandno = htmlspecialchars(strip_tags($data->demandno));
$pymt->demanddate = htmlspecialchars(strip_tags($data->demanddate));
$pymt->amount = htmlspecialchars(strip_tags($data->amount));
$pymt->recpdesc = htmlspecialchars(strip_tags($data->recpdesc));
$pymt->paybank = htmlspecialchars(strip_tags($data->paybank));
$pymt->payref = htmlspecialchars(strip_tags($data->payref));
// create the payment
if ($pymt->create()){
echo "Payment was created.";
}
// if unable to create the payment, tell the user
else{
echo "Unable to create payment.";
}
?>
您是否确认所提供的值实际上是其各自字段的正确类型。VFP对此非常严格。您是否尝试过减少语句并只传递部分字段以找出导致错误的原因?谢谢您的建议。是的,值的类型是正确的,但是在减少语句并用空字符串填充值时,我发现INSERT语句无法识别PHP变量($variablename)。因此出现了uncaught com_execption:syntax error。仍然需要找到一种方法让INSERT语句接受VALUES子句中的PHP变量。
?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// get database connection
$conn = new COM("ADODB.Connection");
$conn->Open("Provider=VFPOLEDB.1;Data Source=C:\inetpub\wwwroot\sonservices\RECEIPT.DBC;Password='';Collating Sequence=MACHINE");
class Payment{
// object properties
public $refnum;
public $paydate;
public $custname;
public $custemail;
public $demandno;
public $demanddate;
public $amount;
public $recpdesc;
public $paybank;
public $payref;
// create payment
function create(){
global $conn;
// convert to string values
$this->refnum=strval($this->refnum);
$this->paydate=strval($this->paydate);
$this->custname=strval($this->custname);
$this->custemail=strval($this->custemail);
$this->demandno=strval($this->demandno);
$this->demanddate=strval($this->demanddate);
$this->amount=strval($this->amount);
$this->recpdesc=strval($this->recpdesc);
$this->paybank=strval($this->paybank);
$this->payref=strval($this->payref);
// query to insert record
$query = "INSERT INTO SON2100
VALUES ('', '$this->refnum', '$this->paydate', '', '', '$this->custname', '$this->custemail', '$this->demandno', '$this->demanddate', $this->amount, '$this->recpdesc', '$this->paybank', '$this->payref', '')";
// execute query
if($conn->execute($query)){
return true;
}
return false;
}
}
// prepare payment object
$pymt = New Payment();
// get posted data
$data = json_decode(file_get_contents("php://input"));
// set payment property values and sanitize
$pymt->refnum = htmlspecialchars(strip_tags($data->refnum));
$pymt->paydate = htmlspecialchars(strip_tags($data->paydate));
$pymt->custname = htmlspecialchars(strip_tags($data->custname));
$pymt->custemail = htmlspecialchars(strip_tags($data->custemail));
$pymt->demandno = htmlspecialchars(strip_tags($data->demandno));
$pymt->demanddate = htmlspecialchars(strip_tags($data->demanddate));
$pymt->amount = htmlspecialchars(strip_tags($data->amount));
$pymt->recpdesc = htmlspecialchars(strip_tags($data->recpdesc));
$pymt->paybank = htmlspecialchars(strip_tags($data->paybank));
$pymt->payref = htmlspecialchars(strip_tags($data->payref));
// create the payment
if ($pymt->create()){
echo "Payment was created.";
}
// if unable to create the payment, tell the user
else{
echo "Unable to create payment.";
}
?>