值无法上载到数据库

时间:2017-08-05 13:05:28

标签: php csv pdo

我正在尝试通过以下代码将csv文件的值上传到数据库

sqlite3 *database;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"DB_NAME.sqlite"];
sqlite3_stmt *updateStmt = nil;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
    const char *sql = [[NSString stringWithFormat:@"update restaurants Set rating = ? WHERE restaurantid = '%@'", rat.ratingClubID] UTF8String];

    if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK){
        NSLog(@"Error while creating update statement. %s", sqlite3_errmsg(database));
     }

    if (sqlite3_bind_text(updateStmt, 1, [rat.ratingValue  UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK){
        NSLog(@"Error while binding value. %s", sqlite3_errmsg(database));
     }

    if (sqlite3_step(updateStmt) != SQLITE_DONE){
        NSLog(@"Error  %s", sqlite3_errmsg(database));
    }

    sqlite3_finalize(updateStmt);
    sqlite3_close(database);
}else{
    NSLog(@"Error while opening database");
}

它返回以下错误

    <?php
include('../connection.php');
$class = $_POST['class'];
echo "hello";

if(isset($_FILES['filename'])){
   echo "hello";
   $filename = $_FILES['filename']['name']; 
   $tmpname = $_FILES['filename']['tmp_name'];
   $filesize = $_FILES['filename']['size'];
   echo $tmpname;
   $file = fopen("$tmpname","r");

   while(!feof($file)){
     $data = fgetcsv($file);
     $q = $data[0];
     echo $q;
     $query =  $connection->prepare("INSERT into mark 
              (stuname,stuid,english,maths,science) values 
              (:stuname,:stuid,:english,:maths,:science)");
     $query->bindParam(":stuname",$q);
     $query->bindParam(":stuid",$data[1]);
     $query->bindParam(":english",$data[4]);
     $query->bindParam(":maths",$data[5]);
     $query->bindParam(":science",$data[6]);
     $query->execute();
  }
  fclose($file); 
}
?>

但是我已经完成Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'stuname' cannot be null in C:\xampp\htdocs\sprwork\admin\csv.php:25 Stack trace: #0 C:\xampp\htdocs\sprwork\admin\csv.php(25): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\sprwork\admin\csv.php on line 25 并尝试通过将值存储在变量$ q中进行打印,但是当我尝试将它们推送到我的数据库时它会出现上述错误

1 个答案:

答案 0 :(得分:0)

通过上面的代码,值被更新到数据库,但是在所有引起错误的名称之后它甚至读取了空值我尝试了这个并且得到了工作

while(!feof($file))
        {
            $data = fgetcsv($file);
            if($class == "6" or $class == "4" or $class == "5" or $class == "7")
            {
                if($data[0] != NULL)
                {
                    $query =  $connection->prepare("INSERT into mark (stuname,stuid,english,maths,science) values (:stuname,:stuid,:english,:maths,:science)");
                    $query->bindParam(":stuname",$data[0]);
                    $query->bindParam(":stuid",$data[1]);
                    $query->bindParam(":english",$data[2]);
                    $query->bindParam(":maths",$data[3]);
                    $query->bindParam(":science",$data[4]);
                    $query->execute();
                    $count = $query->rowCount();
                }
                else
                {
                    if($count>0)
                    {
                        echo "the database is successfully updated";
                    }
                    break;
                }
            }
       }

因此它在读取所有colomns和下一个clomn之后出现循环,因为它为null,它打破了循环,并且随着csv文件的最后一行得到更新,因此$ count的值可能为1因此打印值aand符