tags:

views:

117

answers:

5
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("form", $con);

$reb = "select count(*) from customer where name = '$name';"
if (mysql_result($reb,0) > 0) {
  echo "Item Already Added!<br>";
} else {
  // add the item
}
mysql_close($con);
header( 'Location: http://localhost/cus.php' );
?>

Parse error: parse error in C:\wamp\www\c.php on line 10

+2  A: 

Your query needs to be formulated as a string.

$reb = "select count(*) from customer where name = '{$name}'";

edit:

It looks like you're still trying to figure out the basics of PHP here. Database duplication is probably a concern you can worry about after you learn how to recognize and fix your syntax errors. We've already helped you identify a few. Check out some PHP tutorials and see where those take you.

keithjgrant
Still,the same error.
DAFFODIL
Now,error is on line11
DAFFODIL
Actually in your code `$name` would not be substituted by PHP as the string is not enclosed in double quotes.
Felix Kling
Whoops. Wasn't paying enough attention. fixed.
keithjgrant
+2  A: 

The parse error is happening because the semicolon at the end of your SQL query should go after the end of the string (after the "), not inside it.

"select count(*) from customer where name = '$name';"
                                                   ^

to

"select count(*) from customer where name = '$name'";
                                                    ^
Ilia Jerebtsov
Notice: Undefined variable: name in C:\wamp\www\c.php on line 10Warning: mysql_result() expects parameter 1 to be resource, string given in C:\wamp\www\c.php on line 11add the item getting,this error
DAFFODIL
@DAFFODIL: But in general you know how to debug PHP code if you have an error?
Felix Kling
nope,i dnt know it.
DAFFODIL
@DAFFODIL: Then why are you developing in PHP? You should learn PHP first before using it. We are not here to take care of all the peanuts.
Felix Kling
The $name variable that you're using in your string isn't being set anywhere. The second error is caused by you using the mysql library incorrectly. You need to use mysql_query to send the SQL to the server first. That will give you the resource you pass into mysql_result.
Ilia Jerebtsov
A: 

Aside from the syntax error, to actually answer your question, you should enforce a UNIQUE constraint on the field you want to protect. Perform a blind INSERT and if the statement should fail with an error #1062 (duplicate entry for key 'whatever'), then force the user to reinput a new value. This is a much more smooth algo for providing unique usernames, etc. Don't just search through every record everytime you want to perform an insert.

bdl
I am n't getting that error,it is storing duplicate record into db
DAFFODIL
+1  A: 

To answer your question:

How to avoid duplication entry via form into database

You already seem to have a solution. Just remove the code errors you have.

Reading documentation and having a look at examples is also very helpful. Read about mysql_result

Felix Kling
+1  A: 

Hire a PHP programmer. That will (most likely) fix the error.

webbiedave
ouch. (ok, ok, I know I need more chars)
MJB
I would never give such an answer to others but this guy is clearly not a programmer and is just trying to get free code/fixes and has a zero acceptance rate.I really think it's the most helpful answer for this particular OP.
webbiedave