medvedo medvedo - 6 months ago 22
MySQL Question

Sending content from my frontend to my PHP/MySQL-backend

When I send my content from my frontend it successfully reaches my if (!empty)-statement but the table in my phpmyadmin/mysql-database does not recieve the information and does not add it.

I have two tables. One varchar (text) named "photo" and a ID called "id" which is A_I.

With my current code I only send (well attempt to send) the text about "photo" but nothing about the ID as it is A_I? Maybe I need to add some addiotional code to that as well and that might be the issue here and the reason the database does not seem to add the content that I send?

<?php

$value = json_decode(file_get_contents('php://input'));

$mysql_pekare= new mysqli ("", "","", "");

if(!empty($value)) {

echo "You reached the IF-statement";
$stmt = $mysql_pekare->prepare("INSERT INTO photoAlbum(`photo`) VALUES(?)");

$stmt->bind_param("s", $value['photo']);

$stmt->execute();

$stmt->close();

$mysql_pekare->close();
}

?>


In my frontend when I send the content I recieve this in the log:

{"photo":"test"}


And I also recieve this in the log, the echo call I did if it reaches the IF function which it successfully does:

"You reached the IF-statement"

Answer

By default, json_decode() returns an object, so your value is in $value->photo.

So your INSERT code should be -

if(!empty($value)) {

  echo "You reached the IF-statement";
  $stmt = $mysql_pekare->prepare("INSERT INTO photoAlbum(`photo`) VALUES(?)");

  $stmt->bind_param("s", $value->photo);

  $stmt->execute();

  $stmt->close();

  $mysql_pekare->close();
}