Atik
Atik

Reputation: 177

Insert & update sql data using php

i've a registration form & im trying to insert data if 'ID' doesn't exists. its working fine. now im trying to update data if 'ID' already exists. it's not working at all & i couldn't find the error. here is the condition i set if ID exists or not:

function staff_detail_exist($ic) {    
    $result = null;
    $sql = "SELECT * FROM apply WHERE staffid = '$ic'";
    $data = mysql_query($sql);

    if (mysql_num_rows($data) == 0) {    
        $result = "available";
    } else {    
        $result = "exist";
    }

    return $result;
}

and here is my insert & update function:

 if (staff_detail_exist($ic) == "available") {

    insert_staff_detail($ic, $name, $contact, $mail, $address, $paytype, $applicant);
    echo "Workshop application successful! You will be notified shortly via E-mail after confirmation! Thank You!";
} 
else if (staff_detail_exist($ic) == "exist") {

    update_staff_detail($ic, $name, $contact, $mail, $address, $paytype);
    echo  "Staff Details Updated!" ;
}

function insert_staff_detail($ic, $name, $contact, $mail, $address, $paytype, $applicant) {

    $sql = "INSERT INTO apply (staffid, staffname, staffno, staffemail, staffaddress, paytype, applicant) VALUES ('$ic', '$name', '$contact', '$mail', '$address','$paytype', '$applicant')";
    mysql_query($sql);
}

function update_staff_detail($ic, $name, $contact, $mail, $address, $paytype){
    $sql = "UPDATE apply 
            SET staffname='$_POST[name]',
            staffno='$_POST[contact]',
            staffmail='$_POST[mail]',
            address='$_POST[address]',
            paytype='$_POST[paytype]'
            WHERE staffid='$_POST[ic]'";
    mysql_query($sql);
}

any suggestion please? thanks!

Upvotes: 1

Views: 556

Answers (1)

alditis
alditis

Reputation: 4817

Observation: You must indicate when you receive POST variables. The update_staff_detail method should receive POST variables in the same way you get the insert_staff_detail method.

Change: Definition of update_staff_detail

To:

function update_staff_detail($ic, $name, $contact, $mail, $address, $paytype){
    $sql = "UPDATE apply 
            SET staffname='$name',
            staffno='$contact',
            staffmail='$mail',
            address='$address',
            paytype='$paytype'
            WHERE staffid='$ic' LIMIT 1";
    mysql_query($sql);
}

Regards.

Upvotes: 1

Related Questions