您好,我有一个网站,它接收一个汽车品牌,并生成一个与数据库对应的车辆ID。尽管有许多解决方案可用于将sql查询结果存储到变量,但在这种情况下它似乎不起作用。
我已经在数据库中尝试过sql语句,它可以使用1个结果输出,这是正确的。
<?php
session_start();
include "dbconn.php";
$carcat = $_SESSION['selectedcarcat'];
$carbrand = $_POST['carbrand'];
$userid = $_SESSION['loginid'];
$username = $_SESSION['loginname'];
$startdate = $_POST['date1'];
$enddate = $_POST['date2'];
$pick = $_POST['pickuploc'];
$return = $_POST['returnloc'];
$calqty = 0;
$selcaridsql = "SELECT carid FROM cars WHERE brand='$carbrand' ";
$caridresult = $dbcnx->query($selcaridsql);
$caridrow = mysql_fetch_object($caridresult);
$carid = $caridrow['carid'];
if (!$caridresult)
{
$errmessage = "Your carid select query failed.";
echo "<script type='text/javascript'>alert('$errmessage');</script>";
}
echo '<br>Debug 1 ';
echo '<br>The selected qty is '
.$qtyresult1.'<br />';
echo '<br>The calculated qty is '
.$calqty.'<br />';
echo '<br>The content carid is '
.$carid.'<br />';
echo '<br>The content userid is '
.$userid.'<br />';
echo '<br>The content start is '
.$startdate.'<br />';
echo '<br>The content end is '
.$enddate.'<br />';
echo '<br>The content pick is '
.$pick.'<br />';
echo '<br>The content return is '
.$return.'<br />';
echo '<br>The content carbrand is '
.$carbrand.'<br />';
?>
错误发生在&#34;内容carid为&#34;时显示空白结果。通过论坛后,似乎变量$carid
没有值,我可能错误地推断。
回声结果:
Debug 1
The selected qty is
The calculated qty is 0
The content carid is
The content userid is
The content start is 2016-10-28
The content end is 2016-10-29
The content pick is jurong
The content return is bishan
The content carbrand is Honda
&#13;
答案 0 :(得分:1)
显然在使用var_dump来调试Chris所述的问题之后,问题被缩小到mysql_fetch_object($caridresult);
收集的NULL值。由于数据库连接使用@$dbcnx = new mysqli('localhost','values','values','values');
,因此DB类检索数组值是错误的。因此,解决方案将是mysqli_fetch_array
。
这些代码可能对尝试调试的人有所帮助:
<?php // register.php
session_start();
include "dbconn.php";
$carcat = $_SESSION['selectedcarcat'];
$carbrand = $_POST['carbrand'];
$userid = $_SESSION['loginid'];
$username = $_SESSION['loginname'];
$startdate = $_POST['date1'];
$enddate = $_POST['date2'];
$pick = $_POST['pickuploc'];
$return = $_POST['returnloc'];
$calqty = 0;
$selcaridsql = "SELECT carid FROM cars WHERE brand='$carbrand' ";
echo $selcaridsql."<br>";
$caridresult = $dbcnx->query($selcaridsql);
echo "<br>".var_dump($caridresult);
if ($caridresult->num_rows >0 )
{
// if they are in the database register the user id
echo '<br>Hello more than 1 <br>';
}
else
{
echo '<br>Hello less than 1 <br>';
}
$caridrow = mysqli_fetch_array($caridresult);
echo var_dump($caridrow)."<br>";
$carid = $caridrow['carid'];
echo var_dump($carid)."<br>";
if (!$caridresult)
{
$errmessage = "Your carid select query failed.";
echo "<script type='text/javascript'>alert('$errmessage');</script>";
}
echo '<br>Debug 1 ';
echo '<br>The selected qty is '
.$qtyresult1.'<br />';
echo '<br>The calculated qty is '
.$calqty.'<br />';
echo '<br>The content carid is '
.$carid.'<br />';
echo '<br>The content userid is '
.$userid.'<br />';
echo '<br>The content start is '
.$startdate.'<br />';
echo '<br>The content end is '
.$enddate.'<br />';
echo '<br>The content pick is '
.$pick.'<br />';
echo '<br>The content return is '
.$return.'<br />';
echo '<br>The content carbrand is '
.$carbrand.'<br />';
?>
&#13;
特别感谢Chris帮助找到错误!
答案 1 :(得分:0)
我现在无法对此进行测试。
但是,您已确认$carid
没有值(为空)。
您已确认SQL语句在针对数据库运行时返回正确的值。因此,可能的问题在于您使用代码发送到数据库的查询。
您的查询:
$selcaridsql = "SELECT carid FROM cars WHERE brand='$carbrand' ";
我建议您修复查询:
$selcaridsql = "SELECT carid FROM cars WHERE brand='" + $carbrand + "'";
您可以发送字符串文字“$carbrand
”,除非您打印出要检查的SQL语句,否则不知道它。
你试过吗?
此外,请检查所有$_POST
的值,以确保它们符合您的想法。这对安全也非常重要。
获取那些println语句,让我知道你发现了什么: - )。
打印出所有内容并确保您的价值符合您的预期。
我99%肯定这就是问题所在。