Compare an Input with a record in a bbdd

0

We imagine that I have this form (It's much bigger but I want to try just one field):

<form id="form1 " action="?action" method="POST">                
  <tr>
    <th style="text-align:left;">Acronimo</th>
      <td>
      <input  id="acronimo" class="requisites" class="form-control"  type="text" name="acronimo"  value="acronimo"/>
      </td>
  </tr>
</form>

When we send the form it is sent to phpmyadmin by PDO. Now, how can I compare it with phpmyadmin because "Acronym" would be the station in which it is inserted and if it does not exist it will be executed but if it exists it sends an alert saying: "'value entered per user' already exists in the bbdd"

Would something like this be?:

class CategoriaModel {

private $pdo;


public function __CONSTRUCT() {
    try {
        $this->pdo = new 
PDO('mysql:host=localhost;dbname=deimos1;charset=UTF8', 'root', '');
        $this->pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
        $this->pdo->exec("SET NAMES 'utf8';SET lc_messages = 'es_ES'");
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    } catch (PDOException $e) {
        die($e->getMessage());
    }
}



public function Registrar(Categoria $data) {
try {
    $sql = "INSERT IGNORE INTO categoria (acronimo,categoria,registro_calidad)
        VALUES (?, ?,?)";

        $this->pdo->prepare($sql)->execute(array(
        $data->__GET('acronimo'),
        $data->__GET('categoria'),
        $data->__GET('registro_calidad')
            )
    );
if($sql->rowCount() > 0) {
echo "se insertó ";
}  else {
echo '<script>alert($acronimo "ya exista en la bbdd");</script>;';
};
} catch (PDOException $e) {
    die($e->getMessage());
}
}
}
    
asked by Alberto Cepero de Andrés 14.05.2017 в 19:29
source

1 answer

1

In the following example I am assuming a database called "test" and a table called "table".

The records are the same, but you must assume they are the ones you are bringing with POST.

INSERT IGNORE will insert the data, only if the primary key does not exist. Otherwise, it will simply ignore the entry of that data. Below I check if there was data insertion and under those conditions you can put what you want to do when a record has been inserted or not (if it was inserted it existed, if it was not inserted, it did exist):

<?php

try {
    $params = 'mysql:host=localhost;dbname=test;charset=utf8';
    $db = new PDO($params, 'root', '');
} catch (PDOException $e) {
    die('Error: ' . $e->getMessage());
}

$acronimo=27;
$valor1 = "hola";
$valor2 = "chao";

$insert=$db->prepare("INSERT IGNORE INTO tabla (laPK, columna1, columna2) VALUES (:pk, :v1, :v2)");
$insert->bindValue('pk',$acronimo);
$insert->bindValue('v1',$valor1);
$insert->bindValue('v2',$valor2);
$insert->execute();
if($insert->rowCount() > 0) {
    echo "se insertó ";
}
else {
    echo "se ignoró";
}

?>

I hope it works for you, there is also a way to update the data if the primary key already exists. Tell me if this method served you or you need another example with what I mention.

    
answered by 14.05.2017 / 20:15
source