Tony33 Tony33 - 2 months ago 10
PHP Question

SQLSTATE[HY093]

This is my code:

if (!isset($_POST['cellulare'])) $cell = "-"; else $cell = $_POST['cellulare'];
if (!isset($_POST['ufficio'])) $uff = "-"; else $uff = $_POST['ufficio'];
if (!isset($_POST['email'])) $email = "-"; else $email= $_POST['email'];

$sqlup = "UPDATE rubrica SET nome = :nm,
cognome = :cgm,
mail = :email,
cellulare = :cell,
ufficio = :office,
WHERE id=".$_GET['mod'];
$stmt = $pdo->prepare($sqlup);
$stmt->bindParam(':nm', $_POST['nome'], PDO::PARAM_STR);
$stmt->bindParam(':cgm', $_POST['cognome'], PDO::PARAM_STR);
$stmt->bindParam(':email ', $email, PDO::PARAM_STR);
$stmt->bindParam(':cell', $cell, PDO::PARAM_STR);
$stmt->bindParam(':office', $uff, PDO::PARAM_STR);
$stmt->execute();


Apparently there is no error, but i get this:


Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'

Answer

Are you sure $_GET['mod'] is not empty and contains an integer? If yes, then there is another error in your SQL - you have there , before WHERE:

$sqlup = "UPDATE rubrica SET nome = :nm, 
        cognome = :cgm, 
        mail = :email,  
        cellulare = :cell,  
        ufficio = :office,  /* <== here */
        WHERE id=".$_GET['mod'];

Change it to this:

$sqlup = "UPDATE rubrica SET nome = :nm, 
        cognome = :cgm, 
        mail = :email,  
        cellulare = :cell,  
        ufficio = :office
        WHERE id=".$_GET['mod'];

Also there is error in your binding email, there is extra space as @chris85 mentioned.

$stmt->bindParam(':email', $email, PDO::PARAM_STR);
Comments