-3

I have a database table registration(id,firstname,comment,reg_id)

iam fill the form and values are insert into database (firstname comment) ,i want to insert reg_id automatically.

id  firstname       comment        reg_id
1       Alexy         tfhgfnjh     R20123       
2       Thomas        fdghfgh      R20124  
3       chako         cgjkjhl      R20125       
4       Thomasxc       cgjkjhl     R20126          
5       Thomaseg       fdghgh      R20127      
6       Thomasm        fsgdfgbd    R20128  

but its not work ,reg_id value was not increment properly.

Code From Comment.

if($checksql>$reg_id){ 
  $checksql1=mysql_query("select reg_id from registration where id='$rid'"); 
  $r=mysql_fetch_array($cheksql1); $rgid=$r['reg_id']; 
  $reg_id1= $rgid+1; 
  $regid=mysql_query("update registration set reg_id='$reg_id1' where id='$rid'");  
}

how to solve my problem?

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
IPSCR
  • 59
  • 2
  • 8
  • 1
    You already have an auto incrementing ID column. Why keep 2 columns with same properties? – hjpotter92 Jul 27 '16 at 07:12
  • Iam using this code if($checksql>$reg_id){ $checksql1=mysql_query("select reg_id from registration where id='$rid'"); $r=mysql_fetch_array($cheksql1); $rgid=$r['reg_id']; $reg_id1= $rgid+1; $regid=mysql_query("update registration set reg_id='$reg_id1' where id='$rid'"); }` – IPSCR Jul 27 '16 at 07:13
  • Edit your question accordingly. Incidentally, it's fine and perfectly common to have a public key and a private (primary) key – Strawberry Jul 27 '16 at 07:14
  • 1
    This question is going to turn out bad. It is going to be an OCD custom incrementor thing. – Drew Jul 27 '16 at 07:14
  • @drew no, it's going to be cool. You'll see. – Strawberry Jul 27 '16 at 07:17
  • there is a auto increment id, then why an additional id ? – Jees K Denny Jul 27 '16 at 07:23
  • It's a fixation. Like the movie Bad Grandpa and the [Penguin](https://www.youtube.com/watch?v=sCQQFJDkJlA) ... darn that was the non-bleeped version. Ooops. – Drew Jul 27 '16 at 07:26

2 Answers2

0
if ($checksql > $reg_id) {
    $checksql1 = mysql_query("select reg_id from registration where id='$rid'");
    $r = mysql_fetch_array($cheksql1);
    $rgid = $r['reg_id'];
    $reg_id1 = 'R' . (substr($rgid, 1, strlen($rgid)) + 1);
    $regid = mysql_query("update registration set reg_id='$reg_id1' where id='$rid'");
}

You can use substr() to get the numbers part of your id. Increment it by 1 and concatenate it with the leading "R". (or if it doesn't have to be an "R" but could be something else. If there can be multiple letters you would need to count them first)

$reg_id1 = substr($rgid, 0, 1) . (substr($rgid, 1, strlen($rgid)) + 1);

Also see this: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Philipp
  • 2,787
  • 2
  • 25
  • 27
  • I am still waiting for this question to turn out cool as Strawberry said. There is no need for any of this and it is problematic. There is no enforced check constraint. It would mandate Triggers. Gaps could not be enforced. FK's would need a cascade. The whole thing is a train wreck and the OP needs to be discouraged from this. – Drew Jul 27 '16 at 07:36
0

Here you get the last executed reg_id, then convert into the substring.

$last_regid=mysql_query("SELECT reg_id FROM  registration ORDER BY id DESC LIMIT 0 , 1");
$row=mysql_fetch_assoc($last_regid);
$last_register=$row['reg_id'];
$substring = substr($last_register, 1);
$substring++;
Jees K Denny
  • 531
  • 5
  • 27