1

In my query the update statement doesn't work, the error given is:

Number of parameter doesn't match with prepared statement

this is my code:

public function update_resource($resource)
{
    $mysqli = new MySQLi(HOST, USERNAME, PASSWORD, DATABASE);

    $this->connection_state($mysqli); 

    $id = $resource['id'];
    $descrizione = $resource['descrizione'];
    $sigla =       $resource['sigla'];
    $colore =      $resource['colore'];
    $planning =    $resource['planning'];

    try
    {   
        $query =    "UPDATE risorse SET descrizione = '$descrizione'
                        AND sigla = '$sigla' AND colore = '$colore' AND planning = '$planning'
                        WHERE id = '$id' ";

        $stmt = $mysqli->prepare($query);
        $stmt -> bind_param("ssssi", $descrizione, $sigla, $colore, $planning, $id);
        echo $query;

        if($stmt->execute())
        {
            echo "Added!";
        }
        else
        {
            echo "Err: " . $stmt->error;    
        }

    }catch(Exception $e){   echo $e->getMessage();      }

}

The code go into the Added condition but the query fail, what's the problem?

1
  • It should be $query = "UPDATE risorse SET descrizione =? , sigla = ?, colore =? , planning =? WHERE id = ?"; Commented Oct 8, 2015 at 7:36

3 Answers 3

2
public function update_resource($resource)
{
    $mysqli = new mysqli(HOST, USERNAME, PASSWORD, DATABASE);

    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    $id = $resource['id'];
    $descrizione = $resource['descrizione'];
    $sigla =       $resource['sigla'];
    $colore =      $resource['colore'];
    $planning =    $resource['planning'];

    try
    {   
        $query =    "UPDATE risorse SET descrizione = '$descrizione'
                        , sigla = '$sigla', colore = '$colore', planning = '$planning'
                        WHERE id = '$id' ";

        $stmt = $mysqli->prepare($query);
        $stmt -> bind_param($descrizione, $sigla, $colore, $planning, $id);
        echo $query;

        if($stmt->execute())
        {
            echo "Added!";
        }
        else
        {
            echo "Err: " . $stmt->error;    
        }

    }catch(Exception $e){   echo $e->getMessage();      }

}?
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is that you don't have any placeholders in your query. Refer to manual to see how placeholders should be set.

In general, placeholders are ? which later will be replaced with values, so your query should look like:

$query = "UPDATE risorse SET descrizione = ?
                    AND sigla = ? AND colore = ? AND planning = ?
                    WHERE id = ?";

1 Comment

I'm bit rusty on this now, could you show me please?
0

please visit on http://php.net/manual/en/pdostatement.bindparam.php.you got your answer.see Example #1 Execute a prepared statement with named placeholders

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.