0

i am making a simple posting system for a flutter app and a requirement is it must save the post as a JSON file i have the log in done and same with the posting but i am trying to include the name of the user that posted the post by qwering for the current user and writing that to the file i have function that successfully can echo the full name of the user but i am un able to store the var in a way that lets me write it to the file

I have tried a couple of basic examples of querying and storing in var but to no success, I have also tried returning the var of the $fullname that I have in my function

this is my function to get the Fullname(this works) :

  public function get_fullname($uid){
    $sql3="SELECT fullname FROM users WHERE uid = $uid";
    $result = mysqli_query($this->db,$sql3);
    $user_data = mysqli_fetch_array($result);

    return $user_data['fullname'];

}

this is my code to write the json file (works):

$todays_date = date("y-m-d h:i:sa");
$data = array(

  'name' =>  $user_data['fullname'],
  'Title' => $Title,
  "post" => $Post,
  'date'=> $todays_date,
  'uid'=> $uid

);
$tittle = $user_data['fullname'] . "_post_on_" . $todays_date . ".json";


    function testfun($tittle, $data)
    {

    $fh = fopen("posts/$tittle", 'w')
    or die("error opening output file");
    fwrite($fh, json_encode($data,JSON_UNESCAPED_UNICODE));
    fclose($fh);
    }

    if(array_key_exists('submit',$_POST)){
       testfun($tittle, $data);
    }

the json comes out like this :

name    null
Title   "post test 1"
post    "this is a test"
date    "19-04-28 10:47:13pm"
uid "1"

but i need this :

name    "CURENT_USER"
Title   "post test 1"
post    "this is a test"
date    "19-04-28 10:47:13pm"
uid "1"
tadman
  • 208,517
  • 23
  • 234
  • 262
stamno
  • 13
  • 3
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add any data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or data *of any kind* directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Apr 28 '19 at 23:05
  • Note: The [object-oriented interface to `mysqli`](https://www.php.net/manual/en/mysqli.quickstart.connections.php) is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface where missing a single `i` can cause trouble. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is largely an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Apr 28 '19 at 23:06
  • What is `$user_data` and why do you have this function `get_fullname` in your question if you aren't using it? – miken32 Apr 28 '19 at 23:12
  • `get_fullname` is my attempt at getting the full name from the DB and it does do that but I need the result of that function to be put into the array that gets written to json file `$user_data` is to get an array from `$result` – stamno Apr 29 '19 at 00:25

1 Answers1

0

Your prompt $user_data['fullname'] is empty outside of function, call your function:

$data = array(

  'name' =>  get_fullname($uid),
  'Title' => $Title,
  "post" => $Post,
  'date'=> $todays_date,
  'uid'=> $uid

);
  • when i do this the function that writes the files does nothing and no file is written – stamno Apr 29 '19 at 00:48
  • Get some error? – Jakub Novák Apr 29 '19 at 01:06
  • Fatal error: Uncaught Error: Call to undefined function mysqli_query() in C:\Users\rambo\Downloads\oop-php-authentication-system-master\include\class.user.php:79 Stack trace: #0 C:\Users\rambo\Downloads\oop-php-authentication-system-master\home.php(3): include_once() #1 {main} thrown in C:\Users\rambo\Downloads\oop-php-authentication-system-master\include\class.user.php on line 79 but line 79 is just css line 79 : `margin-top: 310px;` – stamno Apr 29 '19 at 01:09
  • Sou your function get_fullname not working. You call undefined function...your PHP dont know mysqli try mysql_query – Jakub Novák Apr 29 '19 at 01:13
  • it was the server I got it further but now I need to include my secondary file with that part of the php `Error: Call to undefined function get_fullname() in /var/www/html/home.php:170 Stack trace: #0 {main} thrown in /var/www/html/home.php on line 170` even tho i definded it and included it above i posted the full code : [main page](https://pastebin.com/sXtdhQ2k) , [second_page](https://pastebin.com/TGYT8GGm) – stamno Apr 29 '19 at 02:45
  • You call function but this function not exist. If you have function get_fullname($uid) in object $user you must call it like this: $user->get_fullname($uid); – Jakub Novák Apr 29 '19 at 02:55
  • ok thank you for your help you have been very helpful but I still get null when writing to JSON – stamno Apr 29 '19 at 03:17
  • ok i got it fix thank you – stamno Apr 29 '19 at 03:21