funbinod Posted March 27, 2014 Share Posted March 27, 2014 how to check for duplicate entry on a form using php? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 27, 2014 Share Posted March 27, 2014 That's a vague question. If the information is in a database, then get the data from the form and look up any record in the database with the same information. Link to comment Share on other sites More sharing options...
funbinod Posted March 27, 2014 Author Share Posted March 27, 2014 (edited) i tried this --- <?php$query = "SELECT * FROM customerWHERE cname='$_POST[cname]'"; //trying to get data from form field named 'cname'$result = mysql_query($query);if (!$result) die("Unable to select database: " . mysql_error());// match dataif $dup = mysql_result($result,'cname');{echo "Name already register";}?> but didn't work. has this got some rhythm? or i should i have to go elsewhere? please....... Edited March 27, 2014 by funbinod Link to comment Share on other sites More sharing options...
justsomeguy Posted March 27, 2014 Share Posted March 27, 2014 That code has all kinds of errors. One problem is that you are using the mysql extension. You should use mysqli instead and use prepared statements to avoid SQL injection. You're also not connecting to the database before you're trying to use it. The second if statement has several syntax errors also, review the syntax for an if statement. Link to comment Share on other sites More sharing options...
funbinod Posted March 27, 2014 Author Share Posted March 27, 2014 please guide me through prepared statement.. i found this on php.net manual-- <?php$mysqli = new mysqli("example.com", "user", "password", "database");if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;}/* Non-prepared statement */if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) { echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;}/* Prepared statement, stage 1: prepare */if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;}?> please help me understand the use of "new" on first line and the exclamation (!) sign before $mysqli (or elsewhere) Link to comment Share on other sites More sharing options...
justsomeguy Posted March 27, 2014 Share Posted March 27, 2014 Those are very basic. The new keyword creates a new object. The ! operator is the boolean not operator, it inverts the truth of something. !true is false, and !false is true. You'll probably want to study the basics of PHP before moving on to things that are much more complex.There's a tutorial on mysqli and prepared statements here:http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/ Link to comment Share on other sites More sharing options...
funbinod Posted March 28, 2014 Author Share Posted March 28, 2014 u caught the point. i've just started to learn and thinking complex things. i'm reading php books downloaded from net. while going on reading something something strikes me and i try to move with it. i've just completed basics of jQuery. now m entering php (i know some basics of it already).... Link to comment Share on other sites More sharing options...
funbinod Posted March 28, 2014 Author Share Posted March 28, 2014 and please help me understand each elements on these lines-- if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;} and after that please help me become clear about why this used reverse statement before it without defining the statement itself. i mean there is nowhere declared $stmt = $mysqli->prepare() but it used !($stmt = $mysqli->prepare()) { } Link to comment Share on other sites More sharing options...
Ingolme Posted March 28, 2014 Share Posted March 28, 2014 That is just a short way of writing the following $stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)");if(!$stmt) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;} 1 Link to comment Share on other sites More sharing options...
funbinod Posted March 28, 2014 Author Share Posted March 28, 2014 That is just a short way of writing the following $stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)");if(!$stmt) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;} that's nice. thank u. but what is the use of (?) !? is this the value from a corresponding form? if so can we use this the same way for other functions? for example? Link to comment Share on other sites More sharing options...
birbal Posted March 28, 2014 Share Posted March 28, 2014 It is for placeholder where you will insert data to database. As for original post, you can also just insert query as usual and catch the exception on error. Every error has certain codes, so thus duplicate entry has one. just check the codes and do whatever you need to do. You have to make sure though to enable Exception mode of error reporting on PDO. Even further you can parse the error message to find which key is being violated for duplicate entry. Error message have certain fixed format so you can catch the names of the keys too. Link to comment Share on other sites More sharing options...
funbinod Posted April 1, 2014 Author Share Posted April 1, 2014 help me fild error now with this new tryout .... <?//check required fields$emptyCname = (empty($cname));$emptyCadd = (empty($cadd));$emptyCperson = (empty($cperson));if ($emptyCname == true && $emptyCadd == true && $emptyCperson ==true) { echo "Please enter Customer Name, Address and Contact Person!";}else if ($emptyCname == true && $emptyCadd == true) { echo "Please enter Customer Name and Address!";}else if ($emptyCname == true && $emptyCperson == true) { echo "Please enter Customer Name and Contact Person!";}else if ($emptyCadd == true && $emptyCperson == true) { echo "Please enter Address and Contact Person!";}else if ($emptyCname == true) { echo "Please enter Customer Name!";}else if ($emptyCadd == true) { echo "Please enter Address!";}else if ($emptyCperson == true) { echo "Please enter Contact Person!";}//check duplicate entryelse if (mysqli_num_rows(mysqli_query("SELECT cname * FROM customer WHERE cname='$cname'"))) { echo "is already registered.";}else {$sql = ("INSERT INTO customer (cname, cadd, cemail, cphone, cperson, cmob, cob, bal)VALUES ('$_POST[cname]', '$_POST[cadd]', '$_POST[cemail]', '$_POST[cphone]', '$_POST[cperson]', '$_POST[cmob]', '$_POST[cob]', '$_POST[cob]')");$query = mysqli_query($con,$sql) or die (mysqli_error());echo "<center><h2>" . $cname. " registered successfully!";}if (!mysqli_query($con, $sql)) { die('Error: ' . mysqli_error($con)); }mysqli_close($con);?> Link to comment Share on other sites More sharing options...
funbinod Posted April 7, 2014 Author Share Posted April 7, 2014 noone interested? anyway thanks..... Link to comment Share on other sites More sharing options...
thescientist Posted April 10, 2014 Share Posted April 10, 2014 What's the problem? what does it do / not do know? what have you done to investigate the problem? you can add echo statements to your code to trace it's execution, output variables and their values, etc. You could also turn error reporting on, etc <?phpini_set('display_errors', 'on'); //rest of your code You don't need to guess, but you do have to put a little effort into debugging to find your answers. Link to comment Share on other sites More sharing options...
funbinod Posted April 12, 2014 Author Share Posted April 12, 2014 i did it! thank u! if (mysql_num_rows(mysql_query("SELECT cname FROM customer WHERE cname = '$cname'"))) { echo "<div class='errorDiv'>" . $cname . " is registered already!</div>";} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now