UPDATE does not work with data from a POST

Asked

Viewed 64 times

0

I am getting the data from a POST and trying to update a mysql table, but UPDATE does not work at all.

If I use as described below, just replace the values with empty.

<?php
 require('../configs/conect_apostas.php');

$id=$_SESSION['id']; 
$selectOption = $_POST['selectOption'];

$dadosAlterar = $_POST['dadosAlterar']; 

$sql2 = "UPDATE dados SET alunos = '$dadosAlterar' WHERE 1";
if ($mysqli->query($sql2) === TRUE) { 
  echo "Sucesso!";
}

?>

If I give a echo $dadosAlterar get the value correctly.

If I use the code below, UPDATE is performed successfully.

<?php
 require('../configs/conect_apostas.php');

$id=$_SESSION['id']; 
$selectOption = $_POST['selectOption'];

$dadosAlterar = "Mateus"; 

$sql2 = "UPDATE dados SET alunos = '$dadosAlterar' WHERE 1";
if ($mysqli->query($sql2) === TRUE) { 
  echo "Sucesso!";
}
?>

If I perform an INSERT works correctly with the POST, but I need to perform an UPDATE.

  • Can you post the error generated in the update? ?

  • Felipe, no error, just replace with empty. The success query, replacing what you have with empty. And yes, I’ve tried running the query directly and it works.

  • INSERT INTO data (students) values ('$dadsAlter') ON DUPLICATE KEY UPDATE students = '$dadsAlter' tries using this method as it facilitates checking whether you are inserting a new record or changing an existing one

  • You have already printed $sql2 before running to see what is being mounted?

  • bfavaretto, I just printed it out, and by amazing it looks like the variable $dadsAltering is correct. echo of $sql2 -> UPDATE data SET students = 'Matthew' WHERE 1 Receiving data correctly from POST.

  • Felipe Fernandes, tried with the ON DUPLICATE KEY UPDATE, but still is replacing with empty.

Show 1 more comment

1 answer

0

Guys, I got a solution. I added a check and it’s working.

<?php
 require('../configs/conect_apostas.php');

$id=$_SESSION['id']; 
$selectOption = $_POST['selectOption'];

$dadosAlterar = trim($_POST["dadosAlterar"]);

if($dadosAlterar){ 
  $sql2 = "UPDATE dados SET $selectOption = '$dadosAlterar' WHERE id = '$id'";

  if ($mysqli->query($sql2) === TRUE) {   
    echo "Sucesso!";  
  }      
}
?>

my final code looked like this. Thank you all!

Browser other questions tagged

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