0

While creating a statistics program I'm keeping track how many aircraft visit an aerodrome per hour.

My script calculates the number of flights in the last hour and saves this to the database. my i keep getting mysql syntax error if I copy the statement in phpmyadmin it works

$date = 2013091713;
//$date = date('YmdH', mktime(date('H')));

$sql = '';
foreach ($aerodrome as $icao => $value) {
    $sql .= "INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) 
             VALUES('". $date ."', '" . $icao . "' , " . (isset($value['inboud']) ? $value['inboud'] : 0)  . ", " . (isset($value['outbound']) ? $value['outbound'] : 0) . ");";
}
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'EBBR' , 0, 1);
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'ELLX' , 0, 1);
if(mysql_query($sql))
{

} else{
    die(mysql_error());
}

EDIT: Mysql error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) VALUES('2013' at line 2

EDIT2: Fixed the single missing '

EDIT3: On request of the programming student a print_r of his solution

INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) 
             VALUES('2013091713', 'EBBR' , '', '1'),
             VALUES('2013091713', 'ELLX' , '', '1')
You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use 
near 'VALUES('2013091713', 'ELLX' , '', '1')' at line 3
4

3 回答 3

0

建议您使用 PDO http://in1.php.net/manual/en/ref.pdo-mysql.php或 MySQLi 扩展,因为 PHP 5.5 将不再支持 MySQL 扩展

$date = 2013091713;
//$date = date('YmdH', mktime(date('H')));

$db_conn = new PDO('mysql:host=<host>;dbname=<database name>', '<username>', '<password>');


try{
$sql = $db_conn->prepare("INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) VALUES (:date,:aerodrome,:inbound,:outbound)");

foreach ($aerodrome as $icao => $value) {
    $inbound = isset($value['inboud']) ? $value['inboud'] : 0;
    $outbound = isset($value['outbound']) ? $value['outbound'] : 0;
    $sql->execute(
            array(
                ":date" => $date,
                ":aerodrome" => $aerodrome,
                ":inbound" => $inbound,
                ":outbound" => $outbound
            )
    );
}
}
catch (PDOException $pe){
    print $pe->getMessage();
}
于 2013-10-01T21:26:32.407 回答
0

您在和'之间缺少冒号。$date$icao

更正的代码:

$date = 2013091713;
//$date = date('YmdH', mktime(date('H')));

$sql = '';
 $sql .= "INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) ";
foreach ($aerodrome as $icao => $value) {
    $inbound = isset($value['inboud']) ? $value['inboud'] : 0;
    $outbound = isset($value['outbound']) ? $value['outbound'] : 0;
    $sql .= "
             VALUES('". $date ."', '" . $icao . "' , '" . $inboud  . "', '" . $outbound . "'),";
}

$sql = substr($sql, 0, strlen($sql)-1);
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'EBBR' , 0, 1);
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'ELLX' , 0, 1);
if(mysql_query($sql))
{

} else{
    die(mysql_error());
}
于 2013-10-01T20:48:24.690 回答
0

您尚未关闭单引号以将其$date更改'". $date ."'". $date ."'

$sql .= "INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) 
             VALUES('". $date ."', '" . $icao . "' , " . (isset($value['inboud']) ? $value['inboud'] : 0)  . ", " . (isset($value['outbound']) ? $value['outbound'] : 0) . ");";
于 2013-10-01T20:48:43.610 回答