我需要从我的单选按钮中获取值并添加到where子句的字符串中以更新将作为查询的一部分运行的where子句。我不确定如何将$ _POST ['answer']连接到第二个if块中的$ where
当前代码:
<form action="assignment.php" method="POST">
<input type="submit" name="start" value="start game">
<?php
$query = "SELECT `message`, `parentID`,`answerYesID`, `answerNoID`, `nodeID` FROM `creature`";
$where = "";
if (isset($_POST['start'])){
$where = "WHERE `nodeID` ='1'";
}
if (isset($_POST['submit']) && (isset ($_POST['answer']))){
$where = "WHERE `nodeID` =";
}
$result = mysqli_query($dbconn, $query.$where);
$row = mysqli_fetch_assoc($result) ;
echo $row['message'];
?>
<input type="radio" name="answer" value="<?php echo $row['answerYesID'];?>">yes
<input type="radio" name="answer" value="<?php echo $row['answerNoID'];?>">no
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
答案 0 :(得分:2)
$where = "WHERE `nodeID` = " . $_POST['answer'];
或如果您需要引号:
$where = "WHERE `nodeID` = '{$_POST['answer']}'";
答案 1 :(得分:1)
你做对了,可能是你错过了creature
和where
之间的空格,试试:
if (isset($_POST['start']) && isset ($_POST['answer'])){
#always a good idea to clean user input
$where = " WHERE `nodeID` = '".mysqli_real_escape_string($dbconn,$_POST['answer'])."'";
#^ left a space here
}
#You do not need the second isset().
$result = mysqli_query($dbconn, $query.$where);
答案 2 :(得分:0)
首先,你所做的是一个非常糟糕的主意!您可以为单选按钮指定值,您可以根据无线电值更改查询中的ID。
要获得答案,请使用此
$result = mysqli_query($dbconn, $query.' '.$where);
我认为您需要$query
和$where
之间的空格
答案 3 :(得分:0)
<form action="assignment.php" method="POST">
<input type="submit" name="start" value="start game">
<?php
$query = 'SELECT `message`, `parentID`,`answerYesID`, `answerNoID`, `nodeID`
FROM `creature`';
$where = ' ';
if (isset($_POST['start'])){
$node = 1;
$where .= 'WHERE `nodeID` = ?';
} elseif (isset($_POST['submit']) && (isset ($_POST['answer']))){
$node = $_POST['answer'];
$where .= 'WHERE `nodeID` = ?';
}
if ($stmt = $dbconn->prepare($query.$where)) {
$stmt->bind_param('i', $name);
$stmt->execute();
while ($row = $stmt->fetch()) {
echo $row['message'];
}
}
?>
<input type="radio" name="answer" value="<?php echo $row['answerYesID'];?>">yes
<input type="radio" name="answer" value="<?php echo $row['answerNoID'];?>">no
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>