在将csv上载到数据库时检查数据类型和必填字段

时间:2012-05-21 06:55:14

标签: php fgetcsv

我正在将csv文件上传到我的数据库中。这段代码工作正常。 但我想检查数据类型和必填字段是否填充。我不知道该怎么做。

include('db_info.php');

$subwarsdb = new mysqli($db_server, $db_user, $db_password, $db);

    if ($subwarsdb->errno)

        {

            printf("Unable to connect to the database: <br />  %s", $tangdb->error);

            exit();

        }

 $fp = fopen("boatdata.csv", "r"); // Open the file for reading 

while ($line = fgets($fp))  // Loop through each line 

     {

      list($class, $boatname, $boatnumber, $speed1, $speed2, $ftubes, $atubes, $torps, $depth) = split("\t", $line, 9); // Split the line by the tab delimiter and store it in our list 
echo "Class: " . $class . "<br><br>";

echo "Name: " . $boatname . "<br><br>";

echo "Hull Number: " . $boatnumber . "<br><br>";

echo "Speed Surface: " . $speed1 . "<br><br>";

echo "Speed Submerged: " . $speed2 . "<br><br>";

echo "Tubes Forward: " . $ftubes . "<br><br>";

echo "Tubes Aft: " . $atubes . "<br><br>";

echo "Torpedos: " . $torps . "<br><br>";

echo "Depth: " . $depth . "<br><br>";

echo "<br><hr><br>";

 $query = "INSERT INTO `subs` (`sub_id`,`class`,`sub_name`,`hull_number`,`surfspeed`,`subspeed`,`tubes_fore`,`tubes_aft`,`torpedos`,`depth`) values (NULL,'$class','$boatname','$boatnumber','$speed1','$speed2','$ftubes','$atubes','$torps','$depth')"; // Generate ur sql string 

$result=$subwarsdb->query($query);

if (! $result)

        {

            printf("Result Error: %s", $result->errno);

            exit();
        }

 }

我究竟如何检查数据类型和必填字段?

0 个答案:

没有答案