How to code to add a record if there is none, else edit the record?

Discussion in 'PHP' started by tyler_durden, Mar 30, 2007.

  1. #1
    I'm slowly learning php, and I have had a lot of help from this forum so far. Thanks!

    Anyway I have code below that edits a record. I had a 2 day nightmare with syntax errors on my last project, that i'm frazzled to start changing this code. Basically I want the code to check the table for a record for the username, and if there is no record then add the record, else if it finds the record to edit it.

    <?php
    
    extract($_POST);
    extract($_GET);
    
        $april_fools = $_POST['april_fools'];
        $boss_day = $_POST['boss_day'];
        $boxing_day = $_POST['boxing_day'];
        $canada_day = $_POST['canada_day'];
        $chinese_new_year = $_POST['chinese_new_year'];
        $christmas = $_POST['christmas'];
    
        
     
      $query = "UPDATE holiday_reminders SET april_fools='".$april_fools."',boss_day='".$boss_day."',boxing_day='".$boxing_day."',canada_day='".$canada_day."',chinese_new_year='".$chinese_new_year."',christmas='".$christmas."' WHERE usrname='$my->username'";
      mysql_query($query);
    
     
     Header("Location: my-reminders.php"); 
      exit; 
    ?>
    Code (markup):
     
    tyler_durden, Mar 30, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Here a quick example.
    
    $query = "UPDATE holiday_reminders SET april_fools='".$april_fools."',boss_day='".$boss_day."',boxing_day='".$boxing_day."',canada_day='".$canada_day."',chinese_new_year='".$chinese_new_year."',christmas='".$christmas."' WHERE usrname='$my->username'";
    mysql_query($query);
    
    if (!mysql_affected_rows())
    {
         $query = "INSERT INTO table (fields) VALUES (values)";
         mysql_query($query) OR die( mysql_error() );
    }
    
    
    PHP:
     
    nico_swd, Mar 30, 2007 IP
  3. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you - that gives me a good example to start with!
     
    tyler_durden, Mar 30, 2007 IP
  4. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm close, but this error gives me no information. The new code is below.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,,,1)' at line 1
    (this error is given when I select to add the christmas (last) option)

    $query = "UPDATE holiday_reminders SET april_fools='".$april_fools."',boss_day='".$boss_day."',boxing_day='".$boxing_day."',canada_day='".$canada_day."',chinese_new_year='".$chinese_new_year."',christmas='".$christmas."' WHERE usrname='$my->username'";mysql_query($query);
    if (!mysql_affected_rows()){     $query = "INSERT INTO holiday_reminders (usrname,april_fools,boss_day,boxing_day,canada_day,chinese_new_year,christmas) VALUES ($my->username,$april_fools,$boss_day,$boxing_day,$canada_day,$chinese_new_year,$christmas)";     mysql_query($query) OR die( mysql_error() );}
    Code (markup):
     
    tyler_durden, Mar 30, 2007 IP
  5. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hmm, I thought maybe because the database fields had a "Not Null" attribute to it, and I wasn't passing something in every field, that was the issue. Nope...
     
    tyler_durden, Mar 30, 2007 IP
  6. tyler_durden

    tyler_durden Peon

    Messages:
    340
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    forget it, I think I found it. The error is coming from somewhere else. I won't have a chance to look at it until Moday now anyway. Thanks for all the help!
     
    tyler_durden, Mar 30, 2007 IP