-2

My goal in this code is to create apply later section for the user and I created an hidden button to get the id of the exact post, i am getting the id but when i bind and execute i am having this warning, i have checked several other peoples question similar to mine but they do not seem to solve my exact problem. What exactly am I to bind to get the info of the post with that specific ID

Here is the function:

public function Index(){

    if(isset($_POST['submit'])){
        $id = $_POST['applylater_id'];// I got this from a button the user is to click
        // INSERT INTO MYSQL
        $this->query('INSERT INTO apply_later(id, title, body, link, user_id, user_name) SELECT id, title, body, link, user_id, user_name FROM shares WHERE id = :id');
        $this->bind(':title', 'title');
        $this->bind(':body', 'body');
        $this->bind(':link', 'link');
        $this->bind(':user_id', 1);
        $this->bind(':user_name', 'user_name');         
        $this->bind(':id', $id);
        $this->execute();

        //Verify
        if($this->lastInsertId()){
            //Redireect
            Messages::setMsg('Successfully Added to your apply Later section', 'successMsg'); 
            header( "refresh:2; url=http://localhost/phpsandbox/phptutorials/website2/shares/applylater" );

        }
    }
    //
    $this->query('SELECT * FROM shares ORDER by id desc ');
    $rows = $this->resultSet();
    return $rows;
}

//This are my bind and execute functions that I call above

public function query($query){
    $this->stmt = $this->dbh->prepare($query);
}


public function bind ($param, $value, $type =null) {
if(is_null($type)){
    switch(true) {
        case is_int($value):
            $type = PDO::PARAM_INT;
            break;
        case is_bool($value):
            $type = PDO::PARAM_BOOL;
            break;
        case is_null($value):
            $type = PDO::PARAM_NULL;
            break;
            default:
            $type = PDO::PARAM_STR;
    }

        }
    $this->stmt->bindValue($param, $value, $type);
}

public function execute(){
    return $this->stmt->execute();
}

Now this is the main form that i use but it can get the id but doesnt display it on the screen

<?php foreach ($viewmodel as $item) : ?>
    <div class="well">
        <div class="text-center">
            <img src="http://localhost/phpsandbox/phptutorials/website2/assets/image/job.png" class="img-circle" alt="Cinque Terre" width="50" height="50">

        <h3> <?php echo $item['title']; ?></h3>
        <small> Username: <?php echo $item['user_name']; ?></small>
        <br />
        <small><?php echo $item['create_date']; ?></small>
        </div>
        <hr />
        <p><?php echo $item['body']; ?></p>
        <br />
        <div class="text-center">
            <a href="<?php echo $item['link']; ?>" target="_blank"  class="btn btn-info">Apply Now</a>
            <br />

        </div>
        <hr />
        <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" >

// When you click submit I want to get the id of this exact post and send the data into a different table so i can display this exact one in a different page.

                <input type="hidden" name="applylater_id" value="<?php echo $item['id']; ?>">
                <input class="btn btn-primary" type="submit" name="submit" value="Apply Later">
        </form>

Here are my tables

    </div>[enter image description here][1]
rotimi-best
  • 1,852
  • 18
  • 29
  • 1
    Do you understand what this code is doing? The error message is pretty clear... You are trying to bind 6 parameters and you only have 1 spot. – Devon Bessemer Oct 23 '17 at 04:21
  • 1
    Possible duplicate of [SQLSTATE\[HY093\]: Invalid parameter number: number of bound variables does not match number of tokens on line 102](https://stackoverflow.com/questions/20585535/sqlstatehy093-invalid-parameter-number-number-of-bound-variables-does-not-ma) – Devon Bessemer Oct 23 '17 at 04:21
  • So what do I do, I am sorry but I am just learning php and it might not be clear to me. Thanks @Devon – rotimi-best Oct 23 '17 at 04:24
  • Well, why are you binding all these parameters? Don't just copy and paste code, know what you are writing so you can actually learn from it... – Devon Bessemer Oct 23 '17 at 04:26
  • i am trying to bind that exact post with the exact id and then send it into a different table. If there is a better way please I would love to know thats why I asked the question – rotimi-best Oct 23 '17 at 04:29
  • Please What exactly am I to bind to get the info of the post with that specific ID @Devon – rotimi-best Oct 23 '17 at 05:51

1 Answers1

1

Only bind values to the prepared statement where you have a placeholder.

$this->query('INSERT INTO apply_later(id, title, body, link, user_id, user_name)
              SELECT id, title, body, link, user_id, user_name FROM shares WHERE id = :id');
//                       only 1 placeholder-------------------------------------------^^^

Remove these lines because they are not represented in the prepared statement:
$this->bind(':title', 'title');
$this->bind(':body', 'body');
$this->bind(':link', 'link');
$this->bind(':user_id', 1);
$this->bind(':user_name', 'user_name');

Just bind :id:

$this->bind(':id', $_POST['applylater_id']);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136