PDO查询未插入且没有错误

时间:2021-05-03 15:39:52

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");
    }

2 个解决方案

#1


It already says in the manual:

它已经在手册中说:

Returns TRUE if the file named by filename was uploaded via HTTP POST.

如果通过HTTP POST上传由filename命名的文件,则返回TRUE。

For proper working, the function is_uploaded_file() needs an argument like

为了正常工作,函数is_uploaded_file()需要一个像这样的参数

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

the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work.

客户端计算机上的上传文件的名称$ _FILES ['userfile'] ['name']不起作用。

Right now, you're pointing into the size index:

现在,你指的是尺寸指数:

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

Should be:

if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){

#2


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语句的第一部分将回显查询中的错误(如果有的话)

#1


It already says in the manual:

它已经在手册中说:

Returns TRUE if the file named by filename was uploaded via HTTP POST.

如果通过HTTP POST上传由filename命名的文件,则返回TRUE。

For proper working, the function is_uploaded_file() needs an argument like

为了正常工作,函数is_uploaded_file()需要一个像这样的参数

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

the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work.

客户端计算机上的上传文件的名称$ _FILES ['userfile'] ['name']不起作用。

Right now, you're pointing into the size index:

现在,你指的是尺寸指数:

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

Should be:

if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){

#2


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语句的第一部分将回显查询中的错误(如果有的话)