我是php的新手,我已经创建了一个项目,它将数据库中的数据作为用户选择,当项目执行时,它在某个方法上发生了错误,而我现在还没有如何解决它。 听到是PHP代码: -
<?php
$db = new PDO('sqlite:dinner.db');
$meals = array('breakfast','lunch','dinner');
if(in_array( $_POST['meal'], $meals)){
$stmt = $db->prepare('SELECT dish,price FROM meals WHERE meal LIKE ?');
$stmt->execute(array($_POST['meal']));
$rows = $stmt->fetchAll();
if(count($rows) == 0){
print "No dishes available now";
} else {
print "<table><tr><th>dish</th><th>price</th></tr>";
foreach($rows as $row) {
print "<tr><td>$row[0]</td><td>$row[1]</td></tr>";
}
print "</table>";
}
} else {
print "Unknown meal";
}
?>
那就是html页面代码:
<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<lable>Select the meal to see all the available dishes:
<input type="text" name="meal">
</lable><input type="submit" value="Ok">
</form>
</body>
</html>
,这是浏览器的输出: - enter image description here
答案 0 :(得分:0)
根据PHP Docs execute
,不应该有任何参数(void),它只会执行当前查询。
您应该通过bindValue
或bindParam
传递参数来填充查询。 Look at his example以正确的方式看待这种方式。
也许像这样的东西修复了它,$meal
必须只有一个字符串而不是数组:
$stmt = $db->prepare('SELECT dish, price FROM meals WHERE meal LIKE ?');
$stmt->bindValue(1, $meal, SQLITE3_TEXT);
答案 1 :(得分:0)
将你的参数传递给查询你需要使用bindparams而不是在这里执行如何:
<?php
$db = new PDO('sqlite:dinner.db');
$meals = array('breakfast','lunch','dinner');
if(in_array( $_POST['meal'], $meals)){
$stmt = $db->prepare('SELECT dish,price FROM meals WHERE meal LIKE ?');
$stmt->bindParam(1, $_POST['meal']);
$stmt->execute();
$rows = $stmt->fetchAll();
if(count($rows) == 0){
print "No dishes available now";
} else {
print "<table><tr><th>dish</th><th>price</th></tr>";
foreach($rows as $row) {
print "<tr><td>$row[0]</td><td>$row[1]</td></tr>";
}
print "</table>";
}
} else { print "Unknown meal";} ?>