1

我有一个问题,我的脚本有三个mysql_query应该一个接一个使用,我正在尝试创建一个脚本,通过将其状态从已售出 =“否”更改为“是”来预订门票,该脚本计算用户拥有的门票数量在 html 表单上输入,该表单为服务器端提供了一个变量,其中包含名为 = 的票数$tickets

提示:这是一个模型,所以不需要 mysql 注入安全性

这是我的代码:

//get ticket status
    $eventTicket = mysql_query("SELECT eventTickets FROM beventreservation WHERE eventId = '$eventId'") or die(mysql_error());
    $ticketrow = mysql_fetch_array($eventTicket) or die(mysql_error());


    //test... which is working !
    echo $ticketrow['eventTickets'];


    //get classId from classes
    $selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());    

    $classrow = mysql_fetch_array($selectClass) or die(mysql_error());

    //this var is to define which class the user used
    $choosedClass = $classrow['classId'];

//test ... which did not work !!!
echo $classrow['classId'];

        if ($ticketrow['eventTickets'] == "Yes")

    {
        for($counter=1;$counter<$numberOfTickets;$counter++)

        {
            $bookTicket = mysql_query("UPDATE unites SET ticketSold = 'Yes' WHERE businessreservationIdFk = '$eventId' AND classIDfk ='$choosedClass'") or die(mysql_error());  
            echo "ticket ". $counter . "  done !";


        }

    }

该脚本没有获取此语法,并且我的页面上没有显示错误!

$classrow = mysql_fetch_array($selectClass) or die(mysql_error());

另外,我试图$tickets在这个语法之后回显变量,它没有出现,在同一个脚本页面上获取超过 mysql_query 是否有问题?请告诉我我哪里出错了。

4

1 回答 1

4

不要与呼叫die()一起mysql_fetch_*()呼叫。如果没有返回行,则mysql_fetch_array()返回,即使没有错误,也会FALSE触发您的脚本并终止您的脚本。die()由于您已经$selectClassmysql_query()调用中进行了错误检查,因此您知道它已经成功。

// This query returned no rows, but was successful syntactically and functionally.
$selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());    

相反,测试是否返回了行:

if (mysql_num_rows($selectClass) > 0) {
  // Fetch and do other stuff
   $classrow = mysql_fetch_array($selectClass);
   $choosedClass = $classrow['classId'];
   // etc...
   // etc...
}
else {
  // Do whatever you need to do if no rows return
}
于 2012-04-28T12:41:30.330 回答