Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

duplicate entry on database using php

Tags:

c#

php

mysql

how to check for duplicate entry on a form using php?my front end is c#windows application

I am adding a new user to database using c# and php file.

I have created the following:

my project in c# (form1):

c# coding:

  private void btncreate_Click(object sender, EventArgs e)
  {
 var request = (HttpWebRequest)WebRequest.Create("http://***.**.***.***/response.php");
            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/x-www-form-urlencoded";
            using (var stream = request.GetRequestStream())
            {
                var buffer = Encoding.UTF8.GetBytes("userid= " + txtUserid.Text + " & password=" + txtConformpassword.Text + " & first_name=" + txtFirstname.Text + " & last_name=" + txtLastName.Text + "& role= " + cmbRole.Text + "& active=" + cmbActive.Text + "");
                stream.Write(buffer, 0, buffer.Length);
            }
            var response = (HttpWebResponse)request.GetResponse();
            string result = String.Empty;
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
            txtMessage.Text = result; 
}

my PHP file :response.php

<?php    
$uid = $_POST['userid'];   
$pass = $_POST['password'];   
$fname = $_POST['first_name'];   
$lname = $_POST['last_name'];   
$rol = $_POST['role'];
$act = $_POST['active'];

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

   $sql= "INSERT INTO aster(userid,password,first_name,last_name,role,active)
VALUES('$uid', '$pass', '$fname','$lname','$rol','$act');";

     if ($conn->query($sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

it add data successfully but when i enter the same userid in textbox and click create button i need to get an error message in txtmessage as userid already exists.

i search through googling but i did not got any thing about duplicate entry please refer me some coding

like image 492
Arthi Avatar asked Dec 31 '15 07:12

Arthi


People also ask

How to stop duplicate entry in PHP?

1- Remove duplicate values using 'DISTINCT' key in mysql query, if it is present in table. 2- Check whether the value is present in table or not using PHP and Mysql before inserting data into the table. 3- We can also avoid duplicate values storing in mysql table while inserting by primary key.

How to prevent duplicate Entries in sql table?

In the Navigation Pane, right-click the table that contains the field, and then click Design View. Select the field that you want to make sure has unique values. In the Field Properties pane at the bottom of the table design view, on the General tab, set the Indexed property to Yes (No duplicates).

How can we avoid duplicate username and email in your database using PHP and Mysqli?

-- Make it unique ALTER TABLE users ADD UNIQUE (username); This will prevent duplicate records in the table with the same username. When you try to insert the same one then an error will be generated. You can then catch the exception in PHP and check the reason.


4 Answers

You could extend your response.php to do a select statement before the insert. If you are able to select a record with the given ID, then yield a response (essentially an error code) which your C# application will then interpret and display to the user.

As a side note, since the ID field appears to be numeric, you can simply omit it from your form and let the DB increment it automatically. This would also mean that the user will not have to guess which ID values are available.

like image 129
npinti Avatar answered Oct 17 '22 03:10

npinti


you just want that the ID which you have entered should not be inserted at the next time . for that you just need to do one thing. make your id as a primary key in your database table creation , after doing this you will be not able to insert that id again or duplicate entries are not allowed in a primary key.

lemme know if it is useful.

like image 30
Anmol Garg Avatar answered Oct 17 '22 05:10

Anmol Garg


It may not help as much, but I think my explanation will point you to the right direction.

What I would do is:

  1. I would use SQL query to check if record exist and if exist then print "Record Exists" Else "Record does not exist". Below the code I post, 'Justin Yen' on PHP posting a quote, make sure you assign that to the field of inputting the username. In your case it's $uid.
  2. On PHP, assign the result of sql query to a variable.
  3. Create a string variable "Record Exists" and "Record does not exist".
  4. compare the string variable you create on step 3 to the result on step 2.
  5. I don't know any PHP coding, but you can try to find a way to compare string. But here's an example of checking record exist in SQL:

    Declare @Username varchar(MAX) = 'Justin Yen'
    
    IF EXISTS(select Username from EmployeTable where Username = @Username)
    
    begin print 'Record Exists' end
    
    else
    
    begin print 'Record does not exist' end
    
  6. Now all you have to do is if variable match then prompt "Cannot create record because Record created." Else use the add record codes you have in php.

like image 24
John Hang Avatar answered Oct 17 '22 03:10

John Hang


You can simply try to add the contraints(UNIQUE/PRIMARY) to the column 'userid'.

ALTER TABLE aster ADD CONSTRAINT MyUniqueKey UNIQUE KEY(userid)
like image 44
Vasanth Kumar Avatar answered Oct 17 '22 04:10

Vasanth Kumar