2

I have written a long program. Im just sharing here some lines to understand a concept which is my issue here. I want to check if the previous data is equal to the newly entered, if it is, then an error should occur that the data is already entered, without storing again in db. Following code is running but it duplicates data. It is not quite good. may you please check where I am wrong. thanks

Database Name Booking:

Table Name data:
Name: John, Anjel, Smith, David
CNIC: 33, 46, 33, 91

//// storing data in database from html form fields

<?php

$emp_name = addslashes($_POST['emp_name']);
$emp_cnic = addslashes($_POST['emp_cnic']);

?>

//// Selecting CNIC that is duplicated (already existed) on a new form submission

$already = mysql_query("SELECT CNIC FROM data WHERE CNIC = '$emp_cnic'");

//Dieing script without storing the data if it already exists.

if($already){
die('Sorry! you are already registered. For any query, contact the  administrator. ');
  • is `CNIC` `int or varchar` datatype? – Pathik Vejani Feb 22 '16 at 10:04
  • [Please do not use `mysql_*` functions in code, the mysql extension is officially removed in PHP 7.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?lq=1) Use PDO or mysqli instead. –  Feb 22 '16 at 10:13

3 Answers3

0

You couls also REPLACE mysql function.

If not, just change your request with a COUNT() and check if the returned result is greater than 0.

I would also highly recommand you to go on PDO instead of still using mysql_* (which is deprecated / removed)

LoïcR
  • 4,940
  • 1
  • 34
  • 50
0

You could use mysql_num_rows

$already = mysql_query("SELECT CNIC FROM data WHERE CNIC = '$emp_cnic'");

$record_count = mysql_num_rows($already );

// $record_count > 0  means data already exist
if( $record_count > 0 ) {
  // Show error 
} else {
  // Save data
}
Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29
  • I tried. It gives this error and data duplicates or adds data Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\booking\index.php on line 29 – Tehmeena Marvi Feb 22 '16 at 10:09
  • You should print your sql query. It might be problem with your query string. – Uttam Kumar Roy Feb 22 '16 at 10:11
  • You can manually check by this query SELECT CNIC FROM data WHERE CNIC = 43 ,if its work then problem occurs in your sql. – Uttam Kumar Roy Feb 22 '16 at 10:13
0

The problem I see with your code is the following:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The error you mentioned when you use mysql_num_rows() is caused because of the false return of the unsuccessful query.
You are checking a value that might be not boolean, to solve this use:

if(!isset($already) OR mysql_num_rows($already) == 0){
  // die
}
else{
  // do stuff
}

This check if the query was unsuccessful or if it is empty, so you always get a boolean to check your result set.

As @Sakuto pointed out is important to use not deprecated functions, therefor instead of using mysql_query() you should use mysqli or PDO functions.For further information read this.

Community
  • 1
  • 1
Asur
  • 379
  • 3
  • 20
  • I used the above code but it doesnt prevents duplication. Please tell me how to use mysqli or pdo? – Tehmeena Marvi Feb 22 '16 at 11:05
  • Well, you should check the official [manual](http://php.net/manual/es/mysqli.query.php), I'm sure there you will find much better info than the one I could give you :) – Asur Feb 22 '16 at 11:09
  • Just to make sure, is `addslashes()` a sanitazion function or it adds something to the string (something like slashes)? – Asur Feb 22 '16 at 11:11