如果性别类型为0则存储M,如果性别类型为1则使用php和mysql存储F.

时间:2011-08-12 09:02:47

标签: php mysql html

如果用户被选中,我试图存储与收音机盒相关的性别类型。如果用户选中女性,它将被存储为数据库中的M,它将作为F存储在数据库中 或者(如果用户被选中,则男性值为'0',如果值为'0',则'M'将存储在数据库中;如果用户被检查,则女性值为'1',如果值为' 1''F'将存储在数据库中........

我的代码是这样的......

      $terms = "";
      $unique = "";
      $belt = "";
       $beltc = "";
      $email = "";
       $emailc = "";
       $firstname = "";
       $lastname = "";
      $nickname = "";
    $gender = "";
     $weightunit = "";
     $weight = "";
     $dob = "";
      $maxhr = "";
     $phoneno = "";

   if (isset($_POST['register'])) {
if (isset($_POST['terms']))
    $terms = $_POST['terms'];
$unique = $_POST['unique'];
$belt = $_POST['belt'];
$beltc = $_POST['beltc'];
$email = $_POST['email'];
$emailc = $_POST['emailc'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$nickname = $_POST['nickname'];


            if (isset($_POST['sex']))
        $gender = $_POST['sex'];
        $weightunit = $_POST['weightunit'];
        $phoneno = $_POST['phoneno'];
        $dob = str_replace(",", "", $_POST['dob']);

       $password = generatePassword();

$ExistingIndex = -1;
$gIndex=0;    
$query = "SELECT gIndex FROM `gym` WHERE gUnique='$unique'";
$result = mysql_query($query);
if ($result) {
    $row = mysql_fetch_array($result);
    if ($row) {
        $gIndex = $row['gIndex'];
    }
} else {
    //die (mysql_error() . "<br />ResultError:" . $query);
}

$query = "SELECT usrIndex FROM `user` WHERE usrBelt='$belt' AND usrEmail='$email'";

$result = mysql_query($query);
if ($result) {
    $row = mysql_fetch_array($result);
    if ($row) {
        $ExistingIndex = $row['usrIndex'];
    }
} else {
    echo mysql_error() . "<br />ResultError:" . $query;
}

$query = "UPDATE `user` SET usrDeleted=1 WHERE usrBelt='$belt'";
mysql_query($query) or die("Failed Query of " . $query . "<br />" . mysql_error());
$guid = "";
if ($ExistingIndex == -1) {
    $guid = uniqid('Web_', true);
    $query = "INSERT INTO `user`(usrBelt,usrFirstname,usrSurname,usrNickname,usrGender,usrEmail,usrMobile,usrDOB,usrWeight,usrWeightUnit,usrGUID,usrPassword,usrMaxHR,usrSIndex)";
    $query.=" VALUES (";
    $query.="'" . mysql_real_escape_string($belt) . "'";
    $query.=",'" . mysql_real_escape_string($firstname) . "'";
    $query.=",'" . mysql_real_escape_string($lastname) . "'";
    $query.=",'" . mysql_real_escape_string($nickname) . "'";
    $query.=",$gender =";
    $query.=",'" . mysql_real_escape_string($email) . "'";
    $query.=",''";
    $query.=",'" . date("Y-m-d", strtotime($dob)) . "'";
    $query.=",$weight";
    $query.=",$weightunit";
    $query.=",'" . $guid . "'";
    $query.=",'" . md5($password) . "'";
    if (empty($maxhr)) {
        $query.=",NULL";
    } else {
        $query.=",'" . $maxhr . "'";
    }
    $query.=",'" . $gIndex . "'";
    $query.=")";
} else {
    $query = "UPDATE `user` SET ";
    $query.="usrFirstname='" . mysql_real_escape_string($firstname) . "'";
    $query.=",usrSurname='" . mysql_real_escape_string($lastname) . "'";
    $query.=",usrNickname='" . mysql_real_escape_string($nickname) . "'";
    $query.=",usrGender=$gender";
    $query.=",usrEmail='$email'";
    $query.=",usrDOB='" . date("Y-m-d", strtotime($dob)) . "'";
    $query.=",usrWeight=$weight";
    $query.=",usrWeightUnit=$weightunit";
    $query.=",usrPassword='" . md5($password) . "'";
    $query.=",usrDeleted=0";
    $query.=",usrLoggedIn=0";
    if (empty($maxhr)) {
        $query.=",usrMaxHR=NULL";
    } else {
        $query.=",usrMaxHR=" . $maxhr;
    }
    $query.=" WHERE usrIndex=$ExistingIndex";
}
mysql_query($query) or die("Failed Query of " . $query . "<br />" . mysql_error());

这是html代码

        <td style="text-align:right;padding-right:16px;">
                            Male <input type="radio" id="sex_m" name="sex" value="0" <?php if ($gender == '0')
        echo "checked='checked'" ?> onchange="validateRadio('img_sex');" /> Female <input type="radio" id="sex_f" name="sex" value="1" <?php if ($gender == '1')
                                        echo "checked='checked'" ?> onchange="validateRadio('img_sex');" />
                        </td>

我想更改如果用户选中了男性复选框,则值M将存储在数据库中(或)如果用户选中了男性复选框,则默认值为“0”,它将被转换为“m”< / p>

这也将对女性类型复选框进行...

任何人都可以帮忙.......

1 个答案:

答案 0 :(得分:1)

不确定这是一个笑话还是其他什么,但无论如何:

if( $_POST['sex'] == 0 ) {
    $gender = 'M';
}
else {
    $gender = 'F';
}

或者,您知道,首先只使用M和F作为表单值。