I need some help here to put image on an "AD" that user creates. I'm doing it with JSON, Android, PHP and MySql
add_ad.php
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
//initial query
$query = "INSERT INTO ads ( ad_title, ad_price, ad_category, ad_description, ad_user, ad_contact ) VALUES ( :title, :price, :category, :description, :email, :mobile ) ";
//Update query
$query_params = array(
':title' => $_POST['ad_title'],
':price' => $_POST['ad_price'],
':category' => $_POST['ad_category'],
':description' => $_POST['ad_description'],
':email' => $_POST['ad_user'],
':mobile' => $_POST['ad_contact']
);
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Erro base de dados. Impossível adicionar o anúncio.";
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Anúncio criado com sucesso! Aguarde aprovação.";
echo json_encode($response);
} ?>
I don't know how to transform this to receive the image from android and save it on the server/database, PLEASE ALSO tell me the type of the field on database.
This is very important, after that I need to send image from my android to this .php and save it on that user.
Thanks, please be aware that I'm new on android, so be brave :)
I'm not sure how Android sends the image to this php script, but normally (by web) it's by using the $_FILES server variable. You can read more about file uploads and how to use it here: http://www.w3schools.com/php/php_file_upload.asp .
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
// IMAGE UPLOAD SUCCESSFULL, UPDATE DATABASE ROW
}
}
} else {
echo "Invalid file";
}
After the image is uploaded to the server, you save the filename in your database. If your filename won't be longer than 255 characters you should use a VARCHAR(255) field for this in your database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.