0

We have a table vehicle and a simple php form. Before inserting data I do check if the vehicle registration number exist but some client pc could enter duplicate entries. Below is the code snippet. What else could be causing this ?

$vehicleRegistrationNumber=$_POST['vehicleRegistrationNumber'];
$selectQuery1 ="Select vehicleRegistrationNumber From Vehicle Where vehicleRegistrationNumber='".$vehicleRegistrationNumber."'"; 
$result1 = mysqli_query($link,$selectQuery1);
$row1 = mysqli_fetch_array($result1, MYSQL_ASSOC);
$n1 = mysqli_num_rows($result1);

if($n1 > 0) {
  $status="<span class=\"statusFailed\">: Vehicle ".$vehicleRegistrationNumber." Already Exist.</span>";
}
else {
  //insert codes
}
2
  • 7
    Why don´t just use UNIQUE index on vehicleRegistrationNumber? That way mysql will handle it for you and you just have to take care about the error you will get if you try to insert duplicate row. Commented Jun 21, 2013 at 18:07
  • Yes I have now done that on the UNIQUE INDEXING but what could be the problem via the browser that this check failed? Commented Jun 21, 2013 at 18:15

3 Answers 3

1

First of all your code is vulnerable to SQL injection. This check can be bypassed by submitting something like XYZ0001' AND 1='0 or even more malicious values. To prevent this, use prepared statements and param binding instead of string concatenation.

Other possibility is simply user mistake, for example trailing space ("XYZ001" != "XYZ0001 ") that is hard to spot on the first glance ad records in DB. Before checking its existence in DB you should check with PHP if submitted value includes only allowed chars and is free from common mistakes.

Sign up to request clarification or add additional context in comments.

3 Comments

so I got few other codes where I do this sort of check. So how to avoid them or strengthen on their security? So how avoid the common mistakes in php and over come on the sql injection will this be a help fullful if I put them in mysql_real_escape_string() ?
You are using mysqli in your code, so if you insist on not using prepared statement, use mysqli_real_escape_string. Here you can read more about sql injections. As for common mistakes you have to define patterns for desired data and check if it fits your requirements, and if not, prompt the user with desired format.
I have been using mysqli_real_escape_string it in my other pages. IS it safe enough or should I move to prepaered statements?
1

try this with group by

 $selectQuery1 ="Select vehicleRegistrationNumber From Vehicle Where vehicleRegistrationNumber='".$vehicleRegistrationNumber."' GROUP BY vehicleRegistrationNumber"; 

9 Comments

the funny part it that without the group by also should be able to check right?
i mean you can also filter duplicates by php i know you are using php :)
i gave you my answer to dont get duplicates it doesnt help ?
your query , you forgot GROUP BY statment
@echo_Samir can you explain why adding GROUP BY should help?
|
1

The best way is to handle it on the SQL side. Just define the field as UNIQUE INDEX.

Now when trying to insert a duplicate index an error will be thrown and you can catch it like this:

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }

Like this you can avoid the select query before every insert query. Just handle the error as you want.

7 Comments

why and what is wrong with the query infact ? I am quite curious why it works on my side ?
probably nothing is wrong. just suggesting a way to avoid an extra query to the database
what I am curious how come such a simple query could have failed?
the select query didn't fail. but if the registration number is not a unique key, is very easy to insert duplicates.
When you say very easy means in what scenario it might have failed?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.