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

Php 检查单选按钮是否选中并更新另一个字段,php,mysql,Php,Mysql,我有一个表格,上面有两个单选按钮“给我打电话”和“收集钥匙”。为了让用户只选择两个选项中的一个,两个选项的输入名称都相同。如果用户选择值等于“是”的第一个单选按钮,则更新以下数据库字段: UPDATE survey_bookings_mainsite SET appointment_phone = 'Yes' WHERE ref = $ref 否则,如果第二个单选按钮的值等于否,则更新以下数据库字段: UPDATE survey_bookings_mainsite SET appointmen

我有一个表格,上面有两个单选按钮“给我打电话”和“收集钥匙”。为了让用户只选择两个选项中的一个,两个选项的输入名称都相同。如果用户选择值等于“是”的第一个单选按钮,则更新以下数据库字段:

UPDATE survey_bookings_mainsite SET appointment_phone = 'Yes' WHERE ref = $ref
否则,如果第二个单选按钮的值等于否,则更新以下数据库字段:

UPDATE survey_bookings_mainsite SET appointment_keys = 'Yes' WHERE ref = $ref
问题: 第二个查询在
appointment\u phone
字段中返回“No”,并在
appointment\u keys
字段中显示NULL。应该发生的是
appointment\u phone
字段显示“No”,而
appointment\u keys
字段也显示“Yes”。我已尝试在第二个IF语句中将
$appointment\u 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;
} 

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

$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);
我随意添加了
限制1
,如果您只需要更新1行,这将在负载增加时显著提高速度:)

查询中的代码是短if/else或ternairy。下面的操作完全相同

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

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

您的方法实际上是可行的(尽管我强烈建议您不要执行以下操作):

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

对您的代码进行了一些更改,如
array\u map()
(php.net/array\u map)以提高可读性和可维护性:)您能解释一下您的代码在做什么吗在你要求的同时进行编辑。我现在把我的答案扩大了很多,我明白了。因此,将两个IF语句组合成一个,我会做一些类似IF($_POST['appointment\u phone']]){}的事情,然后将update语句放在括号之间?用该代码(完全)替换这两个IF。IFs语句已经移动到查询中(请参阅我添加的额外信息),所以我用代码完全替换了这两个IFs,并将其放在插入查询之前。当我提交表格时,我只得到一张空白页。
// 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'";
}