检查是否选中单选按钮并更新另一个字段

时间:2014-05-22 10:02:43

标签: php mysql

我有一个带有两个单选按钮的表单'给我发电话'和'收集键'。为了使用户只选择两个选项中的一个,两者的输入名称相同。如果用户选择值等于yes的第一个单选按钮,则更新以下数据库字段:

UPDATE survey_bookings_mainsite SET appointment_phone = 'Yes' WHERE ref = $ref

否则,如果第二个单选按钮的值等于no,则更新以下数据库字段:

UPDATE survey_bookings_mainsite SET appointment_keys = 'Yes' WHERE ref = $ref

问题: 第二个查询在appointment_phone字段中返回“否”,并在appointment_keys字段中显示NULL。应该发生的是显示“否”的appointment_phone字段和显示“是”的appointment_keys字段。我试图在第二个IF声明中重新分配$appointment_phone等于看起来像这样:

$appointment_phone = mysql_real_escape_string($_POST['appointment_keys']);

但这不起作用。

include("config/cn.php");
if(isset($_POST['Submit'])){
    // Use array_map to secure all POST values:
    $_POST = array_map('mysql_real_escape_string', $_POST);
    $ref                        = $_POST['ref'];
    $property_number            = $_POST['property_number'];
    $property_address1          = $_POST['property_address1'];
    $property_address2          = $_POST['property_address2'];
    $property_town              = $_POST['property_town'];
    $property_postcode          = $_POST['property_postcode'];
    $appointment_phone          = $_POST['appointment_phone'];
    $appointment_contact_number = $_POST['appointment_contact_number'];
    $appointment_keys           = $_POST['appointment_keys'];

    if($_POST['appointment_phone'] == 'Yes'){
        $sql = mysql_query("UPDATE survey_bookings_mainsite SET appointment_phone = 'Yes' WHERE ref = $ref");
    }
    if ($_POST['appointment_phone'] == 'No'){
        $sql = mysql_query("UPDATE survey_bookings_mainsite SET appointment_keys = 'Yes' WHERE ref = $ref");
    }

    $collect_number       = $_POST['collect_number'];
    $collect_postcode     = $_POST['collect_postcode'];
    $collect_address1     = $_POST['collect_address1'];
    $collect_address2     = $_POST['collect_address2'];
    $collect_town         = $_POST['collect_town'];
    $collect_phone        = $_POST['collect_phone'];
    $report_name          = $_POST['report_name'];
    $report_number        = $_POST['report_number'];
    $report_address1      = $_POST['report_address1'];
    $report_address2      = $_POST['report_address2'];
    $report_town          = $_POST['report_town'];
    $report_postcode      = $_POST['report_postcode'];
    $report_phone         = $_POST['report_phone'];
    $report_email         = $_POST['report_email'];
    $special_instructions = $_POST['special_instructions'];

    $enter_sql = "INSERT INTO survey_bookings_mainsite (ref,property_number,property_address1,property_address2,property_town,property_postcode,appointment_phone,appointment_contact_number,collect_number,
                                       collect_address1,collect_address2,collect_town,collect_postcode,collect_phone,report_name,report_number,report_address1,report_address2,report_town,
                                       report_postcode,report_phone,report_email,special_instructions)
                                       VALUES(\"$ref\",\"$property_number\",\"$property_address1\",\"$property_address2\",\"$property_town\",
                                       \"$property_postcode\",\"$appointment_phone\",\"$appointment_contact_number\",\"$collect_number\",\"$collect_address1\",\"$collect_address2\",\"$collect_town\",\"$collect_postcode\",
                                       \"$collect_phone\",\"$report_name\",\"$report_number\",\"$report_address1\",\"$report_address2\",\"$report_town\",\"$report_postcode\",\"$report_phone\",\"$report_email\",\"$special_instructions\")";                                   

    $enter_query = mysql_query($enter_sql);
    header('Location: /thankyou.php');  
    exit;
} 

1 个答案:

答案 0 :(得分:0)

您的问题是,您的两个if语句将永远不会同时运行,一个将在yes上运行,一个将在no上运行。您必须将它们组合成1个查询:

$query = "UPDATE survey_bookings_mainsite SET 
             appointment_phone = '".($_POST['appointment_phone']=='Yes' ? 'Yes' : 'No')."' ,
             appointment_keys  = '".($_POST['appointment_phone']=='No'  ? 'Yes' : 'No')."'
          WHERE ref = $ref LIMIT 1";
$sql = mysql_query($query);

我冒昧地添加LIMIT 1,如果你只需更新1行,当负载更多时,这会显着提高速度:)

如果/ else或ternairy,查询中的代码很短。以下是完全相同的

if( $var === true ){ echo 'yes';}
else{                echo 'No';}

echo $var ===true ? 'yes' : 'no';

你的方法实际上可行(尽管我强烈建议你不要执行以下操作!):<​​/ p>

// Same code, simplefied for example:
if($_POST['appointment_phone'] == 'Yes'){
    $sql = "UPDATE table SET appointment_phone = 'Yes',appointment_keys='No'";
}
if ($_POST['appointment_phone'] == 'No'){
    $sql = "UPDATE table SET appointment_phone = 'No',appointment_keys='Yes'";
}

你不想要这个,因为现在你有两个查询几乎完全相同的事情。如果要切换到1/0而不是是/否,该怎么办?您将不得不切换两个查询(错误的两倍)。 试想一下,如果你有10个像这样的输入会发生什么。