Pdo - 将值插入数据库错误 SQLSTATE [HY093] [重复]

Posted

技术标签:

【中文标题】Pdo - 将值插入数据库错误 SQLSTATE [HY093] [重复]【英文标题】:Pdo - insert values to db erorr SQLSTATE[HY093] [duplicate] 【发布时间】:2017-07-15 00:44:53 【问题描述】:

嗨,兄弟,这段代码有什么错误,请告诉我这个错误

致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY093]:无效的参数号:绑定变量的数量与数量不匹配

<?php
   if(isset($_POST['addfile']))
     

      $filename = $_POST['file_name'];// user name
      $filecat = $_POST['file_cat'];// user email
      $fileinfo = $_POST['file_info'];// user email
      $imgFile = $_FILES['file_photo']['name'];
      $tmp_dir = $_FILES['file_photo']['tmp_name'];
      $imgSize = $_FILES['file_photo']['size'];
      $urlFile = $_FILES['file_url']['name'];
      $tmp_dir = $_FILES['file_url']['tmp_name'];
      $urlSize = $_FILES['file_url']['size'];

      if(empty($filename))
       $errMSG = "Please Enter file name.";
      
      else if(empty($fileinfo))
       $errMSG = "Please Enter file info.";
      
      else if(empty($urlFile))
       $errMSG = "Please Select File.";
      
      else if(empty($imgFile))
       $errMSG = "Please Select Image File.";
      
      else
      
       $upload_dir = 'share-admin/images/'; // upload directory

       $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

       // valid image extensions
       $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions

       // rename uploading image
       $imgFile1 = rand(1000,1000000).".".$imgExt;

       // allow valid image file formats
       if(in_array($imgExt, $valid_extensions))   
        // Check file size '5MB'
        if($imgSize < 5000000)    
         move_uploaded_file($tmp_dir,$upload_dir.$imgFile1);
        
        else
         $errMSG = "Sorry, your file is too large.";
        
       
       else
        $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";  
       

        $upload_dir = 'share-admin/files/'; // upload directory

       $urlExt1 = strtolower(pathinfo($urlFile,PATHINFO_EXTENSION)); // get image extension

       // valid image extensions
       $valid_extensions = array('zip', 'rar', 'pdf', 'doc'); // valid extensions

       // rename uploading image
       $urlFile1 = rand(1000,1000000).".".$urlExt1;

       // allow valid image file formats
       if(in_array($urlExt1, $valid_extensions))   
        // Check file size '5MB'
        if($urlSize < 5000000)    
         move_uploaded_file($tmp_dir,$upload_dir.$urlFile1);
        
        else
         $errMSG = "Sorry, your file is too large.";
        
       
       else
        $errMSG = "Sorry, only zip, rar, pdf & doc files are allowed.";  
       
      


      // if no error occured, continue ....
      if(!isset($errMSG))
      
          $db=getDB();
       $stmt = $db->prepare('INSERT INTO share_files(file_name,file_photo,file_url,file_date,file_cat,file_size,file_info) 
       VALUES(:filename,:imgFile1,:urlFile1,NOW(),:filecat,:$filesize,:fileinfo)');
       $stmt->bindParam(':filename',$filename);
       $stmt->bindParam(':filecat',$filecat);
       $stmt->bindParam(':fileinfo',$fileinfo);
       $stmt->bindParam(':imgFile1',$imgFile1);
       $stmt->bindParam(':urlFile1',$urlFile1);
       $stmt->bindParam(':$filesize',$urlSize);


       if($stmt->execute())
       
        $successMSG = "new record succesfully inserted ...";
        header("refresh:5;addproject.php"); // redirects image view page after 5 seconds.
       
       else
       
        $errMSG = "error while inserting....";
       
      
     

?>

【问题讨论】:

$ 不是命名占位符的有效字符。 【参考方案1】:

该错误通常表明 INSERT 语句中存在错误 - 字段数与要插入的值数不匹配。

INSERT 语句中使用的 $filesize 参数看起来不正确。不确定这是正确的语法。 $filesize 是否在代码的其他地方有一个值,而您的示例中没有显示?

【讨论】:

以上是关于Pdo - 将值插入数据库错误 SQLSTATE [HY093] [重复]的主要内容,如果未能解决你的问题,请参考以下文章

PDO 异常 SQLSTATE [23000]:在 php 中

PDO 准备语句中的 SQLSTATE[22018] 错误

PDO 错误:“SQLSTATE[HY000]: General error” 更新数据库时

PDO 错误:SQLSTATE [HY000]:一般错误:2031

PHP/PDO 错误:SQLSTATE[HY000] 无法连接:Adaptive Server 不可用或不存在(严重性 9)

为什么PDO的Oracle驱动程序不实现lastInsertId()?