PHP从选项中发布十六进制值,并以字符串格式存储到mysql

时间:2016-10-15 15:42:12

标签: php

我需要将十六进制值(从我的<options>发布的值)存储到我的mysql数据库而不转换它(保持字符串形式),以便我可以将该值与字符串形式的select选项进行比较。我尝试了很多东西,比如base_convert(),(string),hex2bin(),但不幸的是。我不知道该怎么做。

enter image description here

图像显示存储的值。

这是我到目前为止所做的。

的login.php:

$q = mysqli_query($con, "select * from users where email = '$email' and password = '$password'");
if (mysqli_num_rows($q) == 0)
{
$response['message'] = "Account not found.";
}
else
{
$_SESSION['user_currency'] = (string)$f['user_currency'];
}

dasboard.php:

<form id="form_setting_currency">
<select name="select_currency_symbol" class="form-control">
 <option value="&#x24;" <?php echo $_SESSION['user_currency'] == "&#x24;" ? "selected" : ""; ?> >&#x24; Dollar</option>
 <option value="&#xa2;" <?php echo $_SESSION['user_currency'] == "&#xa2;" ? "selected" : ""; ?> >&#xa2; Cent</option>
 <option value="&#xa3;" <?php echo $_SESSION['user_currency'] == "&#xa3;" ? "selected" : ""; ?> >&#xa3; Pound Sterling</option>
<option value="&#xa5;" <?php echo $_SESSION['user_currency'] == "&#xa5;" ? "selected" : ""; ?> >&#xa5; Yen</option>
</select>
</form>

SUBMIT_change_currency.php

<?php
include "includes/connection.php";
require_once("includes/encryption.class.php");

$response['success'] = false;
$response['message'] = "An error has occurred.";

if(isset($_POST['select_currency_symbol']) &&     trim($_POST['select_currency_symbol']) != "")
{

$currencyIcon = mysqli_real_escape_string($con,$_POST['select_currency_symbol']);
// $currencyIcon = mysqli_real_escape_string($con,hex2bin($_POST['select_currency_symbol']));
// $currencyIcon = mysqli_real_escape_string($con,(string)$_POST['select_currency_symbol']);

$query ="UPDATE users SET user_currency = '$currencyIcon' WHERE user_id =   $_SESSION[user_id]";
mysqli_query($con,$query);

if(mysqli_affected_rows($con)>0)
{
    $response['success'] = true;
    $response['message'] = "Successfully changed currency symbol.";
    $_SESSION['user_currency'] = (string)$currencyIcon;
}
else
{
    $response['message'] = "You have not made changes.";
}
}
die(json_encode($response));
?>      `

1 个答案:

答案 0 :(得分:0)

您需要在值中使用htmlspecialchars()打印它,因此它变为&amp;安培;#X24