500 PHP内部服务器错误-将数据插入VFP9数据库

时间:2018-07-04 22:56:43

标签: javascript php

我试图用PHP编写API脚本以将记录插入Foxpro 9数据库,但是调用API时却收到“ 500 Internal Server Error”消息。我是Foxpro开发人员,但对PHP还是陌生的。

在此站点和其他站点上,我已经对该主题进行了多次提问和评论,并且几乎没有建议的情况下实施了所有建议的解决方案。以下是我到目前为止采取的步骤:

  1. IIS和PHP已安装并配置。 (phpinfo()正确显示)
  2. VFP 9已完全安装。 (使用VFPOLEDB驱动程序)
  3. 我已经完全清除了浏览数据。

我不确定问题出在哪里(因为“ 500内部服务器错误”消息可能是PHP脚本或PHP配置出现问题。请有人看看下面的PHP脚本以帮助找出问题所在)。有问题吗?

TIA。

<?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");

// database connection
$conn = new COM("ADODB.Connection");
$conn->Open("Provider=VFPOLEDB.1;Data Source=C:\inetpub\wwwroot\sonreceipt\RECEIPT.DBC;Collating Sequence=Machine");

// get posted data
$data = json_decode(file_get_contents("php://input"));

// set payment values received
$jrefnum = $data->refnum;
$jpaydate = $data->paydate;
$jcustname = $data->custname;
$jcustemail = $data->custemail;
$jdemandno = $data->demandno;
$jdemanddate = $data->demanddate;
$jamount = $data->amount;
$jrecpdesc = $data->recpdesc;
$jpaybank = $data->paybank;
$jpayref = $data->payref;

// create the payment
if(create()){
    echo "Payment was created.";
} 

// if unable to create the payment, tell the user
else {
    echo "Unable to create payment.";
}

// create payment
function create(){

   // query to insert record
    $query = "INSERT INTO SON2100 (refnum, paydate, custname, custemail, demandno, demanddate, amount, recpdesc, paybank, payref)  
                VALUES ($srefnum, $spaydate, $scustname, $scustemail, $sdemandno, $sdemanddate, $smount, $srecpdesc, $spaybank, $spayref)";

    // prepare query
    global $conn
    $stmt = $conn->prepare($query);

    // sanitize
    global $jrefnum, $jpaydate, $jcustname, $jcustemail, $jdemandno, $jdemanddate, $jamount, $jrecpdesc, $jpaybank, $jpayref;
    $srefnum=htmlspecialchars(strip_tags($jrefnum));
    $spaydate=htmlspecialchars(strip_tags($jpaydate));
    $scustname=htmlspecialchars(strip_tags($jcustname));
    $scustemail=htmlspecialchars(strip_tags($jcustemail));
    $sdemandno=htmlspecialchars(strip_tags($jdemandno));
    $sdemanddate=htmlspecialchars(strip_tags($jdemanddate));
    $samount=htmlspecialchars(strip_tags($jamount));
    $srecpdesc=htmlspecialchars(strip_tags($jrecpdesc));
    $spaybank=htmlspecialchars(strip_tags($jpaybank));
    $spayref=htmlspecialchars(strip_tags($jpayref));

    // execute query
    if($stmt->execute()){
        return true;
    } 

    return false;
}
?>

下面是调用API的JavaScript。

<script>
function sendData(data) {
    var XHR = new XMLHttpRequest();
    var jsonData = {"refnum":"1111-2222-3333", "paydate":"01-06-2018", "custname":"O. A. BECKLEY VENTURES", "custemail":"beckleyventures@gmail.com", "demandno":"DEMAND NOTE 001", "demanddate":"01-06-2018", "amount":"15550.00", "recpdesc":"SONCAP", "paybank":"ZENITH BANK PLC", "payref":"0123456789"};

    // Define what happens on successful data submission
    XHR.addEventListener('load', function(event) {
        window.alert('Yeah! Data sent and response loaded.');
    });

    // Define what happens in case of error
    XHR.addEventListener('error', function(event) {
        window.alert('Oops! Something goes wrong.');
    });

    // Set up our request
    XHR.open('POST', 'http://localhost/sonreceipt/api/create_payment.php', true);

    // Add the required HTTP header for form data POST requests
    XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    // Finally, send our data.
    XHR.send(jsonData);
}
</script>

这是已编辑的脚本,但仍然无法正常工作。如前所述,我还是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");

// database connection
$conn = new COM("ADODB.Connection");

try {
    $conn->Open('Provider=VFPOLEDB.1;DSN=RECEIPT;Mode=ReadWrite;Password="";Collating Sequence=MACHINE;');
    if (! $conn) {
       throw new Exception("Could not connect!");
    }
}
catch (Exception $e) {
    echo "Error (File:): ".$e->getMessage()."<br>";
}

if (!$conn)
    {exit("Connection Failed: " . $conn);}
echo "Connection Sucessfull";

// get posted data
$data = json_decode(file_get_contents("php://input"));

// set payment values received
$jrefnum = $data->refnum;
$jpaydate = $data->paydate;
$jcustname = $data->custname;
$jcustemail = $data->custemail;
$jdemandno = $data->demandno;
$jdemanddate = $data->demanddate;
$jamount = $data->amount;
$jrecpdesc = $data->recpdesc;
$jpaybank = $data->paybank;
$jpayref = $data->payref;

// create the payment
if(create()){
    echo "Payment was created.";
} 

// if unable to create the payment, tell the user
else {
    echo "Unable to create payment.";
}

// create payment
function create(){

    global $conn;
    global $jrefnum, $jpaydate, $jcustname, $jcustemail, $jdemandno, $jdemanddate, $jamount, $jrecpdesc, $jpaybank, $jpayref;

    // sanitize
    $srefnum=htmlspecialchars(strip_tags($jrefnum));
    $spaydate=htmlspecialchars(strip_tags($jpaydate));
    $scustname=htmlspecialchars(strip_tags($jcustname));
    $scustemail=htmlspecialchars(strip_tags($jcustemail));
    $sdemandno=htmlspecialchars(strip_tags($jdemandno));
    $sdemanddate=htmlspecialchars(strip_tags($jdemanddate));
    $samount=htmlspecialchars(strip_tags($jamount));
    $srecpdesc=htmlspecialchars(strip_tags($jrecpdesc));
    $spaybank=htmlspecialchars(strip_tags($jpaybank));
    $spayref=htmlspecialchars(strip_tags($jpayref));

   // query to insert record
    $query = "INSERT INTO SON2100 (refnum, paydate, custname, custemail, demandno, demanddate, amount, recpdesc, paybank, payref) 
      VALUES ($srefnum, $spaydate, $scustname, $scustemail, $sdemandno, $sdemanddate, $smount, $srecpdesc, $spaybank, $spayref)";

   // prepare query
    $stmt = $conn->prepare($query);

    // execute query
    if($stmt->execute()){
        return true;
    } 

    return false;
}
?>

2 个答案:

答案 0 :(得分:0)

您尚未在value函数中声明SQL的create()部分中使用的变量,并且缺少分号

//  you have
global $conn

// should be
global $conn ;

答案 1 :(得分:0)

使用

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

找到您的错误。