2 actions for the same sentence a

1

Does anyone know how to place this code correctly? I'm following one of the forum that I found but it always tells me:

SQLSTATE[HY093]: Invalid parameter number: 
number of bound variables does not match number of tokens
<a href="index2.php?action=editar&id="<?php echo $r->acro_documento; ?>
<?php echo $r->acro_subcategoria; ?>><img src="icon_editthis.png" width="30px" 
height="30px"/>

code

index.php     

// Logica
$alm = new Categoria();
$model = new CategoriaModel();

if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
    case 'actualizar':



        // Recupero el id del campo hidden

        $alm->__SET('acro_documento', $_REQUEST['acro_documento']);
        $alm->__SET('acro_subcategoria', $_REQUEST['acro_subcategoria']);
        $alm->__SET('tipo_documento', $_REQUEST['tipo_documento']);
        $alm->__SET('registro_calidad', $_REQUEST['registro_calidad']);
        $alm->__SET('calidad_code', $_REQUEST['calidad_code']);
        $alm->__SET('cod_tempo', $_REQUEST['cod_tempo']);

        $update_results = $model->Actualizar($alm, $_POST['id']);

        header('Location: index2.php');

        break;

    case 'registrar':
        $alm->__SET('acro_documento', $_REQUEST['acro_documento']);
        $alm->__SET('acro_subcategoria', $_REQUEST['acro_subcategoria']);
        $alm->__SET('tipo_documento', $_REQUEST['tipo_documento']);
        $alm->__SET('registro_calidad', $_REQUEST['registro_calidad']);
        $alm->__SET('calidad_code', $_REQUEST['calidad_code']);
        $alm->__SET('cod_tempo', $_REQUEST['cod_tempo']);

        $model->Registrar($alm);
        header('Location: index2.php');
        break;

    case 'eliminar':
        $model->Eliminar($_REQUEST['acro_documento']);
        $model->Eliminar($_REQUEST['acro_subcategoria']);
        header('Location: index2.php');
        break;

    case 'editar':
        // Recupero los datos por el id
        // $obj_categoria es un objeto del tipo Categoria
        $obj_categoria = $model->Obtener($_REQUEST['id']);

        break;
    default:
        // MENSAJE 404 PARA CUANDO LA ACCION NO ES VALIDA
        header('HTTP/1.0: 404 Not Found');
        die('<h1>404 Page Not Found</h1>');
}
}
?>

    

    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
</head>
<body style="padding:15px;">

    <div class="pure-g">
        <div class="pure-u-1-12">

            <form action="?action=<?php echo isset($obj_categoria) ? 'actualizar' : 'registrar'; ?>" method="POST" class="pure-form pure-form-stacked" style="margin-bottom:30px;">
                <input type="hidden" name="id" value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_documento'): ''; ?>" />
                <input type="hidden" name="id" value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_subcategoria'): ''; ?>" />

                <table style="width:500px;">
                    <tr>
                        <th style="text-align:left;">Acronimo Documento</th>
                        <td><input type="text" name="acro_documento"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_documento') : ''; ?>" style="width:100%;" required /></td>
                    </tr>
                    <tr>
                        <th style="text-align:left;">Acronimo Subcategoria</th>
                        <td><input type="text" name="acro_subcategoria"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_subcategoria') : ''; ?>" style="width:100%;" required /></td>
                    </tr>
                    <tr>
                        <th style="text-align:left;">Tipo de Documento</th>
                        <td><input type="text" name="tipo_documento"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('tipo_documento') : ''; ?>" style="width:100%;" required /></td>
                    </tr>
                    <tr>
                        <th style="text-align:left;">Registro de Calidad</th>
                        <td><input type="text" name="registro_calidad"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('registro_calidad') : ''; ?>" style="width:100%;" required /></td>
                    </tr>
                    <tr>
                        <th style="text-align:left;">Calidad Code</th>
                        <td><input type="text" name="calidad_code"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('calidad_code') : ''; ?>" style="width:100%;" required /></td>
                    </tr>
                    <tr>
                        <th style="text-align:left;">Codigo de Tiempo</th>
                        <td><input type="text" name="cod_tempo"  value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('cod_tempo') : ''; ?>" style="width:100%;" required/></td>
                        <td colspan="2">
                            <button type="submit" class="pure-button pure-button-primary">Guardar</button>
                        </td>
                    </tr>
                </table>
            </form>

            <table class="pure-table pure-table-horizontal">
                <thead>
                    <tr>
                        <th style="text-align:left;">Acronimo Documento</th>
                        <th style="text-align:left;">Acronimo Subcategoria</th>
                        <th style="text-align:left;">Tipo de Documento</th>
                        <th style="text-align:left;">Registro de Calidad</th>
                        <th style="text-align:left;">Calidad Code</th>
                        <th style="text-align:left;">Codigo de tiempo</th>
                        <th style="text-align:left;">Edicion</th>
                        <th style="text-align:left;">Eliminar</th>


                    </tr>
                </thead>
                <?php foreach ($model->Listar() as $r): ?>
                    <tr>
                        <td><?php echo $r->__GET('acro_documento'); ?></td>
                        <td><?php echo $r->__GET('acro_subcategoria'); ?></td>
                        <td><?php echo $r->__GET('tipo_documento'); ?></td>
                        <td><?php echo $r->__GET('registro_calidad'); ?></td>
                        <td><?php echo $r->__GET('calidad_code'); ?></td>
                        <td><?php echo $r->__GET('cod_tempo'); ?></td>

                        <td>
                            <a href="?action=editar&id=<?php echo $r->acro_documento;?>"
                               href="?action=editar&id=<?php echo $r->acro_subcategoria; ?>"
                            >
                            <img src="icon_editthis.png" width="30px" height="30px"/></a>
                        </td>
                        <td>
                            <a href="?action=eliminar&acro_documento=<?php echo $r->acro_documento; ?>"
                               href="?action=eliminar&acro_subcategoria=<?php echo $r->acro_subcategoria; ?>"
                            >
                            <img src="delete.png" width="30px" height=30px"/></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>

        </div>
    </div>

</body>

alumno.model2.php

<?php

class CategoriaModel {

private $pdo;

public function __CONSTRUCT() {
    try {
        $this->pdo = new PDO('mysql:host=localhost;dbname=deimos1', 'root', '');
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

public function Listar() {
    try {
        $result = array();

        $stm = $this->pdo->prepare("SELECT * FROM tipodocumentos");
        $stm->execute();

        foreach ($stm->fetchAll(PDO::FETCH_OBJ) as $r) {
            $alm = new Categoria();

            $alm->__SET('acro_documento', $r->acro_documento);
            $alm->__SET('acro_subcategoria', $r->acro_subcategoria);
            $alm->__SET('tipo_documento', $r->tipo_documento);
            $alm->__SET('registro_calidad', $r->registro_calidad);
            $alm->__SET('calidad_code', $r->calidad_code);
            $alm->__SET('cod_tempo', $r->cod_tempo);
            $result[] = $alm;
        }

        return $result;
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

public function Obtener($acronimo) {
    try {
        $stm = $this->pdo->prepare("SELECT * FROM tipodocumentos WHERE acro_documento  = ? and acro_subcategoria = ?");

        $stm->execute(array($acronimo));
        $r = $stm->fetch(PDO::FETCH_OBJ);

        $alm = new Categoria();

        $alm->__SET('acro_documento', $r->acro_documento);
        $alm->__SET('acro_subcategoria', $r->acro_subcategoria);
        $alm->__SET('tipo_documento', $r->tipo_documento);
        $alm->__SET('registro_calidad', $r->registro_calidad);
        $alm->__SET('calidad_code', $r->calidad_code);
        $alm->__SET('cod_tempo', $r->cod_tempo);

        return $alm;
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

public function Eliminar($acronimo) {
    try {
        $stm = $this->pdo
                ->prepare("DELETE FROM tipodocumentos WHERE acro_documento = ? and acro_subcategoria = ?");

        $stm->execute(array($acronimo));
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

// ##############################################################################################
// CAMBIOS REALIZADOS
// - USO DE PARAMETROS NOMBRADOS EN EN METODO .execute()
// ##############################################################################################       
public function Actualizar(Categoria $data, $acro_doc_viejo, $acro_sub_viejo) {

    try {

        $sql = "UPDATE tipodocumentos SET tipo_documento =:tipo_documento, registro_calidad =:registro_calidad, calidad_code =:calidad_code, "
                . "cod_tempo =:cod_tempo, acro_documento =:acro_documento, acro_subcategoria =:acro_subcategoria WHERE acro_documento=:acro_doc_viejo and acro_subcategoria =:acro_sub_viejo";



        return $this->pdo->prepare($sql)
                        ->execute(
                                array(
                                    ':acro_doc_viejo' => $acro_doc_viejo,
                                    ':acro_sub_viejo' => $acro_sub_viejo,
                                    ':acro_documento' => $data->__GET('acro_documento'),
                                    ':acro_subcategoria' => $data->__GET('acro_subcategoria'),
                                    ':tipo_documento' => $data->__GET('tipo_documento'),
                                    ':registro_calidad' => $data->__GET('registro_calidad'),
                                    ':calidad_code' => $data->__GET('calidad_code'),
                                    ':cod_tempo' => $data->__GET('cod_tempo')
                                )
        );
    } catch (Exception $e) {

        die($e->getMessage());
    }
}

// ##############################################################################################

public function Registrar(Categoria $data) {
    try {
        $sql = "INSERT INTO tipodocumentos (acro_documento,acro_subcategoria,tipo_documento,registro_calidad,calidad_code,cod_tempo)
            VALUES (?, ?, ?, ?, ?, ?)";

        $this->pdo->prepare($sql)
                ->execute(
                        array(
                            $data->__GET('acro_documento'),
                            $data->__GET('acro_subcategoria'),
                            $data->__GET('tipo_documento'),
                            $data->__GET('registro_calidad'),
                            $data->__GET('calidad_code'),
                            $data->__GET('cod_tempo')
                        )
        );
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

}

student.entity2.php

<?php
class Categoria
{
private $acro_documento;
private $acro_subcategoria;
    private $tipo_documento;
    private $registro_calidad;
    private $calidad_code;
    private $cod_tempo;

public function __GET($k){ return $this->$k; }
public function __SET($k, $v){ return $this->$k = $v; }
}

</a>

Or this one with which I'm also testing

<a href="index2.php?action=editar&id&id="<?php echo $r->acro_documento; ?>
                                <?php echo $r->acro_subcategoria; ?>><img src="icon_editthis.png" width="30px" height="30px"/>
                            </a>

It is supposed that they collect two data given which it collects but does not know how to redirect itself or something I say

the mysql error and it is done in PDO

    
asked by Alberto Cepero de Andrés 04.04.2017 в 17:33
source

3 answers

0

In this part of the code you have two variables, but you only associate one:

...

public function Obtener($acronimo) {
    try {
        $stm = $this->pdo->prepare("SELECT * FROM tipodocumentos WHERE acro_documento  = ? and acro_subcategoria = ?");

        $stm->execute(array($acronimo));
        $r = $stm->fetch(PDO::FETCH_OBJ);

        $alm = new Categoria();

        ...

That's what the error may be throwing at you and the same thing happens in the Eliminar function (it may be happening elsewhere, but at least in those two).

    
answered by 04.04.2017 в 18:00
0

But in DELETE and in that SELECT at least in DELETE I need to select those two basically because I must delete from a DB records that this has two primary keys, so to delete a data I need to select the two Primary Key I do not know if I you understand

In function delete I made this change

public function Eliminar($acro_documetno,$acro_subcategoria) {
    try {
        $stm = $this->pdo
                ->prepare("DELETE FROM tipodocumentos WHERE acro_documento = ? and acro_subcategoria = ?");

        $stm->execute(array($acro_documento,$acro_subcategoria));
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

Now it does not throw any errors but it does not work either

    
answered by 04.04.2017 в 19:33
0
 public function Eliminar($acro_documento,$acro_subcategoria) {
    try {
        $stm = $this->pdo
                ->prepare("DELETE FROM tipodocumentos WHERE acro_documento = ? and acro_subcategoria = ?");

        $stm->execute(array($acro_documento,$acro_subcategoria));
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

public function Obtener($acro_documento,$acro_subcategoria) {
    try {
        $stm = $this->pdo->prepare("SELECT * FROM tipodocumentos WHERE acro_documento  = ? and acro_subcategoria = ?");

        $stm->execute(array($acro_documento,$acro_subcategoria));
        $r = $stm->fetch(PDO::FETCH_OBJ);

        $alm = new Categoria();

        $alm->__SET('acro_documento', $r->acro_documento);
        $alm->__SET('acro_subcategoria', $r->acro_subcategoria);
        $alm->__SET('tipo_documento', $r->tipo_documento);
        $alm->__SET('registro_calidad', $r->registro_calidad);
        $alm->__SET('calidad_code', $r->calidad_code);
        $alm->__SET('cod_tempo', $r->cod_tempo);

        return $alm;
    } catch (Exception $e) {
        die($e->getMessage());
    }
}

And in the index

 <td>
                            <a href="index2.php?action=editar&id=;editar&id="<?php echo $r->acro_documento; ?><?php echo $r->acro_subcategoria; ?>>
                                 <img src="icon_editthis.png" width="30px" height="30px"/>
                            </a>                               

                        </td>
                        <td>
                            <a href="?action=eliminar&acro_documento+acro_subcategoria=<?php echo $r->acro_documento; ?>
                                <?php echo $r->acro_subcategoria; ?>"><img src="delete.png" width="30px" height=30px"/>
                            </a>
                        </td>

So I have it right now

    
answered by 04.04.2017 в 19:51