提交PhP时出现致命错误

时间:2016-01-05 21:01:27

标签: php html mysql

我正在创建一个表单,我可以通过浏览器将记录添加到数据库中。当我按提交时,它会出现此错误

  

致命错误:在布尔值中调用成员函数execute()   第56行/srv/http/career.php

第56行基于此PhP行:

$result->execute($_POST);

它与数据库的连接无关,因为我能够查看已经创建的记录。

完整代码

HTML

<form method="POST">
    <label for="jobtitle">Job Title</label> <input type="text" name="jobtitle" /> <br>
    <label for="reference">Reference</label> <input type="text" name="reference" /> <br>
    <label for="salary">Salary</label> <input type="text" name="salary"/> <br>
    <label for="location">Location</label> <input type="text" name="location"/> <br> <br>
    <label for="description">Description</label> <input type="text" name="description"/> <br> <br>
    <input type="submit" value="submit" name="submit"/>
</form>

比索

<?php
if(isset($_POST['jobtitle'],$_POST['reference'],$_POST['salary'],$_POST['location'],$_POST['description'])){
    $result= $pdo->query('INSERT INTO jobs (job_title, job_ref, job_salary, job_location, job_desc)
        VALUES ("' . $_POST['jobtitle'] . '","' . $_POST['reference'] . '","' . $_POST['location'] . '","' . $_POST['description'] .'")');
        unset($_POST['submit']); 

    $result->execute($_POST);
}
?>

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

不要使用query()并将值连接到SQL字符串中,而是尝试使用这样的预准备语句:

$stmt = $pdo->prepare('INSERT INTO jobs (job_title, job_ref, job_salary, 
                       job_location, job_desc) VALUES (?, ?, ?, ?, ?)');
$stmt->bindValue(1, $_POST['jobtitle']);
$stmt->bindValue(2, $_POST['reference']);
$stmt->bindValue(3, $_POST['salary']);
$stmt->bindValue(4, $_POST['location']);
$stmt->bindValue(5, $_POST['description']);
$stmt->execute();

这种方法有很多好处,包括让您更容易判断您何时错过了您要插入的一个值(薪水)。