1

我有一个注册表,如果“ID”不存在,我会尝试插入数据。它工作正常。如果“ID”已经存在,现在我正在尝试更新数据。它根本不起作用,我找不到错误。如果 ID 存在与否,这是我设置的条件:

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;
}

这是我的插入和更新功能:

 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);
}

请问有什么建议吗?谢谢!

4

1 回答 1

1

观察:您必须在收到 POST 变量时指明。该update_staff_detail方法应该以与您获取该insert_staff_detail方法相同的方式接收 POST 变量。

变化:定义update_staff_detail

至:

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);
}

问候。

于 2012-12-10T03:02:00.093 回答