【发布时间】:2016-12-20 07:21:02
【问题描述】:
我正在尝试使用 php webservice 和 json 将图像从我的 xamarin.Forms 代码保存到 mysql 数据库
运行 php 脚本时出现以下错误
** [20-Dec-2016 00:17:08] PHP 警告:mysqli_prepare() 期望参数 1 为 mysqli,在第 13 行的 /home7/gsakolac/public_html/BBA/prod_update_image.php 中给出 null [20-Dec-2016 00:17:08] PHP 警告:mysqli_stmt_bind_param() 期望参数 1 为 mysqli_stmt,在第 14 行的 /home7/gsakolac/public_html/BBA/prod_update_image.php 中给出了 null [20-Dec-2016 00:17:08] PHP 警告:mysqli_stmt_execute() 期望参数 1 为 mysqli_stmt,在第 15 行的 /home7/gsakolac/public_html/BBA/prod_update_image.php 中给出了 null [20-Dec-2016 00:17:08] PHP 警告:mysqli_stmt_affected_rows() 期望参数 1 为 mysqli_stmt,在第 17 行的 /home7/gsakolac/public_html/BBA/prod_update_image.php 中给出 null **
我的 php 脚本
<?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);
?>
我的 xamarin.forms 代码将图像文件(imgFile 名称(字符串))转换为 base64string 并调用 php 脚本
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);
}
}
我是 php 编码新手,请帮助我。
阿米特·萨拉夫
【问题讨论】:
-
您的应用程序的错误报告级别不足
标签: php android mysql mysqli xamarin.forms