Dillinger
Dillinger

Reputation: 1903

Query doesn't insert value into DB

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?

Upvotes: 1

Views: 59

Answers (3)

Alok Kumar Mishra
Alok Kumar Mishra

Reputation: 169

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();      }

}?

Upvotes: 2

Patel Kishan
Patel Kishan

Reputation: 65

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

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

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 = ?";

Upvotes: 0

Related Questions