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