0

我有一个非常粗略的脚本来完成每年参加不同活动的团队填充我的一个数据库表的单调任务。但是,如果我不必每次都返回并更正数据库中的年份,那会好很多。

我有一个查询的年度事件表,这反过来又允许脚本获取该事件的团队列表。使用以下设置年份:

$year = date('Y');

对于每个团队-事件-年份组合,将触发以下查询:

INSERT INTO attendance (`team`,`event`,`year`) VALUES ('$team','$event','$year');

0000一切看起来都很好(在我看来),但是即使脚本2012在调试时打印,年份也会像在数据库中一样返回。

我的代码如下:

// from shared parent script

$year = (date('m') < 9) ? date('Y') : date('Y')+1;

// attendance.php - file with code in question

<?php
header("Refresh: 43200;"); // refresh every 12 hours
ob_start();

echo "<p>Filling event rosters for the {$year} season.</p>"; ob_flush(); flush();

function getTeamList($event) {
    echo "<h3>Event '{$event}':</h3>"; ob_flush(); flush();

    echo "Removing old entries... "; ob_flush(); flush();
    mysql_query("DELETE FROM attendance WHERE event='$event' AND year='$year'");

    echo "Loading list from FIRST TIMS... "; ob_flush(); flush();
    $page =
file_get_contents("https://my.usfirst.org/myarea/index.lasso?page=teamlist&event_type=FRC&event=$event&year=$year&sort_teams=number");
    $page = explode('<td nowrap>',$page);
    $page = implode('<td>',array_slice($page,'1'));
    if ($event == 'CMP') {
        $page2 =
file_get_contents("https://my.usfirst.org/myarea/index.lasso?page=teamlist&event_type=FRC&event=$event&year=$year&sort_teams=number&skip_teams=250");
        $page2 = explode('<td nowrap>',$page2);
        $page2 = implode('<td>',array_slice($page2,'1'));
        $page = '<html><body><center><table>
<tr>
<td>'.$page.' <tr bgcolor="#FFFFFF">
        <td>'.$page2;
    }
    $timsList = new DOMDocument;
    $timsList->loadHTML($page);
    $listings = $timsList->getElementsByTagName('a');

    echo "Adding all current entries... "; ob_flush(); flush();
    foreach ($listings as $listing) {
        if (is_numeric($listing->nodeValue)) {
            $team = $listing->nodeValue;
            $query = "INSERT INTO attendance (`team`,`event`,`year`) VALUES ('$team','$event','$year')";    // Build SQL query for events
            mysql_query($query);
        }
    }
    echo "Done."; ob_flush(); flush();
    if ($event == 'CMP') {
        $div = array('archimedes','curie','galileo','newton');
        foreach ($div as $division) {
            mysql_query("DELETE FROM attendance WHERE event='$division' AND year='$year'");
            $page =
file_get_contents("https://my.usfirst.org/myarea/index.lasso?page=teamlist&event_type=FRC&event=$event&division=$division&year=$year&sort=teamnum");
            $page = explode('<td nowrap>',$page);
            $page = implode('<td>',array_slice($page,'1'));
            $timsList = new DOMDocument;
            @$timsList->loadHTML($page);
            $listings = $timsList->getElementsByTagName('a');
            foreach ($listings as $listing) {
                if (is_numeric($listing->nodeValue)) {
                    $team = $listing->nodeValue;
                    $query  = "INSERT INTO attendance (`team`,`event`,`year`) VALUES ('$team','$division','$year')";    // Build SQL Query
for Divisions
                    mysql_query($query);
                }
            }
        }
    }
}


if (isset($_GET["event"])) {
    $event = strtoupper(mysql_real_escape_string($_GET["event"]));
    mysql_select_db("frc");
    getTeamList($event);
} else {
    mysql_select_db("frc_{$year}scouting");
    $events = mysql_query("SELECT id FROM event WHERE event.start >= NOW()") or die(mysql_error());
    mysql_select_db("frc");
    while ($row = mysql_fetch_array($events)) {
        $event = $row['id'];
        getTeamList($event);
    }
}
?>
4

4 回答 4

1

MYSQL中年份字段的数据类型是什么?如果将其设置为日期时间字段,则您的日期无效,它将返回该年的 0000。(无效日期在 mysql 日期时间中存储为 0000-00-00 00:00:00)

您可以更改数据库字段以要求 INT 使其工作。另一种选择是将年份发送为 $year.'-01-01 01:01:01' 以将其存储为日期的一月第一个。但考虑到那是虚假的一年,为什么要存储额外的数据?只需使用INT。

更新:

您显示的代码是正确的。它没有错。您没有显示的代码中一定有一些事情发生了,或者您认为您按照您所说的设置了所有内容但没有。

$mysqli = new mysqli(...);

$team = 10;
$event = 'Testing';
$year = date('Y');
$query = "INSERT INTO attendance (`team`,`event`,`year`) VALUES ('$team','$event','$year')";

print $query;
$mysqli->query($query);

使用与 mysql 中的确切日期类型一样的魅力。

更新 2:

您在函数中使用 $year,但未包含全局 $year;

function getTeamList($event) {
    global $year;
    //....
}
于 2012-12-04T09:09:11.603 回答
1

年份只接受 1901 - 2155

MySQL 年份类型只接受特定范围内的年份。如果您插入无效值,它将返回零:

mysql> create database so;
Query OK, 1 row affected (0.00 sec)

mysql> use so;
Database changed
mysql> create table example ( y year(4) );
Query OK, 0 rows affected (0.09 sec)

mysql> insert into example values (1234);
Query OK, 1 row affected, 1 warning (0.08 sec)

mysql> select * from example;
+------+
| y    |
+------+
| 0000 |
+------+
1 row in set (0.00 sec)

mysql> insert into example values (2012);
Query OK, 1 row affected (0.07 sec)

mysql> select * from example;
+------+
| y    |
+------+
| 0000 |
| 2012 |
+------+
3 rows in set (0.00 sec)

mysql> 

鉴于这就是 MySQL 的行为方式 - 插入的值很可能不是数字,或者不是此范围内的数字。

于 2012-12-04T09:19:59.300 回答
0

你可以试试 ::

INSERT INTO 
attendance (`team`,`event`,`year`) 
VALUES  ('$team','$event',YEAR(CURRENT_DATE))
于 2012-12-04T09:04:14.917 回答
0

如果年份字段的类型是整数,则更改为:

INSERT INTO attendance (`team`,`event`,`year`) VALUES ('$team','$event',$year);
于 2012-12-04T09:04:58.980 回答