PHP API with React Native to upload image and text


When I try to upload with html and php, I get perfect with this code

  // Create database connection
  $db = mysqli_connect("localhost", "root", "", "image_upload");

  // Initialize message variable
  $msg = "";

  // If upload button is clicked ...
  if (isset($_POST['upload'])) {
    // Get image name
    $image = $obj['image']['name'];
    // Get text
    $image_text = mysqli_real_escape_string($db, $_POST['image_text']);

    // image file directory
    $target = "images/".basename($image);

    $sql = "INSERT INTO images (image, image_text) VALUES ('$image', '$image_text')";
    // execute query
    mysqli_query($db, $sql);

    if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
        $msg = "Image uploaded successfully";
        $msg = "Failed to upload image";
  $result = mysqli_query($db, "SELECT * FROM images");

the html is this:

<!DOCTYPE html>
<title>Image Upload</title>
<style type="text/css">
    width: 50%;
    margin: 20px auto;
    border: 1px solid #cbcbcb;
    width: 50%;
    margin: 20px auto;
   form div{
    margin-top: 5px;
    width: 80%;
    padding: 5px;
    margin: 15px auto;
    border: 1px solid #cbcbcb;
    content: "";
    display: block;
    clear: both;
    float: left;
    margin: 5px;
    width: 300px;
    height: 140px;
<div id="content">
    while ($row = mysqli_fetch_array($result)) {
      echo "<div id='img_div'>";
        echo "<img src='images/".$row['image']."' >";
        echo "<p>".$row['image_text']."</p>";
      echo "</div>";
  <form method="POST" action="index.php" enctype="multipart/form-data">
    <input type="hidden" name="size" value="1000000">
      <input type="file" name="image">
        placeholder="Say something about this image..."></textarea>
        <button type="submit" name="upload">POST</button>

and this is how I normally try to upload everything from native react to the server, of course without an image, my question is how do I use the php above to work with native react, does anyone help me?

<?php include 'config2.php';

$json = file_get_contents('php://input');
    $obj = json_decode($json, true);

    $name = $obj['name'];
    $email = $obj['email'];
    $phone_number = $obj['phone_number'];
    $iamgeSource = $obj['iamgeSource'];

    if(mysqli_query($db, "INSERT INTO users(name,lastname, city, photo)VALUES('$name','$email','$phone_number','$iamgeSource')")){
        echo json_encode('Incidente incertado correctamente');
        echo json_encode('Fallo a el enviar el incidente');

asked by Andres Pèrez 12.11.2018 в 22:54

0 answers