hello, im wondering how to throw an error message to a user if the input value is a duplicate in INSERT INTO...

I have:

<?php
    require "connection.php";

    $name = $_POST["names"];
    $event_id = $_POST["events1"];
    $euser_id = $_POST["sessions"];
    $single_chair_id = $_POST["schair"];

    $assign = $dbh->prepare("INSERT INTO cinema(cuser_id,event_id,guest_name,chair_id) VALUES(?,?,?,?)");
    $assign->bindParam(1, $euser_id, PDO::PARAM_INT);
    $assign->bindParam(2, $event_id, PDO::PARAM_INT);
    $assign->bindParam(3, $name, PDO::PARAM_STR);
    $assign->bindParam(4, $single_chair_id, PDO::PARAM_STR);
    $assign->execute();
?>

example 1:

name : Xavi
event_id: 2
euser_id: 4
single_chair_id: A1

values of example 1 gets inserted yay!

example 2:

name : Nadia
event_id: 2
euser_id: 4
single_chair_id: A1

single_chair_id of example 1 and example 2 are the same, so how then do i alert the user that example 2 values cannot be inserted because single_chair_id is already in the database? thanks in advance!

I'm assuming you're working with MySQL and not MSSQL, have a look at this blog post about using a mutex table to do this with MySQL. If you just happen to actually be using MSSQL, you can do if not exists with a subquery to see if a value exists already.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.