2017-05-10 3 views
0

Ich habe für diese Linie in meiner connect Datei diesen Fehler:

$ statement -> Ausführen ($ params);

Hier ist meine connect-Datei (btw ich weiß, was meine Anmeldeinformationen Ich habe es einfach nicht zeigen wollen),

<?php 
class connect 
{ 

    private static function db() 
    { 
     $pdo = new PDO('mysql:host=127.0.0.1;dbname=mysql;charset = utf8', 'username','password'); 
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     return $pdo; 
    } 
    public static function query($query,$params = array()) 
    { 

     $statement = self :: db()->prepare($query); 
     $statement->execute($params); 
     if(explode(' ',$query)[0] == 'SELECT') 
     { 
      $data = $statement->fetchAll(); 
      return $data; 
     } 
    } 
} 

?> 

jedoch das Problem aus diesem Aufruf stammt,

connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0, \'\')', array(':postimg'=>$postimg)); 

Das ist von dieser Datei, wo ich Bilder über die Imgur-API hochlade und anzeige (das ist nicht die vollständige Datei).

<?php 
include("connect.php"); 
include("check.php"); 
//include("image.php"); 

$postbody = ""; 
$posts = ""; 
$postimg = ""; 


       if (isset($_POST['post'])) { 

         if ($_FILES['postimg']['size'] == 0) 
         { 
           $postbody = $_POST['postbody']; 
           $loggedInUserId = check::isLoggedIn(); 
           if (strlen($postbody) > 160 || strlen($postbody) < 1) 
           { 
            die('Incorrect length!'); 
           } 

           connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0,:postimg)', array(':postbody'=>$postbody)); 

           // Post::createPost($_POST['postbody']); 

         } 
         else { 

          $url  = 'https://api.imgur.com/3/image'; // API endpoints, info: https://api.imgur.com/endpoints/image#image-upload 
        $client_id = '72316d91aac85c7'; // Get client_id here: https://api.imgur.com/#registerapp 

        $fh = fopen($_FILES['postimg']['tmp_name'], 'r'); 
        $read = fread($fh, $_FILES['postimg']['size']); 
        fclose($fh); 
        $post = array(
           'image' => base64_encode($read) 
          ); 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
        curl_setopt($ch, CURLOPT_POST, true); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                  'Authorization: Client-ID '.$client_id 
                 )); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        $json = curl_exec($ch); // Response, info: https://api.imgur.com/#responses 
        curl_close($ch); 
        // $image = json_decode($json, true); // Array 
        $image = json_decode($json); // Object 
        // var_dump($image); 
        //$postimg = $image['data']['link']; // Array-style 
        $postimg = $image->data->link; // Object-style 


            // Insert $postimg to database? 
            //connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0, \'\')', array(':postimg'=>$postimg)); 
        connect::query('INSERT INTO dry_posts VALUES (null, :postbody, NOW(), 0, :postimg)', array(':postimg'=>$postimg)); 
            //connect::query("UPDATE posts SET postimg=:postimg WHERE id=:postid", array(':postid' => $postid, ':postimg' => $postimg)); 
          } 


         } 

       if (isset($_GET['postid'])) 
       { 
         //Post::likePost($_GET['postid']); 

        if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid']))) 
        { 
          connect::query('UPDATE dry_posts SET likes=likes+1 WHERE id=:postid', array(':postid'=>$_GET['postid'])); 
          connect::query('INSERT INTO post_likes VALUES (null, :postid)', array(':postid'=>$_GET['postid'])); 
        } 
        else 
        { 
          connect::query('UPDATE dry_posts SET likes=likes-1 WHERE id=:postid', array(':postid'=>$_GET['postid'])); 
          connect::query('DELETE FROM post_likes WHERE post_id=:postid', array(':postid'=>$_GET['postid'])); 
        } 

        //Post::likePost($_GET['postid']); 
       } 

       // $posts = Post::displayPosts(); 

     //title is body or whereclause, search_results is posts or $paramsarray, results is paramsarray 


       $dbposts = connect::query('SELECT * FROM dry_posts ORDER BY id DESC'); 
       $posts = ""; 
      //if(isset($_POST['postimg'])){ 
       foreach($dbposts as $p){ 
         if (!connect::query('SELECT post_id FROM post_likes WHERE post_id=:postid', array(':postid'=>$p['id']))) { 
           $posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])." 
           <form action='try.php?postid=".$p['id']."' method='post'> 
             <input type='submit' name='like' value='Like'> 
             <span>".$p['likes']." likes</span> 
           </form> 
           <hr /></br /> 
           "; 

         } else { 
           $posts .="<img src='".$p['postimg']."'>".htmlspecialchars($p['body'])." 
           <form action='try.php?postid=".$p['id']."' method='post'> 
             <input type='submit' name='unlike' value='Unlike'> 
             <span>".$p['likes']." likes</span> 
           </form> 
           <hr /></br /> 
           "; 
         } 
       } 


?> 

connect::query('SELECT body FROM dry_posts WHERE body LIKE :body '.$whereclause.'', $paramsarray); 

Ich bin sicher, es ist nichts falsch mit der Art, wie ich Parameter in meiner connect Datei bin zu akzeptieren, da es funktioniert, wenn ich meinen $ postbody Array übergeben, so ist es etwas falsch mit der Art und Weise postimg definiert ist, was Vermisse ich? postimg ist eine Eingangsgröße von dieser Form

<form action="try.php" class = "forum" method="post" enctype="multipart/form-data"> 
<textarea name="postbody" rows="4" cols="60" class = "text"></textarea> 
     <br />Upload an image: 
     <input type="file" name="postimg"> 
     <input type="submit" name="post" value="Post"> 
     <input type="submit" value="Upload"> 
</form> 

Antwort

1
connect::query(' 
    INSERT 
    INTO dry_posts 
    VALUES (
     null, 
     :postbody, <-- you name your placeholder :postbody 
     NOW(), 
     0, 
     \'\' 
    ) 
', array(
    ':postimg' => $postimg <-- you're trying to fill a placeholder named :postimg 
)); 

Ändern Sie Ihre Abfrage :postimg als Platzhalter zu verwenden, oder Ihr Array ändern, um zu versuchen, die :postbody Platzhalter zu füllen.

+0

Danke, dass es funktioniert, aber ich habe ein Problem beim Speichern in der Postimg Spalte in meiner Mysql Tabelle Dry_posts es wird in der Postbody-Spalte gespeichert, stattdessen habe ich versucht mit diesem Aufruf: connect :: query ('INSERT INTO dry_posts VALUES (null,: postbody, NOW(), 0,: postimg) ', array (': postimg '=> $ postimg)); aber die Postimg-Spalte wird standardmäßig als Null eingerichtet, wenn ich versuche, zu ändern, bekomme ich diesen Fehler: Abfragefehler: # 1265 - Daten abgeschnitten für Spalte 'postimg' in Zeile 1, irgendwelche SQL-Anweisungen, um dies weg zu machen, sorry Für alle zusätzlichen – Bubba

+0

ist der letzte Wert in der INSERT-Anweisung für postimg – Bubba

+0

Also wäre es nicht 'VALUES (null, \ '\', NOW(), 0,: postimg)' dann? – rickdenhaan

Verwandte Themen