Update does not work in the database

0

I have the following code and when I submit it it does not update the NADA in the database.

$query = "SELECT * FROM tests WHERE ID = :ID";
$result = $db->prepare($query);
$result->execute(array(':ID' => $_REQUEST['ID']) );
if ($row = $result->fetch(PDO::FETCH_ASSOC)) { 
?>


 <br />
 <br />

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 

  <input id="Game" type="text" name="Name" value="<?php echo $row['Name]; ?>" required />



 <br />

 <input type="hidden" name="ID" value="<?php echo $row['ID']; ?>" />
 <input type="Submit" name="Submit" value="Salvar alterações" />
</form>

<? 
 } 
 elseif (isset($_POST['Submit'])) {
 $ID = $_POST['ID']; 

 $Name = $_POST['Name'];

 $queryupdate = "UPDATE tests SET Name = :Name WHERE ID= :ID";  
 $q = $db->prepare($queryupdate);
 $q->execute(array(":ID" => $ID, ":Name" => $Name));
 header ('Location: edit.php');}
    
asked by anonymous 24.04.2015 / 21:18

1 answer

2
The problem is in the first if that will always return true if there is data in the tests table and will never enter elseif to execute update , try changing that elseif to only if e make submit of form again. See below:

Simplifying your code for better understanding.

/* Você faz um select na tabela e testa
 * se houverem dados da tabela, então faça
 */
if ($row = $result->fetch(PDO::FETCH_ASSOC)){
  ...
} 
//Aqui você testa senão houverem dados na tabela e se houver o post, então faça
elseif (isset($_POST['Submit'])) {
  ...
}

So update does not occur, change elseif to if :

//Se houverem dados da tabela, então
if ($row = $result->fetch(PDO::FETCH_ASSOC)){
  ...
} 
//Se houver o post, então
if (isset($_POST['Submit'])) {
  ...
}
    
24.04.2015 / 21:50