mysql查询中的变量

时间:2011-09-07 14:12:33

标签: php

for ($i=0; $i<$count; $i++) {
    $appid = $chk[$i];


    include "dbconnect.php";
    $selectquery = mysql_query("SELECT * FROM regform_admin WHERE tid = '$appid'");
    $fetch = mysql_fetch_array($selectquery);
    $tid = $fetch['tid']; $username = $fetch['username']; $c_month = $fetch['month']; $c_day =$fetch['day']; $c_year = $fetch['year'];
    $c_month2 = $fetch['month2']; $c_day2 =$fetch['day2']; $c_year2 = $fetch['year2']; 
    $pickup = "".$c_month."/".$c_day."/".$c_year."";
    $return = "".$c_month2."/".$c_day2."/".$c_year2."";
    $pickuploc = "".$fetch['pickupret']." "." ".$fetch['speclocation']."";
    $desti = "".$fetch['destination']." "." ".$fetch['location']."";
    $vehicle1 = $fetch['vehicle1'];
    $datesent = date("n j, Y; G:i"); ;
    $rand = rand(98765432,23456789);

    include "vehicledbconnect.php";
    $vquery = mysql_query("SELECT * FROM vehicletbl WHERE vehicle = '$vehicle1'");
            $getvquery = mysql_fetch_array($vquery);
            $maxcars = $getvquery['maxcars'];
            $carsleft = $getvquery['carsleft'];
            if ($carsleft == 0) {
            echo '
        <script language="JavaScript">
        alert("Cannot move reservation to Pending for payment status. No available vehicles left for this reservation.");
        </script>';

        echo "$vehicle1";

            }

大家好我的问题是,如果将车辆插入数据库查询中,$ vehicle不返回其值($ vquery = mysql_query(“SELECT * FROM vehicletbl WHERE vehicle ='$ vehicle1'”);)但是如果它得到了回应,它回归了它的价值。这里的逻辑是它将从vehicletbl中选择所有值,其中'vehicle'列中任何值的值将等于$ vehicle1。谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

您的查询出现了ZERO错误处理。尝试在查询调用中添加一些调试:

$result = mysql_query(...) or die(mysql_error());

其余的代码很难看,但看起来很“好”,所以开始查看为什么你没有从查询中得到任何回复。

永远不要假设查询成功。

答案 1 :(得分:0)

尝试此调试:

$sql = "SELECT * FROM vehicletbl WHERE vehicle = '" . $vehicle1 . "'";
$vquery = mysql_query($sql) or die(mysql_error() . "\n<br>$sql");

这就是我在sql中查找错误的方法。

答案 2 :(得分:-1)

Noob程序员?以下是一些需要了解的事项:

for ($i=0; $i<$count; $i++) {
    $appid = $chk[$i];

// Replaced By ...
foreach($chk as $appid){

http://php.net/manual/en/control-structures.foreach.php

// Include the file before the loop ! You're including 20 times your file, but you just need to do it once ! Another thing to know:
include_once("dbconnect.php");

http://php.net/manual/en/function.include-once.php

$desti = "".$fetch['destination']." "." ".$fetch['location']."";
// WHY ?? Isn't that easier to do this ?
$desti = $fetch['destination']."  ".$fetch['location'];

安全:

// Don't forget to escape your variables before putting it in mysql queries
$appid = mysql_real_escape_string($appid);
$selectquery = mysql_query("SELECT * FROM regform_admin WHERE tid = '$appid'");

Best way to defend against mysql injection and cross site scripting

还有其他评论,但请先尝试改善这些要点!