edit data problem

Discussion in 'PHP' started by mirosoft1, Jan 20, 2008.

  1. #1
    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>&nbsp;</p>
    <p>&nbsp;</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>
     
    mirosoft1, Jan 20, 2008 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    Change your database structure to:
    id primary key auto_increment
    username
    password
    sex
    date
     
    Kaizoku, Jan 20, 2008 IP
  3. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    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?????
     
    mirosoft1, Jan 20, 2008 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    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?
     
    Kaizoku, Jan 20, 2008 IP
  5. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    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
     
    mirosoft1, Jan 20, 2008 IP
  6. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    so there is no solution to it????????
     
    mirosoft1, Jan 20, 2008 IP
  7. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #7
    What he wants is index not primary key; I think he has the wrong idea of how primary key works.
     
    Kaizoku, Jan 20, 2008 IP
  8. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #8
    username can make it primary key?????
     
    mirosoft1, Jan 20, 2008 IP
  9. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #9
    please i want solution to it
     
    mirosoft1, Jan 20, 2008 IP
  10. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Set id as primary key and username as index ie: ALTER TABLE `blahoo` ADD INDEX ( `username` ) or UNIQUE,
    ALTER TABLE `blahoo` ADD UNIQUE ( `username` )
     
    wing, Jan 20, 2008 IP
  11. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #11
    he donot want id at all he want username is the primary key so i ask you if it will work or not?
     
    mirosoft1, Jan 20, 2008 IP
  12. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #12
    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 !
     
    killerj, Jan 20, 2008 IP
  13. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #13
    Well, I think the guy is wrong, you should have user id. :)
     
    wing, Jan 20, 2008 IP
  14. webexpert

    webexpert Banned

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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..
     
    webexpert, Jan 20, 2008 IP
  15. webexpert

    webexpert Banned

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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)..
     
    webexpert, Jan 20, 2008 IP
  16. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #16
    yes he want to not dupliacte the value of username but i make it programming so tell me the code of checking please??
     
    mirosoft1, Jan 20, 2008 IP
  17. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #17
    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 ?
     
    killerj, Jan 20, 2008 IP
  18. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #18
    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.
     
    Sabbir, Jan 21, 2008 IP
  19. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #19
    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
     
    mirosoft1, Jan 22, 2008 IP
  20. webexpert

    webexpert Banned

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    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..
     
    webexpert, Jan 22, 2008 IP