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

I have:

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

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!

2 Years
Discussion Span
Last Post by pixelsoul

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.