带消息'SQLSTATE [HY093]的PDOException'

时间:2015-06-17 11:13:36

标签: php mysql pdo

错误:

  消息'SQLSTATE [HY093]的

PDOException':参数号无效:绑定变量数与令牌数不匹配

<?php

require '../database.php';

$id = null;
if ( !empty($_GET['id'])) {
    $id = $_REQUEST['id'];
}

if ( null==$id ) {
    header("Location: index.php");
}



//
//echo "<pre>";
//echo print_r($_POST);
//echo "</pre>";
//
//die();


if ( !empty($_POST)) {
    // keep track validation errors
//    $pr_id_vinError = null;
    $pr_nomError = null;
    $pr_soustitreError = null;
    $pr_enligneError = null;
    $pr_millesimeError = null;
    $pr_descriptionError = null;
    $pr_typeError = null;
    $pr_prixError = null;
    $pr_ordreError = null;


    // keep track post values
    $pr_nom = $_POST['pr_nom'];
    $pr_id_vin = $_POST['pr_id_vin'];
    $pr_soustitre = $_POST['pr_soustitre'];
    $pr_enligne = $_POST['pr_enligne'];
    $pr_millesime = $_POST['pr_millesime'];
    $pr_description = $_POST['pr_description'];
    $pr_type = $_POST['pr_type'];
    $pr_prix = $_POST['pr_prix'];
    $pr_ordre = $_POST['pr_ordre'];
    $pr_couleur = $_POST['pr_couleur'];


    // validate input
    $valid = true;

//    if(empty($pr_id_vin)) {
//        $pr_id_vinError =  'Vieille selectione le nom du vin';
//        $valid = false;
//    }

    if (empty($pr_nom)) {
        $pa_nomError = 'Vieille entre le nom du produit';
        $valid = false;
    }

    if (empty($pr_soustitre)) {
        $pr_soustitreError = 'Vieille ente sous titre du produit';
        $valid = false;
    }

    if (empty($pr_enligne)) {
        $pr_enligneError = 'Vieille selectione  si il est en ligne ou non';
        $valid = false;
    }

    if (empty($pr_millesime)) {
        $pr_millesimeError = 'Vieille entre millesieme en chifre';
        $valid = false;
    }

    if (empty($pr_description)) {
        $pr_descriptionError = 'Vieille entre la description du vin';
        $valid = false;
    }

    if (empty($pr_type)) {
        $pr_typeError = 'Vieille entre le typ du produit';
        $valid = false;
    }

    if (empty($pr_prix)) {
        $pr_prixError = 'Vieille entre le du produit';
        $valid = false;
    }

    if (empty($pr_ordre)) {
        $pr_ordreError = 'Vieille entre ordre d\'affichage';
        $valid = false;
    }

//    var_dump($valid);die();

    // insert data
    if ($valid) {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec("set names utf8");


//        echo "<pre>";
//        echo "<br>";
//        echo print_r($_POST);
//        echo "</pre>";
//
//        die();

        $sql = "UPDATE produit  set pr_id_vin, pr_nom = ?, pr_soustitre = ?, pr_enligne =?, pr_millesime =?, pr_description =?, pr_type =?, pr_prix =?, pr_ordre =?, pr_couleur =? WHERE pr_id = ?";
        $q = $pdo->prepare($sql);
        $q->bindParam(':pr_id_vin', $pr_id_vin, PDO::PARAM_INT);
        $q->bindParam(':pr_nom', $pr_nom, PDO::PARAM_STR, 255);
        $q->bindParam(':pr_soustitre', $pr_soustitre, PDO::PARAM_STR, 255);
        $q->bindParam(':pr_enligne', $pr_enligne, PDO::PARAM_STR, 1);
        $q->bindParam(':pr_millesime', $pr_millesime, PDO::PARAM_STR, 4);
        $q->bindParam(':pr_description', $pr_description, PDO::PARAM_STR);
        $q->bindParam(':pr_type', $pr_type, PDO::PARAM_STR);
        $q->bindParam(':pr_prix', $pr_prix, PDO::PARAM_STR);
        $q->bindParam(':pr_ordre', $pr_ordre, PDO::PARAM_INT);
        $q->bindParam(':pr_couleur', $pr_couleur, PDO::PARAM_STR);
        $q->bindParam(':pr_id', $id, PDO::PARAM_INT);
        $q->execute();


//        $q->execute(array($pr_id_vin, $pr_nom,$pr_soustitre,$pr_enligne,$pr_millesime,$pr_description,$pr_type,$pr_prix,$pr_ordre,$pr_couleur, $id));
        Database::disconnect();
        header("Location: index.php");
    }
}else {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->exec("set names utf8");
    $sql = "SELECT * FROM produit where pr_id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($id));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $pr_id = $data['pr_id'];
    $pr_id_vin = isset( $data['pr_id_vin']) ? $data['pr_id_vin'] : '';   //A VOIRE DATA[]
    $pr_nom = $data['pr_nom'];
    $pr_soustitre = $data['pr_soustitre'];
    $pr_enligne = $data['pr_enligne'];
    $pr_millesime = $data['pr_millesime'];
    $pr_description = $data['pr_description'];
    $pr_type = $data['pr_type'];
    $pr_prix = $data['pr_prix'];
    $pr_ordre = $data['pr_ordre'];
    $pr_couleur = $data['pr_couleur'];
    Database::disconnect();
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="../assets/css/bootstrap.min.css" rel="stylesheet">
    <script src="../assets/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../tinymce/js/tinymce/tinymce.min.js"></script>
    <script type="text/javascript" src="../tinymce/js/tinymce/wysywig-myConfiguration.js"></script>
</head>

<body>
<div class="container">

    <div class="span10 offset1">
        <div class="row">
            <h3>Mis a jour produit</h3>
        </div>

        <form class="form-horizontal" action="update.php?id=<?php echo $pr_id?>" method="post">
            <div class="control-group <?php echo !empty($pr_id_vinError)?'error':'';?>">
                <label class="control-label">Choisissez nom du vin </label>
                <div class="controls">
                    <select class="form-control" name="pr_id_vin">
                        <?php
                        try
                        {
                            $pdo = Database::connect();
                            $pdo->exec("set names utf8");
                            $result = $pdo->query("SELECT pk_producto, nombre FROM ta_productos");
                            foreach($result as $row)
                            {
                                echo '<option value="'.$row['pk_producto'].'"';
                                if($row['pk_producto']== isset($pr_id_vin))
                                {
                                    echo ' selected';
                                }
                                echo '>'. $row['nombre'] . '</option>'."\n";
                            }
                        }
                        catch(PDOException $e)
                        {
                            echo 'Pas de resultat';
                        }
                        ?>
                    </select>
                    <?php if (!empty($pr_nomError)): ?>
                        <span class="help-inline"><?php echo $pr_id_vinError;?></span>
                    <?php endif; ?>
                </div>
            </div>
            <div class="control-group <?php echo !empty($pr_nomError)?'error':'';?>">
                <label class="control-label">Nom du produit</label>
                <div class="controls">
                    <input name="pr_nom" type="text"  placeholder="Nom du prduit" value="<?php echo !empty($pr_nom)?$pr_nom:'';?>">
                    <?php if (!empty($pr_nomError)): ?>
                        <span class="help-inline"><?php echo $pr_nomError;?></span>
                    <?php endif; ?>
                </div>
            </div>
            <div class="control-group <?php echo !empty($pr_soustitreError)?'error':'';?>">
                <label class="control-label">Sous tite du produit</label>
                <div class="controls">
                    <textarea  name="pr_soustitre"  value="<?php echo !empty($pr_soustitre)?$pr_soustitre:'';?>"><?php echo !empty($pr_soustitre)?$pr_soustitre:'';?></textarea>
                    <?php if (!empty($pr_soustitreError)): ?>
                        <span class="help-inline"><?php echo $pr_soustitreError;?></span>
                    <?php endif;?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Choisir si en ligne ou non</label>
                <div class="controls">
                    <label class="radio-inline"><input type="radio" id="enLigne" name="pr_enligne" value="Y"> En ligne</label>
                    <label class="radio-inline"><input type="radio" id="horsLigne" name="pr_enligne" value="N" checked="checked"> Hors ligne</label>
                    <?php
                    if (strpos($pr_enligne,'Y') !== false) {
                        echo "<script>";
                        echo "document.getElementById('enLigne').checked = true;";
                        echo "</script>";
                    }else {
                        echo "<script>";
                        echo "document.getElementById('horsLigne').checked = true;";
                        echo "</script>";
                    }
                    ?>
                </div>
            </div>
            <div class="control-group <?php echo !empty($pr_millesimeError)?'error':'';?>">
                <label class="control-label">Millesime</label>
                <div class="controls">
                    <input name="pr_millesime" type="number"  placeholder="Millesime du produit" value="<?php echo !empty($pr_millesime)?$pr_millesime:'';?>">
                    <?php if (!empty($pr_millesimeError)): ?>
                        <span class="help-inline"><?php echo $pr_millesimeError;?></span>
                    <?php endif;?>
                </div>
            </div>
            <div class="control-group <?php echo !empty($pr_descriptionError)?'error':'';?>">
                <label class="control-label">Description du produit</label>
                <div class="controls">
                    <textarea  name="pr_description"  value="<?php echo !empty($pr_description)?$pr_description:'';?>"><?php echo !empty($pr_description)?$pr_description:'';?></textarea>
                    <?php if (!empty($pr_descriptionError)): ?>
                        <span class="help-inline"><?php echo $pr_descriptionError;?></span>
                    <?php endif;?>
                </div>
            </div>
            <div class="control-group <?php echo !empty($pr_typeError)?'error':'';?>">
                <label class="control-label">Type du produit</label>
                <div class="controls">
                    <input name="pr_type" type="text"  placeholder="Typ du produit" value="<?php echo !empty($pr_type)?$pr_type:'';?>">
                    <?php if (!empty($pr_typeError)): ?>
                        <span class="help-inline"><?php echo $pr_typeError;?></span>
                    <?php endif;?>
                </div>
            </div>
            <div class="control-group <?php echo !empty($pr_prixError)?'error':'';?>">
                <label class="control-label">Prix</label>
                <div class="controls">
                    <input name="pr_prix" type="number" step=0.01 placeholder="Insere le prix" value="<?php echo !empty($pr_prix)?$pr_prix:'';?>">
                    <?php if (!empty($pr_prixError)): ?>
                        <span class="help-inline"><?php echo $pr_prixError;?></span>
                    <?php endif;?>
                </div>
            </div>
            <div class="control-group <?php echo !empty($pr_ordreError)?'error':'';?>">
                <label class="control-label">Ordre d'affichage</label>
                <div class="controls">

                    <?php
                    $selected = '';
                    echo '<select name="pr_ordre">',"\n";
                    for($i=0; $i<=150; $i++)
                    {
                        if($i === 1)
                        {
                            $selected = ' selected="selected"';
                        }
                        echo "\t",'<option value="', $i ,'"', $selected ,'>', $i ,'</option>',"\n";
                        $selected='';
                    }
                    echo '</select>',"\n";
                    ?>

                </div>
            </div>
            <div class="control-group">
                <label class="control-label">Couleur</label>
                <div class="controls">
                    <?php
                    $arrayCouleurs = array(

                        '#ff0000' => 'rouge',
                        '#ffffff' => 'blanc',
                        '#FD6C9E' => 'Rose'
                    );
                    $selected = '';
                    echo '<select name="pr_couleur">',"n";
                    foreach($arrayCouleurs as $valeurHexadecimale => $nomCouleur)
                    {
                        if($nomCouleur === 'rouge')
                        {
                            $selected = ' selected="selected"';
                        }
                        echo "\t",'<option value="', $nomCouleur ,'"', $selected ,'>', $nomCouleur ,'</option>',"\n";
                        $selected='';
                    }
                    echo '</select>',"\n";
                    ?>
                </div>
            </div>
            <input type="hidden" name="id" value="<?php echo isset($id) && !empty($id) ? $id : 0 ?>">
            <div class="form-actions">
                <button type="submit" class="btn btn-success">Mis a jour</button>
                <a class="btn" href="index.php">Return</a>
            </div>
        </form>
    </div>

</div> <!-- /container -->
</body>
</html>

我使用几乎相同的代码进行产品创建并且工作正常。
我正在寻找StackOverflow上的其他主题,但没有积极的结果。
我不确定发生了什么事 我认为我必须约束每一个参数 有人能说出我失踪了吗?

2 个答案:

答案 0 :(得分:4)

这里有一些问题。

首先,您错过了UPDATE produit set pr_id_vin,

的SET参数

然后,您将?:占位符类型混合在一起。

由于我无法确定您的列名是什么,我将通过以下示例让您填写这些名称:

$query = "UPDATE table SET col_1 = :col_1, col_2 = :col_2 WHERE col_x = :col_x";

参考:

旁注:正如Fluffeh可能已经思考并评论过的那样,这不是一个没有记录的功能。

自己测试后,在添加缺失的?时也遇到了同样的错误。

  • 必须使用:var

编辑:我的想法&#34;没有进一步测试。

<击> 一个想法:

然而,您可以做的是使用并基于手册的示例#2

$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);

并将其更改为:

$q->bindParam('1', $pr_id_vin, PDO::PARAM_INT);
$q->bindParam('2', $pr_nom, PDO::PARAM_STR, 255);
...

等,但我无法100%确定,但你可以尝试一下。

答案 1 :(得分:3)

你错过了第一个专栏的占位符:

变化:

UPDATE produit  set pr_id_vin, ...

UPDATE produit  set pr_id_vin = ?, ...