samedi 9 mai 2015

Problems when images need to upload

Good Day to all

I have a problem with my php and mysql. Everything worked fine till I added new things to my database and now my images give me errors when I click on create.

Below is the errors I am getting:

Notice: Undefined variable: conn in C:\wamp\www\SA-Property Finder\create.php on line 140

Fatal error: Call to a member function lastInsertId() on a non-object in C:\wamp\www\SA-Property Finder\create.php on line 140

And here is my code:

//START IMAGES

        // insert data
        if ($valid) {
            $pdo = Database::connect();


            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO properties (title,description,area,map,status,type,bedrooms,bathrooms,parking,garage,garden,pool,price,pets,agentnum,agentemail) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($title,$description,$area,$map,$status,$type,$bedrooms,$bathrooms,$parking,$garage,$garden,$pool,$price,$pets,$agentnum,$agentemail));
            Database::disconnect();

        }
       **$last_id = $pdo->lastInsertId();**
       $directory="uploads/";
       $directoryagents="agents/";
$a=0;

foreach ($_FILES['agentfile']['name'] as $nameFile) {
            $ext = pathinfo($nameFile, PATHINFO_EXTENSION);

            if(is_uploaded_file($_FILES['agentfile']['tmp_name'][$a]))
            {
                move_uploaded_file($_FILES['agentfile']['tmp_name'][$a], $directoryagents.$last_id."_".$a.".".$ext);
            }
            $fileandpath = $directoryagents.$last_id."_".$a.".".$ext;

            $pdo1 = Database::connect();
            $pdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql1 = "UPDATE properties SET agentimageurl=? WHERE PropertyId=?";
            $q1 = $pdo1->prepare($sql1);
            $q1->execute(array($fileandpath,$last_id));
            Database::disconnect();
        }


foreach ($_FILES['file']['name'] as $nameFile) {


        $ext = pathinfo($nameFile, PATHINFO_EXTENSION);
            if(is_uploaded_file($_FILES['file']['tmp_name'][$a]))
            {
                move_uploaded_file($_FILES['file']['tmp_name'][$a], $directory.$last_id."_".$a.".".$ext);
            }
            $fileandpath = $directory.$last_id."_".$a.".".$ext;

            $pdo1 = Database::connect();
            $pdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql1 = "INSERT INTO images (propertyid,imageurl) values(?, ?)";
            $q1 = $pdo1->prepare($sql1);
            $q1->execute(array( $last_id,$fileandpath));
            Database::disconnect();
            $a++;

        }
          header("Location: admin_list.php");
      }

This is line 140:

   $last_id = $pdo->lastInsertId();

I just don't understand it worked fine, can't seem to see where my problem is.

Aucun commentaire:

Enregistrer un commentaire