i have form to update the data i made a database on it username primary key password sex date and the username is primary key but when i make any updates give me error Duplicated key 'data' for key 1 i get the data from another page this is the code include("conn.php"); $username=$_GET["username"]; $sql="select username,password,sex,email,address,date from $branch where username='$username'"; $result=mysql_query($sql,$connection) or die(mysql_error()); while($row=mysql_fetch_array($result)) { $username=$row['username']; $password=$row['password']; $sex=$row['sex']; $email=$row['email']; $address=$row['address']; $date=$row['date']; } ?> <p> </p> <p> </p> <form name="form1" method="post" action="formsubmit.php?mode=update"> <table width="500" border="1" align="center" cellpadding="2" cellspacing="2" dir=rtl> <tr> <td bgcolor="#800000" colspan="2"> <p align="center"><b><font color="#FFFFFF"><span lang="ar-sa"> edit</span></font></b></td> </tr> <tr> <td bgcolor="#800000"><b><font color="#FFFFFF">username </font></b> </td> <td><input name="username" type="text" id="username" value="<? echo $username; ?>"></td> </tr> <tr> <td bgcolor="#800000"><b><font color="#FFFFFF">password</font></b></td> <td><input name="password" type="text" id="password" value="<? echo $password; ?>"></td> </tr> <tr> <td bgcolor="#800000"><b><font color="#FFFFFF">sex</font></b></td> <td><input name="sex" type="text" id="sex" value="<? echo $sex; ?>"></td> </tr> <tr> <td bgcolor="#800000"><b><font color="#FFFFFF">email</font></b></td> <td><input name="email" type="text" id="email" value="<? echo $email; ?>"></td> </tr> <tr> <td bgcolor="#800000"><b><font color="#FFFFFF">address</font></b></td> <td><input name="address" type="text" id="address" value="<? echo $address; ?>"></td> </tr> <tr> <td bgcolor="#800000"><b><font color="#FFFFFF">date</font></b></td> <td><input name="date" type="text" id="date" value="<? echo $date; ?>"></td> </tr> <tr> <td><input type="submit" name="Submit" value="edit"></td> <td> </td> </tr> </table> </form>
i made that before but the owner of the site donot want that, he want from me to make username is the primary key i donot know what i do?????
That is very bad practice and inefficient, which may clog the database if there is alot of db calls. Anywho what is the data type you set the username as?
username is varchar i told that to him he told me when the user enter the same username it will be slow to search if the username is not primary key
Set id as primary key and username as index ie: ALTER TABLE `blahoo` ADD INDEX ( `username` ) or UNIQUE, ALTER TABLE `blahoo` ADD UNIQUE ( `username` )
well it will work as long as there is NO duplicate username Edit : Ah , you speaking about update ? It would only be good for delete No wayfor update mate ,you need a primary key for sure for example say you want to update a user "John" set username="jon" where username="John" Code (markup): is not the right way nor is it sensible to do . Your best bet would be to use a primary key,say 'member_id' of auto increment type !
This is just you are trying to insert or update a duplicate record... before any insert or update operation, you must check with a select query if username is already exist.. then donont update or insert that username... thatz it..
actually.. that project owner want to make sure that there should not be username duplication in anyway.. however this could be handle at programming level, but he chosen to make username primary key.. but no problem.. you are just trying to update/insert same/duplicate username.. you just try to make a check if username is already exist then don't update or insert that username (if update query, then can update rest of fields but not username)..
yes he want to not dupliacte the value of username but i make it programming so tell me the code of checking please??
having you learned programming ? Don't you know basic codes for checking duplicate entries when you say you code for some client ? Or are you just a designer or some sort ?
hi, username should be unique. like there is no sabbirhs in yahoo mail. i think making the username as primery key it will do every checking. you just need to take care of the error type and make change.
you donot understand me i mean where will i put the checking code after the update code or before it ?? if i but it before the update code and want to change any other field it will not changed because the username is the same and thank you for your words
i just wakeup actually, very fresh.. and saw you are stills having problem... let me try to help you out... ofcourse you will need to place checks before your update query... here is an example; first of all, i saw your current programming.. you really shouldn't use while loop.. because as you mentioned so many times that username is primary key, so definitely if username found then select query will return only one row, then while loops is not necessary and not an smart coding.. you should use mysql_num_rows instead.. if(mysql_num_rows($result) != 0) { $username=$row['username']; $password=$row['password']; $sex=$row['sex']; $email=$row['email']; $address=$row['address']; $date=$row['date']; } now, here is some help for you how you will do checks; $username=$_POST["username"]; $password=$_POST['password']; $sex=$_POST['sex']; $email=$_POST['email']; $address=$_POST['address']; $sql="select username from TableName where username='$username'"; $result=mysql_query($sql,$connection) or die(mysql_error()); if(mysql_num_rows($result) != 0) { $sql="update TableName set password='$password', sex='$sex', email='$email', address='$address' where username='$username'"; $result=mysql_query($sql,$connection) or die(mysql_error()); } if username found then update query will get executed for that specific row where username resides.. And if username not found, then it means username not exists and you may add an else block if you want an insert query to insert that user.. please note: you shouldn't allow to update the username, because its a primary key.. if you will allow user to update the username as well as password, sex and address, then you cannot update the records because username is primarykey, and if you will try to update it by itself then you don't have any other handler primary key for update query where clause..