If or While and How to do this?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AJ Lewis
    New Member
    • Feb 2011
    • 1

    If or While and How to do this?

    Hi all,
    im writing some php script to add records to my db. However, if a value thats about to be added already exists, then dont add that record.
    for example: I have a table called assignment and a col that is called assignment_num which has the values of 71,72,73,74,75, 76,77 and 78. Each value represents one assignment. If 71 and 72 are already in the db I want to insert only the next one which would be 73. I hope this make sense?

    Below I have be able to check and see if they exists and print them out but cannot figure out how to only insert one from here.
    Code:
    
    if ( $row['assignment_num'] >= "71" && $row['assignment_num'] <="78")
    { 
    print $row[assignment_num] . "<br> "; 
    	$row[assignment_num]++;
    }
    Right now it pulls from my DB and shows
    71
    72
    73
    74

    Because that is what I have in the col within the DB. I want it to add 75 to it this time. Next time I access the page I want it to add 76 and so on.


    Thanks,
    AJ
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Make that column Primary key and also make it auto_increment.

    Code:
    Create TABLE tbltest
    {
     Id integer PRIMARY KEY AUTO_INCREMENT,
     Info varchar(20)
    };
    And Your Query to insert data would be.

    Code:
    INSERT INTO tbltest(Info) values('Data');

    CHECK Syntax

    Comment

    Working...