Jump to content

duplicate entry


funbinod

Recommended Posts

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 by funbinod
Link to comment
Share on other sites

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

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

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

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

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

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;}
  • Like 1
Link to comment
Share on other sites

 

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...