doukanxi4246 2019-06-12 20:06
浏览 51
已采纳

API不允许使用文本

I've just created a simple API for a CAD/MDT I'm working on, I've managed to get it to show the correct information when I do /citations/userid/1. This will then display all the correct values from the SQL database however, if I do /citations/issued_by/kevingorman1000 it will just throw an error. I can't tell what the error is as I'm using Slim php and can't seem to get the errors to display.

Any ideas why it isn't working ? I've added my code below..

$app->get('/citation/issuedby/{issued_by}', function(Request $request, Response $response){

$issued_by = $request->getAttribute('issued_by');

$sql = "SELECT * FROM ncic_citations WHERE issuedby = $issuedby";
try{

    // Get DB Object
    $db = new db();
    // Call Connection to DB
    $db = $db->connect();

    $stmt = $db->query($sql);

    $issby = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;

    echo json_encode($issby);

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

Any ideas why this is the case? Does it only allow getting via number or do I need too do something else? First time using this and kinda new to PHP as well.

Thanks for any help.

  • 写回答

2条回答 默认 最新

  • dongzhu6900 2019-06-12 20:12
    关注

    Your problem is called SQL injection. You can solve it by using prepared statements. Never escape the values with quotes or anything else, as others might have suggested.

    $sql = "SELECT * FROM ncic_citations WHERE issuedby = ? ";
    $stmt = $db->prepare($sql);
    $stmt->execute([$issuedby]);
    $issby = $stmt->fetchAll(PDO::FETCH_OBJ);
    

    For a good tutorial on PDO and prepared statements I recommend: https://phpdelusions.net/pdo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料