简体   繁体   English

无法在MySQL数据库中存储Base64数据

[英]Unable to Store Base64 Data in MySql Database

I am trying to store an Base64 string to my MySql database using PHP. 我正在尝试使用PHP将Base64字符串存储到MySql数据库中。 Here is my code. 这是我的代码。

<?PHP
//Store Image
$photoData = $_POST['PhotoData'];

//Database connection details
$db_url = "192.168.1.140";
$db_user_name = "easyblogadmin";
$db_password = "admin";
$db_name = "easyblog";

$con = mysqli_connect($db_url, $db_user_name, $db_password);
$sql = "insert into temptable (photodata) values('$photoData')";

if(mysqli_connect_errno()){
        echo "Failed to Connect Database";
        echo mysqli_connect_errno();
        die();
    } else {
        //Connecting to Application Database
        $db_selected = mysqli_select_db($con, $db_name);
        if(!$db_selected){
            echo "Failed to Connect Application Database";
            die();
        } else {
            if (!mysqli_query($con,$sql)) {
                die('Error: ' . mysqli_error($con));
            }
            mysqli_close($con);
            echo "Successfully Added photo".strlen($photoData);
        }
    }?>

Here the $photoData is my Base64 encoded string and I am storing this in my DB. 这里的$ photoData是我的Base64编码的字符串,我将其存储在数据库中。 When i am trying to retrieve this data from DB some data in my string is missing. 当我尝试从数据库检索此数据时,我的字符串中的某些数据丢失了。 I don't understand why this is happening. 我不明白为什么会这样。 Can any one help me out. 谁能帮我吗。

regards. 问候。

chnage the sql query as below mentioned 如下所述更改sql查询

$sql = "insert into databasename.temptable (photodata) values('$photoData')";

also please make sure if the encoded value has any single quotes,if it is then replace the data as mentioned below . 还请确保编码后的值是否具有单引号,如果是,则按如下所述替换数据。

$photoData =str_replace("'","\'",$_POST['PhotoData']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM