Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

Dewayne Thomas
PLUS
Dewayne Thomas
Courses Plus Student 10,283 Points

Database question

Hi,

im am trying to save a picture to my mysql database using base64string. i can take a pic or open he pic from my gallery and show it on screen. from there i try to save it. it saves all data except my image. i have stepped through my code to verify that it was converted to base64string but the db shows blob - 0' i am using a medium blob. if i convert online using a base64 converter then doing a save through an sql using phpmyadmin statement it works. but from my app it doesnt work. Please help. My code is below.

private byte[] _picData;
    if (resultCode == Result.Ok)
    {
        if (requestCode == 2)
        {
            Uri selectedImage = data.Data;

            Bitmap mBitmap = null;
           try
            {
                mBitmap = Media.GetBitmap(this.ContentResolver, selectedImage);
                photo.SetImageURI(selectedImage);

                Stream stream = ContentResolver.OpenInputStream(data.Data);
                Bitmap bitmap = BitmapFactory.DecodeStream(stream);
                MemoryStream newStream = new MemoryStream();
                bitmap.Compress(Bitmap.CompressFormat.Webp, 100, newStream);
                _picData = newStream.ToArray();
            }
            catch (Java.IO.IOException e)
            {
                e.PrintStackTrace();
            }

        }



        WebClient client = new WebClient();
        System.Uri uri = new System.Uri("http://www.sbmgroup.ca/test/biteboard/meal.php");
        NameValueCollection parameters = new NameValueCollection();
        if (_type.Equals("takePicture") || _type.Equals("openPicture"))
        {
            parameters.Add("type", "add");
            parameters.Add("user_id", _user_id.ToString());
            parameters.Add("meal", mealType);
            parameters.Add("scribble", scribble.Text);
            parameters.Add("feeling", _feelValue.ToString());
            parameters.Add("taste", _tasteValue.ToString());
            parameters.Add("date", date.ToString("yyyy-MM-dd H:mm:ss"));
            parameters.Add("picture", Convert.ToBase64String(_picData));
            client.UploadValuesAsync(uri, parameters);
            client.UploadValuesCompleted += Client_UploadValuesCompleted;
        }

my php

<?php
    include("ConnectionInfo.php");
    global $type;
    global $id;
    global $user_id;
    global $meal;
    global $scribble;
    global $date;
    global $taste;
    global $feeling;
    global $picture;
    global $mealArray;

    $type = $_REQUEST['type'];
    $id = $_REQUEST['id'];
    $user_id = $_REQUEST['user_id'];
    $meal = $_REQUEST['meal'];
    $scribble = $_REQUEST['scribble'];
    $date = $_REQUEST['date'];
    $dateLike = "{$_REQUEST['date']}%";
    $taste = $_REQUEST['taste'];
    $feeling = $_REQUEST['feeling'];
    $picture = $_REQUEST['picture'];
    $mealArray = array();

    if ($type === "add")
    {
        $stmt = $conn->prepare('INSERT INTO meals (user_id,meal,comment,date,taste,feeling,mealPic) values(?,?,?,?,?,?,?)');
        $stmt->bind_param('isssiib',$user_id,$meal,$scribble,$date,$taste,$feeling,$picture);
        $stmt->execute();
        $stmt->store_result();
        $stmt->fetch();
        $newId = $conn->insert_id;
        echo $newId;
    }

1 Answer

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Generally the best practice is to store images on a file system or CDN and then reference the filename of that image in the database.