-1

I'm trying to save image from my xamarin.Forms code to mysql database using php webservice and json

I'm getting following error while runing php script

** [20-Dec-2016 00:17:08] PHP Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in /home7/gsakolac/public_html/BBA/prod_update_image.php on line 13 [20-Dec-2016 00:17:08] PHP Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in /home7/gsakolac/public_html/BBA/prod_update_image.php on line 14 [20-Dec-2016 00:17:08] PHP Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, null given in /home7/gsakolac/public_html/BBA/prod_update_image.php on line 15 [20-Dec-2016 00:17:08] PHP Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, null given in /home7/gsakolac/public_html/BBA/prod_update_image.php on line 17 **

My php script

<?php
$conn = mysqli_connect('localhost', 'username', 'password', 'dbname');

if($_SERVER['REQUEST_METHOD'] == "POST"){
    $id = isset($_POST['id']) ? $_POST['id'] : "";
    $image = isset($_POST['image']) ? $_POST['image'] : ""; 

    if(!$image==null)
    {
        $sql = "UPDATE tbl_image SET Image = ? WHERE id=$id";

        $stmt = mysqli_prepare($con, $sql);
        mysqli_stmt_bind_param($stmt, "s", $image);
        mysqli_stmt_execute($stmt);

        $check = mysqli_stmt_affected_rows($stmt);
        if($check==1)
            $json=array("status" => 1, "msg" => "Image updates successfully");
            else
            $json=array("status" => 0, "msg" => "error happend while updating image to server.");
    }
    else
        $json=array("status" => 0, "msg" => "Image not uploaded");  
}else{
    $json = array("status" => 0, "msg" => "Request Not Accepted.");
}
@mysqli_close($conn);

/* Output header */
header('Content-type: application/json');
echo json_encode($json);
?>

My xamarin.forms code that converts image file (imgFile Name (string)) to base64string and calls php script

var bytes = default(byte[]);
using (var StreamReader = new StreamReader(imgFile))
{
    using (var mstream = new MemoryStream())
    {
        StreamReader.BaseStream.CopyTo(mstream);
        bytes = mstream.ToArray();
    }
}
string imgstr = Convert.ToBase64String(bytes);

using (var client = new HttpClient())
{
    var url = "url";
    var content = new FormUrlEncodedContent(new[] {
    new KeyValuePair<string,string>("id", id.ToString()),
    new KeyValuePair<string, string>("image", imgstr)
});

    var resp = await client.PostAsync(new Uri(url), content);
    if (!resp.IsSuccessStatusCode)
        General.GSErr("Nothing retrieved from server.");
    else
    {
        var result = JsonConvert.DeserializeObject<General_Response>(resp.Content.ReadAsStringAsync().Result);
        if (result.status == 0)
            General.GSErr(result.msg);
    }
}

I'm new with php codeing please help me.

Amit Saraf

Amit Saraf
  • 101
  • 1
  • 3
  • 15

1 Answers1

2

Because your first parameter is $con, which should be $conn

$stmt = mysqli_prepare($conn, $sql);

And don't bind variables directly to your query. You're already using prepared statement, utilize it.

$sql = "UPDATE tbl_image SET Image = ? WHERE id = ?";

$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "si", $image, $id);
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49