0

I am trying to store an Base64 string to my MySql database using PHP. 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. 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.

1 Answers1

0

chnage the sql query as below mentioned

$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']);
Ashok sri
  • 73
  • 7