可捕获的致命错误:类mysqli_result的对象无法在upload.php中转换为字符串

时间:2016-03-21 14:10:54

标签: php mysql

我试图将fileDetailsID这是mysql数据库FileDetails表中的主键放入文件中。当一个用户上传时,文件的详细信息将相互附加。

如果我将'$_SESSION[login_user]'放入$fsql的值中,我会获得当前用户的userID,因此我可以假设上传到数据库的工作正常。

我已就此网站上的先前问题尝试过很多建议,但没有任何效果。虽然我可能做错了,因为我是pvp的新手。

我想我很亲近, 这就是我必须获得我想要的主键

  $fses_sql = mysqli_query($conn,"SELECT fileDetailID FROM FileDetails WHERE userID = '$_SESSION[login_user]' ");

这是传递给

的内容
 $fsql = "INSERT INTO files (fileDetailID) VALUES ('$fses_sql')";

        if (mysqli_query($conn, $fsql)) {
        $info = "file uploaded Successfully!";
      } else {
        $info ="Unable to Add file!";
      }

但我收到此错误

  

可捕获的致命错误:类mysqli_result的对象不可能   转换为/home2/nathanha/public_html/Nhall/upload.php上的字符串   第87行

这是我的upload.php代码

             <?php
 include('config.php');
 include('session.php');

 function test_input($data) {
 $data = trim($data);
 $data = stripslashes($data);
 $data = htmlspecialchars($data);
 return $data;
} 
  $fses_sql = mysqli_query($conn,"SELECT fileDetailID FROM FileDetails WHERE userID = '$_SESSION[login_user]' ")
 $upload1Ok = 1;
 $target_dir = "uploads/";
 $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
 $uploadOk = 1;
 $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
 // Check if file already exists
 if (file_exists($target_file)) {
 echo "Sorry, file already exists.";
 $uploadOk = 0;
 }
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if ($imageFileType != "zip") {
echo "Sorry, only zip files are allowed.";
$uploadOk = 0;
}
// file details

 if($_SERVER["REQUEST_METHOD"] == "POST") {

  if (empty($_POST["txtfileType"])) {
    $fileTypeErr = "file type required";
      $upload1Ok = 0;
  } 
  else {
    $fileType = mysqli_real_escape_string($conn,$_POST["txtfileType"]);
  }
  if (empty($_POST["txtfileSize"])) {
    $fileSizeErr = "fileSize required";
      $upload1Ok = 0;
  }  
  else {
    $fileSize = mysqli_real_escape_string($conn,$_POST["txtfileSize"]);
  }
  if (empty($_POST["txtdateCompleted"])) {
    $dateCompletedErr = "Date completed required";
      $upload1Ok = 0;
  }  
  else {
 $dateCompleted = mysqli_real_escape_string($conn,$_POST["txtdateCompleted"]);
  }
  if (empty($_POST["txtnameOfFile"])) {
    $nameOfFileErr = "Name of file required";
      $upload1Ok = 0;
  }  
  else {
    $nameOfFile = mysqli_real_escape_string($conn,$_POST["txtnameOfFile"]);
  }
  // Check if $uploadOk and $upload1OK is set to 0 by an error
  if ($uploadOk == 1 || $upload1OK == 1) {

  // if everything is ok, try to upload file
  $sql = "INSERT INTO FileDetails (fileType, fileSize, dateCompleted, nameOfFile, userID )
        VALUES('$fileType', '$fileSize', '$dateCompleted', '$nameOfFile','$_SESSION[login_user]')";

      if (mysqli_query($conn, $sql)) {

        $info = "User Created Successfully!";
      } else {
        $info ="Unable to Add User!";
      }
   // insert file details primary key into files table
   $fsql = "INSERT INTO files (fileDetailID) VALUES ('$fses_sql')";

        if (mysqli_query($conn, $fsql)) {
        $info = "file uploaded Successfully!";
      } else {
        $info ="Unable to Add file!";
      }


    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } 
        }
   else {
      $info ="All Feilds must be completed correctly!";
    }  
   }
   ?>
  <!DOCTYPE html>
  <html>
   <head>
  <title>Login Page</title> 
  <style type = "text/css">
     body {
        font-family:Arial, Helvetica, sans-serif;
        font-size:14px;
     }

     label {
        font-weight:bold;
        width:100px;
        font-size:14px;
     }

     .box {
        margin:0 0 10 0;
        border:#666666 solid 1px;
     }

     .error {
        color: #FF0000;
     }
  </style>  
   </head> 
   <body bgcolor = "#FFFFFF">

  <div align = "center">
     <div style = "width:450px; border: solid 1px #333333; " align = "left">
        <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Register User</b></div>

        <div style = "margin:30px">  
           <form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method = "post" enctype="multipart/form-data">

              <label>File Type  :</label><input type = "text" name = "txtfileType" class = "box" value="<?php echo $fileType;?>"/><span class="error">* <?php echo $fileTypeErr;?></span>
 <br />                
              <label>File Size  :</label><input type = "text" name = "txtfileSize" value="<?php echo $fileSize;?>" class = "box"/><span class="error">* <?php echo $fileSizeErr;?></span>
<br />
              <label>Date Completed  :</label><input type = "text" name = "txtdateCompleted" value="<?php echo $dateCompleted;?>" class = "box"/><span class="error">* <?php echo $dateCompletedErr;?></span>
<br />
               <label>Name Of File  :</label><input type = "text" name = "txtnameOfFile" value="<?php echo $nameOfFile;?>" class = "box"/><span class="error">* <?php echo $nameOfFileErr;?></span>
    <br />                  
    <form action="upload.php" method="post" enctype="multipart/form-data">
    Select file to upload:
   <input type="file" name="fileToUpload" id="fileToUpload">
   <input type = "submit" value = " Submit "/><br />
   <h2><a href="dashboard.php">Back to dashboard</a></h2>
     </form>
        </form>
    <h2><a href="login.php">Login User</a></h2>
             <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $info; ?></div>

        </div>

       </div>

      </div>

     </body>
   </html>

我已更改此内容并将输入更改为$ ressult1

$fses_sql = mysqli_query($conn,"SELECT fileDetailID FROM FileDetails WHERE userID = '$_SESSION[login_user]'[, string $ressult1 = MYSQLI_STORE_RESULT ] ");

错误已经消失,但我想填充的字段现在没有任何内容

0 个答案:

没有答案