【问题标题】:Mysql query completing but returning as an error due to null that appears at the end of the queryMysql 查询完成但由于查询末尾出现 null 而返回错误
【发布时间】:2017-10-25 22:02:28
【问题描述】:

好的,所以我们在用于组织的 api 中使用的精简 php 应用程序存在问题。当我们向 slim 应用程序发布更新查询时,数据会通过。

问题是即使数据传递到api并且对所有字段执行操作。它作为错误返回。这是因为当我们运行该函数时,它在 SQL 查询的末尾对应的 sql 查询会在 where 语句中以这种格式从函数调用中的变量旁边轻敲一下,即 WHERE id=$idnull。 $id 是实际值。该函数的结构与我们成功运行的场地方法相同。有没有人有任何想法?

我想尽办法解决这个问题,以便为组织写入我们的数据库的函数正常运行。

function updateOrg($id) {
error_log('addOrg\n', 3, 'php.log');
$request = Slim::getInstance()->request();
$body = $request->getBody();
//var_dump($body);
$org = json_decode($body);

// Handle Checkboxes
if(isset($_REQUEST['recAddEnable'])){ $rAdde = 1; } else $rAdde = 0;
if(isset($_REQUEST['recTelEnable'])){ $rTele = 1; } else $rTele = 0;
if(isset($_REQUEST['recUrlEnable'])){ $rUrle = 1; } else $rUrle = 0;

$sql = "UPDATE usg_usg_orgs 
        SET 
            oname='$_REQUEST[oname]', 
            oemail='$_REQUEST[oemail]', 
            odesc='$_REQUEST[odesc]', 
            ourl='$_REQUEST[ourl]', 
            oadd1='$_REQUEST[oadd1]', 
            oadd2='$_REQUEST[oadd2]', 
            ozip='$_REQUEST[ozip]', 
            ocity ='$_REQUEST[ocity]',
            ostate ='$_REQUEST[ostate]', 
            otel ='$_REQUEST[otel]',
            ofax ='$_REQUEST[ofax]', 
            recTel ='$_REQUEST[recTel]', 
            recTelEnable= $rTele, 
            recAdd1='$_REQUEST[recAdd1]',
            recAdd2='$_REQUEST[recAdd2]',
            recAddEnable= $rAdde,
            recCity='$_REQUEST[recCity]', 
            recCounty='$_REQUEST[recCounty]', 
            recUrl='$_REQUEST[recUrl]', 
            recUrlEnable=$rUrle, 
            recState='$_REQUEST[recState]',
            recZip='$_REQUEST[recZip]',
            recPubNote='$_REQUEST[recPubNote]',
            recPrivNote='$_REQUEST[recPrivNote]', 
            priContact='$_REQUEST[priContact]', 
            priTitle='$_REQUEST[priTitle]', 
            priPhone='$_REQUEST[priPhone]',
            priCell='$_REQUEST[priCell]',
            priEmail='$_REQUEST[priEmail]',
            secContact='$_REQUEST[secContact]',
            secTitle='$_REQUEST[secTitle]', 
            secPhone='$_REQUEST[secPhone]',
            secCell='$_REQUEST[secCell]',
            secEmail='$_REQUEST[secEmail]',
            addContact='$_REQUEST[addContact]', 
            addTitle='$_REQUEST[addTitle]',
            addPhone='$_REQUEST[addPhone]',
            addCell='$_REQUEST[addCell]',
            addEmail='$_REQUEST[addEmail]',
            invContact='$_REQUEST[invContact]',
            invTitle='$_REQUEST[invTitle]', 
            invPhone='$_REQUEST[invPhone]',
            invCell='$_REQUEST[invCell]',
            invEmail='$_REQUEST[invEmail]', 
            recModified = UNIX_TIMESTAMP() 
        WHERE oid= $id";
        echo $sql;
try {
    $db = getConnection();
    $stmt = $db->prepare($sql);  
    $stmt->execute();
    $db = null;
    echo json_encode($org); 
} catch(PDOException $e) {

    echo '{"error":{"text":'. $e->getMessage() .'}}';

}

}

这是我们用来写入 orgs 的函数。

下面是我们用来写到api来写orgs的函数。

jQuery.ajax({
        type: "post",
        url: "//usasportgroup.com/api3/orgs/update/"+oid,
        data: {     
        oname:jQuery('#onameMF').val(),
        oadd1:jQuery('#oadd1MF').val(),
        oadd2:jQuery('#oadd2MF').val(),
        otel:jQuery('#otelMF').val(),
        ocity:jQuery('#ocityMF').val(),
        ostate:jQuery('#ostateMF').val(),
        ozip:jQuery('#ozipMF').val(),
        ourl:jQuery('#ourlMF').val(),
        oemail:jQuery('#oemailMF').val(),
        recAdd1:jQuery('#recAdd1MF').val(),
        recAdd2:jQuery('#recAdd2MF').val(),
        recAddEnable:jQuery('#recAddEnableMF').val(),
        recTel:jQuery('#recTelMF').val(),
        recTelEnable:jQuery('#recTelEnableMF').val(),
        recZip:jQuery('#recZipMF').val(),
        recCity:jQuery('#recCityMF').val(),
        recState:jQuery('#recStateMF').val(),
        recUrl:jQuery('#recUrlMF').val(),
        recUrlEnable:jQuery('#recUrlEnableMF').val(),
        recPubNote:jQuery('#recPubNoteMF').val(),
        priContact:jQuery('#priContactMF').val(),
        priTitle:jQuery('#priTitleMF').val(),
        priPhone:jQuery('#priPhoneMF').val(),
        priEmail:jQuery('#priEmailMF').val(),
        priCell:jQuery('#priCellMF').val(),
        secContact:jQuery('#secContactMF').val(),
        secTitle:jQuery('#secTitleMF').val(),
        secPhone:jQuery('#secPhoneMF').val(),
        secEmail:jQuery('#secEmailMF').val(),
        secCell:jQuery('#secCellMF').val(),
        invContact:jQuery('#invContactMF').val(),
        invTitle:jQuery('#invTitleMF').val(),
        invPhone:jQuery('#invPhoneMF').val(),
        invEmail:jQuery('#invEmailMF').val(),
        invCell:jQuery('#invCellMF').val(),
        addContact:jQuery('#addContactMF').val(),
        addTitle:jQuery('#addTitleMF').val(),
        addPhone:jQuery('#addPhoneMF').val(),
        addEmail:jQuery('#addEmailMF').val(),
        addCell:jQuery('#addCellMF').val()},
        dataType: "json",
        success: function(data) {
        jQuery('#orgModal').modal('hide');
        },
        error: function(data) {     
        console.log(data);
        }
    });

这是我们获取 ID 的方式:

$app->post('/update/:id', 'updateOrg');

正如下面的评论链中所述 这是我们从错误函数 WHERE oid= 1216null 返回的示例

有人有什么想法吗?任何人?自从我们第一次构建系统以来,这一直困扰着我们。

我认为它可能是服务器端的,因为 ID 经过并被使用,但在语句的末尾有一个未附加到数字的空值。我已经检查过了。 在您询问绑定参数之前,这是一个长期目标。 经过进一步检查,它似乎返回了一个好的状态文本。状态为 200。

【问题讨论】:

  • 请发布您的代码
  • 请您准备好您的查询/部分代码好吗?
  • 你的代码在哪里? (不是代码截图)
  • 恭喜:您的查询容易受到SQL-injection的攻击。
  • 天啊...你为什么不使用参数!?!?!你甚至在使用准备......这太疯狂了。

标签: php mysql database slim


【解决方案1】:

好的,经过大量测试和研究,问题比我想象的要简单得多。事实证明,出于某种原因,在这个 api 文章中,除了答案之外,我们仍在积极回应 SQL。导致它以 200 的 http 响应返回。这是整个臭烘烘时间的问题。我注释掉了 echo $sql 命令,它现在的工作方式完全符合它的预期。我非常抱歉浪费了每个人的时间,回想起来这是一个非常明显的解决方案。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2014-02-15
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2012-03-20
    相关资源
    最近更新 更多