mysql UPDATE, does not receive function value in PHP

Asked

Viewed 125 times

0

I have that function:

$id = pega_assunto_por_nome_menu($_GET['assunto'])['id'];

That returns the id value if you use echo $id; But when I update to mysql, it doesn’t work.

This error appears:

You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '' at line 3

Line six is the definition of id = {$id};

$atualiza = "UPDATE assuntos SET
            nome_menu = '{$nome_menu}'
            WHERE id = {$id}";

If I use var_dump, this is the result.

$assunto = pega_assunto_por_nome_menu($_GET['assunto']);
var_dump($assunto); 

array (size=10)
  0 => string '1' (length=1)
  'id' => string '1' (length=1)
  1 => string 'Sobre a Preliminartes' (length=21)
  'nome_menu' => string 'Sobre a Preliminartes' (length=21)
  2 => string 'sobre_a_preliminarte' (length=20)
  'nome_menu_slug' => string 'sobre_a_preliminarte' (length=20)
  3 => string '1' (length=1)
  'posicao' => string '1' (length=1)
  4 => string '1' (length=1)
  'visivel' => string '1' (length=1)
  • would not be $id = pega_assunto_por_nome_menu($_GET['assunto']['id']); ?

  • This error appears if change, Illegal string offset 'id'

  • Debug this array before moving to the function. var_dump($_GET['assunto']);

  • do so : $assunto = pega_assunto_por_nome_menu($_GET['assunto']); and then var_dump($assunto); and put the result here...

  • I put the result at the end of the question

  • @Alêmoraes, I added an answer there, do the test. If it doesn’t work leave in the comments of the reply.

  • I have not yet managed, I put the whole code of this study in this link: http://pastebin.com/zjwAmbDx

  • @Alêmoraes managed to?

  • I got it, I still don’t understand why, kkk, but in the form action, it applied the id instead of the name_menu_slug, hence the function returned zero always

Show 4 more comments

3 answers

1


(ALWAYS) Validate the variables before performing any operation with the database:

// Valida se a variável está vazia
if (empty($_GET['assunto']))
  die('Assunto não informado.');

// Recebe todos os dados da função
$assunto = pega_assunto_por_nome_menu($_GET['assunto']);

// Verifica se recebeu algum ID da função anterior
if (empty($assunto) || empty($assunto['id']))
  die('Nenhum resultado encontrado.');

// ¬¬
$id = $assunto['id'];

// Dependendo de onde você recebe essa variável é bom valida-la também
$nome_menu = filter_var($_GET['nome_menu']); // Caso a validação falhe, será retornado FALSE

// Verifica se a string  filtrada é valida
if ($nome_menu === FALSE)
   die('Nome inválido.');

$atualiza = "UPDATE assuntos SET
        nome_menu = '{$nome_menu}'
        WHERE id = {$id}";

0

Change that line: $id = pega_assunto_por_nome_menu($_GET['assunto'])['id'];

By this line: $id = pega_assunto_por_nome_menu($_GET['assunto']['id']);

  • This error appears if I change, Illegal string offset 'id'

  • Actually, the error is in the array structure. : $id = pega_assunto_por_nome_menu($_GET['assunto']['id']); so: $id = pega_assunto_por_nome_menu($_GET['assunto'][1]['id']);

  • Illegal string offset 'id appears'

  • I put the whole code of this study on this link: http://pastebin.com/zjwAmbDx

0

Hello, your function has to be returning something... return $algo;. If your application is the way you passed it, it won’t work, the ID index is outside the function parameter. Change this:

$id = pega_assunto_por_nome_menu($_GET['assunto'])['id'];

therefore:

$id = pega_assunto_por_nome_menu($_GET['assunto']['id']);

Then having the value inside the $id variable, debug the same: var_dump($id); to confer.

  • With var_dump($id): string '1' (length=1)

  • Then do the following: when updating, use this: $atualiza = "UPDATE assuntos SET nome_menu = '" . $nome_menu . "' WHERE id = '" . $id . "'; More by Trigger, because your id is coming as string.

  • I have not yet managed, I put the whole code of this study in this link: http://pastebin.com/zjwAmbDx

  • On line 6 - $id = pega_assunto_por_nome_menu($_GET['assunto'])['id'];, change to that: $id = pega_assunto_por_nome_menu($_GET['assunto']);. If even so, give the error, what error does it return you? On line 33, debug that $inteiro. Right after you assign value to $inteiro, make the following command: $atualiza = "UPDATE assuntos SET nome_menu = 'teste' WHERE id = " . $inteiro; . with this is to update the menu name as "test" in your comic, if it is not, the error is in your id. NOTE: check before the test if there is nothing NOTNULL in the table...

  • Notice: Array to string Conversion on line 9 - echo $id;

  • The mistake is there, that $id has to have a string or integer value, not array. debug this $id and give me the value... Value has to be like this: $id = '4';

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.