I have created a form in html which prompts users to input first name, last name and phone number. I want this data to be inserted into an oracle table called W_TEST. I want the phone number to be unique (e.g. two users may have the same first and last name, but not the same number. The database I use is oracle. One way I fixed this was by setting phone number as primary key, but i don’t like that solution.
How could I modify my php code to warn user that his/her data was not inserted in case of duplicate phone numbers?
I would appreciate your valuable help!
My code is the following:
(1) oracle table:
CREATE TABLE W_TEST
(
FNAME VARCHAR2(64) NULL,
LNAME VARCHAR2(64) NULL,
TNUM INT NOT NULL,
CONSTRAINT MYKEY PRIMARY KEY (TNUM) – I want to work without it!
);
COMMIT;
(2) html form:
> <!DOCTYPE html>
> <html lang="en">
> <head>
> <meta charset="UTF-8">
> <link rel="stylesheet" type="text/css" href="form_style.css">
> <title>User Input Data</title>
> </head>
> <body>
>
> <k1 class = "myhome">Input Data</k1>
>
> <!-- <h3>Please enter your personal data</h3> --->
>
> <form method="post" action="form_data.php" id="forma">
> <fieldset>
> <legend>Insert your data below:</legend>
> Όνομα:<br>
> <input type="text" name="firstname" required><br>
> Επώνυμο:<br>
> <input type="text" name="lastname" required><br>
> Τηλέφωνο:<br>
> <input type="number" name="phone" required pattern="[0-9]{11}"><br>
> <input type="submit" value="ok!">
> </fieldset>
> <h6>(*Note: You must fill all fields!)</h6>
> </form>
>
> </body>
> </html>
>
> ----
>
> (3) **php code (this is where I need your help!):**
>
> <?php
>
> echo "<link rel='stylesheet' type='text/css' href='form_style.css'>";
>
> error_reporting(E_ALL);
> ini_set('display_errors', 'On');
>
> $username = "KONRMS"; // Use your username
> $password = "Welcome2018"; // and your password
> $database = "localhost/XE"; // and the connect string to connect to your database
>
> $a = $_POST["firstname"];
> $b = $_POST["lastname"];
> $c = $_POST["phone"];
>
> $quake = "
> INSERT INTO W_TEST
> (FNAME, LNAME, TNUM)
> VALUES
> ('$a', '$b', '$c')
> ";
>
> $c = oci_connect($username, $password, $database, 'UTF8');
>
> if (!$c) {
> $m = oci_error();
> trigger_error('Could not connect to database: '. $m['message'], E_USER_ERROR);
> }
>
> $s = oci_parse($c, $quake);
> if (!$s) {
> $m = oci_error($c);
> trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
> }
>
> $r = oci_execute($s);
> if (!$r) {
> $m = oci_error($s);
> trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
> }
>
> echo "<ppp> Successfully saved data! </ppp>";
>
> ?>
I was aware of that kind of solution. But we want to have the duplicate check done by the php code… I try to check if TNUM count>0 and if yes, code to say user number alrwady exist. Could help? Thanks a lot!
That is the wrong approach and will build in a race condition. What if two people share a house phone? You are only going to allow one of them? You might want to re-think your approach.
I’m practicing and I want to see how this works with that concept. I want each person to have a unique number. Ok two people can share a phone number. Let’s replace then phone number with a unique number id. Two people can have the same first and last name but cannot have the same id. Could you help with php code in that case?
You usually don’t want the user to have control over the primary key (id) of their database record.
What if you have some service that only paying users can access but users are allowed to change their id to whatever they want?
User 88 did not pay. User 99 did pay. User 99 changes their id to 1099 (which does not yet exist) and then user 88 changes their id to 99, now that user can access the private content for free.
It would make sense for mobile phone or e-mail address to be unique.
I have modified my php as following. It’s very odd sometimes it works and sometimes not! Could you help me a little with it (especially with if statement).
Please don’t stick to phone number recommendations. I’m practicing and I try to understand how oci(s) work.