0

this is my connection to database:

<?php
$servername = "localhost";
$user = "root";
$pass = "mhimlaA#1";

$conn = new PDO("mysql:host=$servername;dbname=mydb", $user, $pass);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//echo "Connected successfully";

function read($where = '')
{
    global $conn;
    $sql = "SELECT * FROM `form` $where LIMIT 1000;";
    $stm = $conn->prepare($sql);
    $stm->execute();
    return $stm->fetchAll(PDO::FETCH_ASSOC);
}

and this my store my where the user enter his data to the DB:

<?php
//print_r($_POST);
include 'db.php';
$sql = "INSERT INTO mydb.form(username, password, email) VALUES (:username,:password,:email)";
$stm = $conn->prepare($sql);
$stm->execute($_POST);
header('location: index.php');

i want to handle the duplication error for username and password and send message to user?

  • It's hard to tell since your question is missing a description of the expected behavior. But I think what you are looking for is the [mysql `insert ... on duplicate key update` feature](https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html). – Zeitounator Jan 23 '22 at 13:36
  • Do you mean duplication error for username and email? Duplicate passwords shouldn't be an issue (and you shouldn't even be able to check for that since they should be stored as hashes generated by password_hash()) – M. Eriksson Jan 23 '22 at 13:40
  • yeah i meant email sorry – Amir Hossein Shirazi Jan 23 '22 at 14:04

0 Answers0