提问人:ginbosqueeze 提问时间:10/30/2023 最后编辑:Shadowginbosqueeze 更新时间:10/31/2023 访问量:81
MySQL在提交查询 #PHP 时无法识别变量的值
MySQL not recognizing variable's value when submitting a query #PHP
问:
每当我尝试发布MySQL查询时,我都会检索除...这是我在创建一个 HTML 下拉列表后收到的值,该下拉列表在属于当前登录用户的数据库中检索汽车模型。$your_model
如果我在检查查看的“IF”语句之前和之后进行回显,我能够看到......但是一旦我提交或发布我的里程表读数,MySQL就会看到值,值,但看不到值。if(isset($_POST['odo']))
$your_model
$odo
$time
$your_model
我尝试在我的代码之前和之后编写那段MySQL代码。我试图将它嵌套到代码中的不同区域,甚至将其嵌套在我执行 if num_of_cars > 0 的地方
我尝试过用引号、单引号、带或不带引号的大括号括起来等$your_model
"$your_model"
'$your_model'
{$your_model}
'{$your_model}'
我也尝试过,但不允许MySQL知道变量中的内容'".$your_model."'
我真的尝试了我能想到的一切,并在互联网上搜索。我看到几个论坛有类似的问题......一个没有解决,另一个是通过将大括号包裹在变量周围来解决的,以便MySQL可以识别它。
老实说,我相信这与命令有关......我想知道一旦我提交里程表读数来查询数据库,如果不知何故因为页面刷新,MySQL丢失了变量的值?$_POST
$your_model
<?php
require_once 'header.php';
if (!$loggedin) die("</div></body></html>");
if (isset($_GET['view'])) $view = sanitizeString($_GET['view']);
else $view = $user;
if ($view != "")
{
if ($view == $user) $name1 = $name2 = "Your";
else
{
$name1 = "<a href='members.php?view=$view'>$view</a>'s";
$name2 = "$view's";
}
echo "<h4>$name1 Car Portal</h4>";
showProfile($view);
echo "<br><br>";
$car_query = "SELECT model FROM cars WHERE user='$user'";
$car_result = queryMysql($car_query);
$num_of_cars = $car_result->num_rows;
if ($num_of_cars > 0) {
while ($row = mysqli_fetch_assoc($car_result)) {
$selectedModels[] = $row['model'];
}
}
if (isset($_POST['selectAModel'])) $your_model = sanitizeString($_POST['selectAModel']);
echo <<<_END
<form method='post' action='car_test.php?view=$view'>
<select name="selectAModel">
<option value='Select Model'>Select A Model </option>
_END;
foreach($selectedModels as $modelName) {
echo "<option name='$modelName' value='$modelName'> $modelName </option>";
}
echo <<<_END
</select>
<input type='submit' name='post_car' value='Submit Car Model'>
</form>
_END;
echo "<br><br>";
echo "<div style='text-align:center;'><h3>Your $your_model Odometer Reading</h3></div>";
if (!isset($_POST['odo']))
{
echo "<span class='info' style='color:red'>You have not entered your $your_model current odometer reading yet</span><br>";
}
if (isset($_POST['odo']) && isset($_POST['selectAModel']))
{
$odo = sanitizeString($_POST['odo']);
if ($odo != "")
{
$time = time();
queryMysql("UPDATE cars SET odo = '$odo', time = '$time' WHERE user = '$user' AND model = '$your_model'");
}
}
echo <<<_END
<form method='post' action='car_test.php?view=$view'>
<input type='text' id='odo' name='odo'>
<label for='odo'>Current Odometer Reading</label><br>
<input data-transition='slide' type='submit' value='Submit Odometer Reading'>
</form>
_END;
echo "<br>";
date_default_timezone_get();
if (isset($_GET['erase']))
{
$erase = sanitizeString($_GET['erase']);
queryMysql("DELETE FROM car WHERE odo='$erase' AND user='$user'");
}
$query = "SELECT * FROM car WHERE user='$view' ORDER BY time DESC";
$result = queryMysql($query);
$num = $result->num_rows;
for ($j = 0; $j < $num; ++$j)
{
$row = $result->fetch_array(MYSQLI_ASSOC);
if ($row['odo'] != "" && $row['user'] == $user)
{
echo date('M jS \'y g:ia:', $row['time']);
echo "<br><br>";
echo "<a href='car.php?view=" . $row['user'] . "'>" . $row['user']. "</a>";
echo "<br>";
echo "last oil change was at: " . $row['odo'] . "<br>";
$current_odo = ((int) $row['odo']);
$eight_thousand = 8000;
$next_change = $current_odo + $eight_thousand;
echo "your next oil change is at: $next_change";
if ($row['user'] == $user)
echo "<br><br>[<a href='car.php?view=$view" . "&erase=" . $row['odo'] . "'>erase</a>]";
echo "<br><br><br>";
}
}
}
?>
</div><br>
</body>
</html>
答:
Web 请求是无状态的。它们不会自动记住从一个请求到下一个请求的任何内容。每次加载页面、发布表单或向服务器发送 AJAX 请求时,PHP 脚本都会从请求的脚本的开头运行,并且就像以前从未运行过一样。这有一些优点,但一个明显的缺点是它不保留任何变量值。
为了解决这个问题,你必须采取特定的步骤来保留你想要在请求之间保留的数据,无论是针对特定用户还是一般存储,然后在需要时检索它。
在您的方案中,您有两种形式,一种用于模型,另一种用于里程表数据。这意味着将有两个单独的请求到服务器。因此,如果您在一个请求中提交了包含模型的表单,则在包含里程表读数的请求中,模型值将不可用。
您可以将两个字段放在一个表单中并将它们一起提交,或者采取措施在后续请求中保留/传递模型值 - 例如,将其放入会话中然后检索它,或者将模型值放入里程表表单上的隐藏字段中。
下面是一个简单的示例,说明如何使用会话:
<?php
session_start(); //remember to use this to enable Session usage in your script
require_once 'header.php';
//...etc, until you get to
if (isset($_POST['selectAModel']))
$your_model = sanitizeString($_POST['selectAModel']);
$_SESSION["your_model"] = $your_model; //add the model data to the session
}
//then we get to:
//if (isset($_POST['odo']) && isset($_POST['selectAModel'])) - this can't work because both POST values won't exist at the same time. Replace it with:
if (isset($_POST['odo']))
{
if (isset($_SESSION["your_model"])) {
$your_model = $_SESSION["your_model"];
}
else
{
die("No valid model value set, cannot proceed to save odometer reading");
}
//...etc
评论
echo "Here is {$variable} example";
echo "Here is $variable example";
echo "Here is ".$variable." example";
echo 'Here is '.$variable.' example';