dongzhuo1958 2014-09-15 15:25
浏览 40
已采纳

是否可以用来自html表单的输入替换SQL查询的限制值?

This is my query: SELECT * FROM learning_assessment.tbl_qna order by rand() limit 10;


tbl_qna is my table for questions. What I wanted to happen is to substitute the limit value "10" to a number coming from an input of the admin.

<html>
<head></head>
<body>
<form action="#" method="post">
Number of questions: <br>
<input type="text" name="numQ"></input>
<input type="submit" value="Number of Questions to Generate"  name="save"></input>
</form>

Is it possible to get the input value from


<input type="text" name="numQ"></input>

and substitute it to my query limit value (10) , so that the user could generate his desired number or questions like 50 or 100.

  • 写回答

1条回答 默认 最新

  • douyun1972 2014-09-15 15:29
    关注

    Yes you can use a parameter

    $sth = $dbh->prepare("SELECT * FROM learning_assessment.tbl_qna order by rand() limit :limit;")
    $sth->bindParam(":limit",$_POST['numQ'],PDO:PARAM_INT);
    $result = $sth->execute();
    

    I'm most comfortable with PDO, but you can do the same with MySQLi

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件