Update does not work in database

Asked

Viewed 83 times

0

I have the following code and when I give Submit it does not update the ANYTHING 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');}
  • 1

    From what I understand of your code, your if will always return true if there are data in the table tests and will never enter the elseif to execute update, try to change this elseif by only if and do the submit of form again.

  • It worked, you can tell me why I can’t find the elseif? Answer with this and I give a right in your answer to get more points !

1 answer

2


The problem is in the first if who will always return true if there are data in the table tests and will never enter the elseif to execute update, try to change this elseif by only if and do the 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'])) {
  ...
}

Hence the update does not occur, change the elseif for 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'])) {
  ...
}
  • now I have another bug/bug..

Browser other questions tagged

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