2

我正在开发一个用于每小时预订资源的系统。例如按小时或其他方式预订会议室。我几乎已经让所有功能正常工作,但有些事情远非完美,而且我有一个功能让我很烦恼。它正在预订一整周的资源。我的代码如下,但任何有助于提高效率的帮助将不胜感激。

谢谢!

史蒂夫

function bookweek($week) {
global $deskid, $date, $time, $member_id, $bookingTimes, $day0, $day1, $day2, $day3, $day4, $day5, $day6, $day7, $day8, $day9, $day10, $day11, $day12, $day13;

dbconnect();

switch($week) {
    case 1: 
        $dupecheck = mysql_query("SELECT * FROM booked where deskid='$deskid' AND date>='$day0' AND date <'$day7'");        
        if (mysql_num_rows($dupecheck) == 0) 
        {
            for($j = 0; $j< 7; $j++) 
            {   
                $daynumber=$j;
                $testing = "day".$daynumber;
                $daytotal= $$testing;
            for($i = 1; $i < count($bookingTimes)+1; $i++) 
            {
                $sql="INSERT INTO booked (date, time, deskid, member_id) VALUES ('$daytotal', '$i', '$deskid', '$member_id')";
                    $result = mysql_query($sql);
                    if (!$result) {
                        die('Invalid query: ' . mysql_error());
                    }
            }
            }
        }
        break;
    case 2:
        $dupecheck = mysql_query("SELECT * FROM booked where deskid='$deskid' AND date > '$day6'");         
        if (mysql_num_rows($dupecheck) == 0) 
        {
            for($j = 0; $j< 7; $j++) 
            {   
                $daynumber=$j+7;
                $testing = "day".$daynumber;
                $daytotal= $$testing;
            for($i = 1; $i < count($bookingTimes)+1; $i++) 
            {
                $sql="INSERT INTO booked (date, time, deskid, member_id) VALUES ('$daytotal', '$i', '$deskid', '$member_id')";
                    $result = mysql_query($sql);
                    if (!$result) {
                        die('Invalid query: ' . mysql_error());
                    }
            }
            }
        }
        break;
}
}
4

2 回答 2

1

如果在任何情况下使用整个 sql 块,这将很难维护。 让它更有效率。

function bookweek($week) {
global $deskid, $date, $time, $member_id, $bookingTimes, $day0, $day1, $day2, $day3, $day4, $day5, $day6, $day7, $day8, $day9, $day10, $day11, $day12, $day13;

$queryP = "SELECT * FROM booked where deskid='".$deskid."' AND date>";
dbconnect();
switch($week) {
case 1: 
       $dupecheck = mysql_query($queryP."='".$day0."' AND date <'".$day7."'");      
       break;
case 2:
       $dupecheck = mysql_query($queryP."'".$day6."'");         
       break;
}
    if (mysql_num_rows($dupecheck) == 0) 
[...]     

试试mysqli

现在代码更清晰了,很明显可以改进。

$queryP = "SELECT * FROM booked where deskid='".$deskid."' AND date>";
dbconnect();

switch($week) {
case 1: 
       $queryP .= "='".$day0."' AND date <'".$day7."'";
       break;
case 2:
       $queryP .= "'".$day6."'";
       break;
}
       $dupecheck = mysql_query($queryP);          
       if (mysql_num_rows($dupecheck) == 0) 
[...]

让我们进入循环。
为了更好地测试错误。我只会更改内部循环。

if (mysql_num_rows($dupecheck) == 0) 
  {
         for($j = 0; $j< 7; $j++) 
         {  
          $testing = "day".$j;
          $daytotal= $$testing;
          $sql = "INSERT INTO booked (date, time, deskid, member_id) VALUES ";
          $sqlTest = $sql;
          for($i = 1; $i < count($bookingTimes)+1; $i++) 
            {
              $sql .= "('".$daytotal."', '".$i."', '".$deskid."', '".$member_id."'),";
            }
          if ($sql == $sqlTest) {
                // nothing to be done or die
            } else {
            $sql = rtrim($sql,",");    
            result = mysql_query($sql);
              if (!$result) {
                 die('Invalid query: ' . mysql_error());
              }
           }
         }
 }
于 2012-10-27T15:29:11.313 回答
0

我认为如果你使用准备好的语句,你会节省很多 MySQL 资源。

于 2012-10-27T19:43:32.713 回答