简体   繁体   English

PDO查询未插入且没有错误

[英]PDO query not inserting and no error

Im trying to execute this code but my code dies because it is not able to insert into the table for some reason.the error given is "there was an error in storing to table" I have looked over my code but cannot see anything wrong with my PDO insert into query nor php error log. 我试图执行此代码,但由于某些原因而无法将其插入表中,因此我的代码死了。给出的错误是“存储到表中存在错误”,我查看了我的代码,但看不到任何错误我的PDO插入查询或php错误日志。

try {
        $result = $s3->putObject([
                'Bucket' => $config['s3']['bucket'],
                'Key' => "uploads/{$name_of_uploaded_file}",
                'Body' => fopen($path_of_uploaded_file, 'rb'),
                'ACL' => 'public-read'
            ]);

            if (is_uploaded_file($_FILES['uploaded_file']['size'])){
                  //send items to pending database

                  //inserts in pending db
                  $sql = "INSERT INTO pending (id,photo,title,description,name,pLike,pDislike) VALUES ('', :photo, :title, :description, :name, :pLike, :pDislike)";
                  $stmt = $conn->prepare($sql); 
                  $stmt->bindParam(':title', $title);
                  $stmt->bindParam(':photo', $result);
                  $stmt->bindParam(':description', $story);
                  $stmt->bindParam(':name', $first_name);
                  $stmt->bindValue(':pLike', 0, PDO::PARAM_INT);
                  $stmt->bindValue(':pDislike', 0, PDO::PARAM_INT);
                  $stmt->execute();       
            }else {
                die("there was an error in storing to table");        
            }

        //remove the file
            unlink($path_of_uploaded_file);
    } catch(S3Exception $e){
        die("there was an error");
    }

It already says in the manual : 它已经在手册中说:

Returns TRUE if the file named by filename was uploaded via HTTP POST. 如果以filename命名的文件是通过HTTP POST上传的,则返回TRUE。

For proper working, the function is_uploaded_file() needs an argument like 为了正常工作,函数is_uploaded_file()需要一个类似

$_FILES['userfile']['tmp_name'] , $_FILES['userfile']['tmp_name']

the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work. 客户端计算机$_FILES['userfile']['name']上载文件$_FILES['userfile']['name']无效。

Right now, you're pointing into the size index: 现在,您要指向size索引:

is_uploaded_file($_FILES['uploaded_file']['size']
                                        // ^

Should be: 应该:

if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){
try{
                  $sql = "INSERT INTO pending (id,photo,title,description,name,pLike,pDislike) VALUES ('', :photo, :title, :description, :name, :pLike, :pDislike)";
                  $stmt = $conn->prepare($sql); 
                  $stmt->bindParam(':title', $title);
                  $stmt->bindParam(':photo', $result);
                  $stmt->bindParam(':description', $story);
                  $stmt->bindParam(':name', $first_name);
                  $stmt->bindValue(':pLike', 0, PDO::PARAM_INT);
                  $stmt->bindValue(':pDislike', 0, PDO::PARAM_INT);
                  $stmt->execute();     
}
catch(PDOException $e){
echo 'error in query:  '.$e->getMessage();
}  

In the first part of your if statement will echo the error in your query (if any) 在if语句的第一部分中,将回显查询中的错误(如果有)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM