上传图像时参数号无效

时间:2016-09-27 16:40:38

标签: php mysql pdo

我正在尝试创建一个允许用户输入信息的表单,然后上传图像以获取该信息。输入所有信息并附上图像后,如果按下提交按钮,我会收到此错误:

  

SQLSTATE [HY093]:参数号无效:参数未定义。

然而,无论如何都会上传图像,并且实际上没有任何内容插入到数据库中

在提交之前,我也收到此错误:

  

注意:未定义索引:图像输入   第5行的E:\ xampp \ htdocs \ authTest \ adminNewProperty.php

这是表格的代码:(adminNewProperty.php)

<?php
require_once 'dbconfig.php';
//var_dump($_FILES);
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

//Checks if the create new activity button was clicked, if it was, the entered data will be used to create a new activity 
if(isset($_POST['button_CreateProperty']))
{
    $check                              = getimagesize($_FILES["image"]["tmp_name"]);
    $property_title                 = $_POST['title'];
    $property_address               = $_POST['address'];
    $property_description       = $_POST['description'];
    $property_rent                  = $_POST['rent'];
    $property_reference         = $_POST['reference'];
    $property_image             = $_FILES['image']['name'];

   //Error handling
   //Checks if the activity name is empty, if it is, produce an error
   if(strlen($property_title)=="") 
   {
        $error[] = "Please enter a title for this property"; 
   }

   //Checks if the activity date is empty, if it is, produce an error
   if($property_address=="") 
   {
        $error[] = "Please enter the address for this property"; 
   } 
   //Checks if the activity start time is empty, if it is, throw an error
   if($property_description=="") 
   {
        $error[] = "Please enter a description of this property";
   } 

   //Checks if the activity end time is empty, if it is, throw an error
   if($property_rent=="")
   {
        $error[] = "Please enter a description of the property";
   } 

   //Checks if the activity description is empty
   if($property_reference=="") 
   {
        $error[] = "Please enter a reference for this property";
   }

         //IMAGE UPLOAD
    if($check !== false) 
    {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } 
    else 
    {
        echo "File is not an image.";
        $uploadOk = 0;
    }


// Check if file already exists
if (file_exists($target_file)) 
{
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 900000) 
{
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"&& $imageFileType != "gif" ) 
{
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) 
{
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else 
{
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
    } 
    else
    {
        echo "Sorry, there was an error uploading your file.";
    }
}

   if (empty($error))
   {
      try
      {
         $stmt = $DB_con->prepare("SELECT * FROM property WHERE property_reference=:reference");
         $stmt->execute(array(':reference'=>$property_reference));
         $row=$stmt->fetch(PDO::FETCH_ASSOC);

         if($row['reference']==$property_reference) 
         {
            $error[] = "There is already a property with this reference!";
         }
         else
         {
             //if all entered data successfully validates, the property is created and the user is taken to the index
            if(true === $property->new_property($property_title, $property_address, $property_description, $property_rent, $property_reference, $property_image)) 
            {
                $user->redirect('properties.php');
            }
         }
     }
     catch(PDOException $e)
     {
        $error[] = $e->getMessage();
     }
  } 
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Guardian</title>
    <!--Bootstrap 4 CSS-->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/css/bootstrap.min.css" integrity="sha384-MIwDKRSSImVFAZCVLtU0LMDdON6KVCrZHyVQQj6e8wIEJkW4tvwqXrbMIya1vriY" crossorigin="anonymous">
    <!--Bootstrap 4 JS-->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/js/bootstrap.min.js" integrity="sha384-ux8v3A6CPtOTqOzMKiuo3d/DomGaaClxFYdCu2HPMBEkf6x2xiDyJ7gkXU0MWwaD" crossorigin="anonymous">  </script>
    <!--Custom Styling-->
    <link rel="stylesheet" href="css/style.css">
 </head>

<body>

<div class="container">
<h1> New Property Listing </h1>
<?php echo implode('</br>', (array)$error) ?>
<form method="POST" enctype="multipart/form-data">
  <div class="form-group">
    <label for="name">Property Title:</label>
    <input type="text" class="form-control" id="title" name="title" placeholder="Title of the Property" value="<?php if(isset($error)){echo $property_title;}?>">
  </div>
  <div class="form-group">
    <label for="rent">Property Address:</label>
    <input type="text" class="form-control" id="address" name="address" placeholder="Address for the property" value="<?php if(isset($error)){echo $property_address;}?>">
  </div>
  <div class="form-group">
    <label for="address">Property Description:</label>
    <input type="text" class="form-control" id="description" name="description" placeholder="Description of the property" value="<?php if(isset($error)){echo $property_description;}?>">
  </div>
  <div class="form-group">
    <label for="reference">Property Rent:</label>
    <input type="text" class="form-control" id="rent" name="rent" placeholder="Monthly rent for this property" value="<?php if(isset($error)){echo $property_rent;}?>">
  </div>
  <div class="form-group">
    <label for="reference">Property Reference:</label>
    <input type="text" class="form-control" id="reference" name="reference" placeholder="Unique identifier for this property" value="<?php if(isset($error)){echo $property_reference;}?>">
  </div>
  <div class="form-group">
    <label for="image">Upload an Image:</label>
    <input type="file" id="image" name="image" value="Upload Image">
    <p class="help-block">Upload an image to be used with this property listing</p>
  </div>
  <button type="submit" class="btn btn-default" name="button_CreateProperty">Submit</button>
</form>
</div>

</body>
</html>

另外,这是我正在使用的课程:

   <?php
    class Property
    {

        private $db;
        private $error;

        //Create property class on execute, using database connection (DB_Con) in dbconfig.php
        function __construct($DB_con)
        {
          $this->db = $DB_con;
        }

        public function new_property($property_title, $property_address, $property_description, $property_rent, $property_reference, $property_image)
        {
           try
           {
               //PDO statement that uses 'this' (the current user) to create a new user based on entered values
               $stmt = $this->db->prepare("INSERT INTO property (property_title, property_address, property_description, property_rent, property_reference, property_image) 
                                                        VALUES(:title, :address, :description, :rent, :reference, :name)");

               $stmt->bindparam     (":title"   , $property_title);
               $stmt->bindparam     (":address" , $property_address);   
               $stmt->bindparam     (":description" , $property_description);  
               $stmt->bindparam     (":rent"    , $property_rent);  
               $stmt->bindparam     (":reference"   , $property_reference);  
               $stmt->bindparam     (":image"   , $property_image);  
               $stmt->execute(); 
               return $stmt; 
           }
           catch(PDOException $e)
           {
               echo $e->getMessage();
           }    
        }
    }

谢谢!

0 个答案:

没有答案