Home  >  Article  >  Backend Development  >  我这个代码为什么提示错误?

我这个代码为什么提示错误?

WBOY
WBOYOriginal
2016-06-06 20:08:31851browse

我这个代码为什么提示错误?

<code>


      <meta charset="utf-8">
    <title></title>


<form method="post">
    <input type="text" name="num">
    <button type="submit">提交</button>
</form>
<?php $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$stmt=$pdo->prepare("select * from qq where num=?");
$stmt->execute($num);        //这一行提示错误
$res=$stmt->fetchall(PDO::FETCH_ASSOC);

?>

</code>

回复内容:

我这个代码为什么提示错误?

<code>


      <meta charset="utf-8">
    <title></title>


<form method="post">
    <input type="text" name="num">
    <button type="submit">提交</button>
</form>
<?php $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
$stmt=$pdo->prepare("select * from qq where num=?");
$stmt->execute($num);        //这一行提示错误
$res=$stmt->fetchall(PDO::FETCH_ASSOC);

?>

</code>

自己思考不解释了 1- 表单提交之后数据会出现在以下数组中$_REQUEST(所有外部参数), $_GET(get方式提交) 和 $_POST (post方式提交),而你的例子中提交后保存在$_POST['num']中,而不是$num
2- 其实没有2,变量不存在导致调用方法的时候参数数量不足。

<code>


      <meta charset="utf-8">
    <title></title>


<form method="post">
    <input type="text" name="num">
    <button type="submit">提交</button>
</form>
<?php if (isset($_POST['num'])) {
    $pdo=new PDO("mysql:host=localhost;dbname=t1","root","");
    $stmt=$pdo->prepare("select * from qq where num=?");
    $stmt->execute(array($_POST['num']));
    $res=$stmt->fetchall(PDO::FETCH_ASSOC);
    print_r($res);
}
?>

</code>

大哥 pdo 如果用 Prepare那就需要绑定值啊,你这个$num 明明是 null 能不给你报错吗
看你的意思应该是这样 $num = trim($_POST['num']);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn