insert into database with loop statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tlou22
    New Member
    • Dec 2013
    • 4

    insert into database with loop statement

    Hello
    trying to make a loop that can insert into database. every time user clicks on submit and if in the database dNum is 1 it loop and upgrade dNum which will make it 2..

    $sql="INSERT INTO temp_members_db ".
    "(dNum)".
    "VALUES('$dNum' )";
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that’s not called a loop, that’s called increment.

    you can do that by a simple SQL query:
    UPDATE mytable SET myfield = myfield+1 WHERE ?

    Comment

    • manigandanta
      New Member
      • Dec 2013
      • 1

      #3
      You can make use of the mysql INSERT ... ON DUPLICATE KEY UPDATE

      Code:
      INSERT INTO table (dNum) VALUES ($dNum)
        ON DUPLICATE KEY UPDATE dNum=dNum+$dNum;

      Comment

      Working...