0
votes

am having a student table thata holds records of students. Am having a problem with the on duplicate update key function. With the way my system work, I randomly assign unique numbers 2 student after they save q record..

My query luk likes this

$length=(10000000,999999999); $random=substr(uniqid,0,$length); This query creates an alphanumeric unique key 4 me which works perfectly. .

My save query is dis

$save="insert into student (REG_NUMBER,username,surname) VALUES('$random','username', '$surname') ON DUPLICATE KEY UPDATE username='$username' "; $result=mysql_query($save) or die(mysql_error());

The save part of the query works perfectly, But it doesn't update the existing record, Rather the it creates a new random id without updating The old record. Pls hw do I go about it..need help becos am new 2 php

2
What is the primary key of your table? This will be checked for duplicates.yunzen
The primary key is the $_session(random).once the record is saved,a unique number is given to the user.it is automatically assigned and its is not auto incremented.user2943374

2 Answers

0
votes

Firstly, reg_number has to be a unique, primary key - so make sure it is.

Secondly, you are using $random as reg_number, can you guarantee it isn't a duplicate?

Thirdly, make sure that you don't have an AUTOIncrement property set on your reg_number column

0
votes

The effects are not identical for an InnoDB table where primary_key/unique_key is an auto-increment column. With an auto-increment column, an INSERT statement increases the auto-increment value but UPDATE does not

check your engine as well as autoincrement in PK