gpt4 book ai didi

php - 意外的 T_VARIABLE,为什么我的下拉列表找不到我创建的变量?

转载 作者:行者123 更新时间:2023-11-29 21:39:17 26 4
gpt4 key购买 nike

我对 PHP 还很陌生。我创建了一个下拉列表,其中显示了(肉、素食或纯素食)成分列表。当用户注册到我的网站时,他们会选择自己遵循的饮食习惯,并将其插入到数据库的“dietID”列中。

我希望下拉列表自动仅显示肉类或素食或纯素食成分列表,具体取决于他们注册的用户“dietID”。

我收到的错误是

unexpected '$_SESSION' (T_VARIABLE) ..... on line 42.

(第42行是SELECT语句)

require_once("connect.php");
if (!$db_server) {
die("Unable to connect to MySQL: " . mysqli_connect_error());
} else {
mysqli_select_db($db_server, $db_database);
$query = "SELECT * FROM ingredients WHERE dietID="$_SESSION['diet']"";
$result = mysqli_query($db_server, $query);
if (!$result)
die("Query failed: " . mysqli_error($db_server));
while ($row = mysqli_fetch_array($result)) {
$str_options .= "<option value='" . $row['ID'] . "'>";
$str_options .= $row['ingredient'];
$str_options .= "</option>";
}
}

和形式:

<form method="post" action="chefsclaw.php">
Which ingredient are you interesting in finding out the healthy alternatives for?
<br>
<br>
<select name="choice">
<?php
echo $str_options;
?>
</select>
<br>
<br>
<input type="submit" name="submit" value="Lookup" />
</form>

注意,我已经在注册页面上为 DietID 创建了变量。这些变量是:

$db_diet = $row['dietID'];

$_SESSION['diet'] = $db_diet;

我将不胜感激任何指导。

编辑:其余表单处理代码:

<?php // connect to database
require_once 'connect.php';
require_once 'checklog.php';
require_once 'functions.php';
mysqli_select_db($db_server, $db_database);
$str_message = $message = $str_comments = $output = "";
if (!$db_server) {
die("Unable to connect to MySQL: " . mysqli_connect_error());
} else {
// this tests if look up form has an input
if(trim($_POST['submit']) == "Lookup") { // set to value of your submit
if (isset($_POST['choice'])) { //Capture form data, if anything was submitted
$choice = clean_string($db_server, $_POST['choice']);
$query = "SELECT ingredient, alternative, dietID FROM ingredients WHERE ID=$choice";
mysqli_select_db($db_server, $db_database); // query the database
$result = mysqli_query($db_server, $query);

if (!$result)
die("Database access failed: " . mysqli_error($db_server) . $query);

if ($row = mysqli_fetch_array($result)) { // if there are any rows, print out the contents
$output = 'The healthy alternatives for ' . $row['ingredient'] . ' could be ' . $row['alternative'] . ' which would suit a ' . $row['dietID'] . ' based diet. ';

} else {
$output = 'The ingredient selected was not found in the database';
}
mysqli_free_result($result);
} else {
$output = 'No ingredient was selected';
}
}

}

最佳答案

您对引号中的引号有疑问,请尝试此操作。

$query  = "SELECT * FROM ingredients WHERE dietID='".$_SESSION['diet']."'";

关于php - 意外的 T_VARIABLE,为什么我的下拉列表找不到我创建的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34665740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com