A
A
Andreas Shopen2020-05-11 14:14:08
PHP
Andreas Shopen, 2020-05-11 14:14:08

How to make changes to multiple cells of a MySql database table through ORM RedBean php?

I logged into the site with a login and password, now I need to implement a change in user information.
There is a users table in the database (id, user_name, user_surname, user_age, user_thirdname, email, password).
I use sessions in php when logging in, and when logging in, the user gets into his personal account, where he can change information about himself, for this I use findOne, but when I make changes (name and age), it gives an error:
Fatal error: Uncaught RedBeanPHP\RedException: Expected array, NULL given.

Here is the code:

<?php
require "db.php";
$data = $_POST;

// изменение данных
if ( isset($_POST['change'])) {
      $findUserMail = R::findOne('users', 'email = ?', $data['email']);

      if ($findUserMail) {
            $findUser->user_name = $data['user_name'];
            $findUser->user_surname = $data['user_surname'];
            $findUser->user_thirdname = $data['user_otchestvo'];
            $findUser->user_age = $data['user_age'];	
            R::store($findUser);
      } else {
            echo "<script>alert(\"В введённых данных возникла ошибка:  \");</script>";
      }
}  
//выход из аккаунта и из сессии
if ( isset($_POST['out']) ) { 
  unset($_SESSION['logged_user']);
  header("Location:/index.php");
}


<form action="/index2.php" method="POST">
                        <p>Изменить личные данные</p>
                        <input type="text" placeholder="имя" name="user_name" required minlength="2" >
                        <input type="text" placeholder="фамилия" name="user_surname" required minlength="2" >
                        <input type="text" placeholder="отчество" name="user_otchestvo" required minlength="2" >
                        <input type="text" placeholder="возраст(лет)" name="user_age" required >
                        <button type="submit" name="change">Изменить</button>
                  </form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andreas Shopen, 2020-05-11
@Al_Shopen

In general, somehow * I thought of it myself - here is the solution:

$id = $_SESSION['logged_user']->id;
$id = (int)$id;

if ( isset($_POST['change'])) {
      $findUser = R::findOne('users', 'id = ?', [$id]);
      
            $findUser->user_name = $data['user_name'];
            $findUser->user_surname = $data['user_surname'];
            $findUser->user_thirdname = $data['user_otchestvo'];
            $findUser->user_age = $data['user_age'];	
            R::store($findUser);
            header("Location:/index2.php");
}

The rofl is that I determined the email from the session, then I decided to check by the user id, but then the idea came to me to ward dump and I got a string))) and in the database this cell is int and all I did was calculate the user id from the session and converted it to a number, since without conversion it was a string (I love dynamic typing) and then I realized that I mixed up the variable name - I had to just write findUser and that's it, otherwise it turned out I didn’t understand what it was :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question