Update does not work in the database

2

Good folks, already made a topic here about this and resolved soon, but later I noticed that in arranging this problem I got another. Here's the code:

<?
error_reporting(0);
require('cdn/inc/header.php'); 

if(isset($_SESSION['user_data'])):
$user_level = $_SESSION['user_data']['level'];


switch($user_level):
case 1:
case 2: 
case 3: 
case 5:
case 8:
case 9:

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


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

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


 <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']; 

 $Title = $_POST['Title'];

  $queryupdate = "UPDATE testes SET Tile = :Title WHERE ID= :ID";   
 $q = $db->prepare($queryupdate);
 $q->execute(array(":Title" => $Title));
 header ('Location: edit.php');} 

 else {
 $stmt = $db->query("SELECT * FROM testes ORDER BY STR_TO_DATE(Date, '%d-%m-%y') ASC, Title ASC);
 $stmt->execute();
 $data = $stmt->fetchAll(PDO::FETCH_ASSOC);?>

 <div id="user-bar" style="background: #C16011;">
  <i>
   <a href="add.php">Adicionar</a>
  </i>
 </div>

 <br />
 <br />

 <?
 foreach($data as $index => $row) { 
 $className = $index % 2 == 0 ? "class" : "class-1" ?>

<form action="" name="Inser" method="post">
 <div class="selector">
  <input name="selector[]" class="selector" type="checkbox" value="<?php echo $row['ID']; ?>" />
 </div>

   <a href="edit.php?ID=<? echo $row['ID']; ?>"><? echo $row['Title']; ?></a>


   </div>
  </h6>
 </div>

<?
 }
 if(empty($data)){?>

 <div class="no-data">
  DATA/HORA: <b><?echo date("d-m-Y");?></b>/<b><? echo date("H:i");?></b>
 <br />
  SEM NADA
 </div>

<?}
else
{?>

 <div class="btn-padding">
  <div class="btn-group dropup pull-right">
   <button type="button" class="btn btn-primary">Seleciona uma acção</button>
   <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
    <span class="caret"></span>
    <span class="sr-only"></span>
   </button>
   <ul class="dropdown-menu dropdown-menu-right" role="menu">
    <li><a href="" class="selectall-button" onClick="return false;">Selecionar tudo / Nao selecionar</a></li>
    <li class="divider"></li>
    <li><a href="#" type="submit" class="delete-button"onclick="javascript:document.Insert.submit();">Eliminar</a></li>
   </ul>
  </div>
 </div>
</form>


 <?
 }
 $edittable=$_POST['selector'];
 $N = count($edittable);
 for($i=0; $i < $N; $i++)
 {
 $result = $db->prepare("DELETE FROM testes WHERE ID= :ID");
 $result->bindParam(':ID', $edittable[$i]);
 $result->execute();
 header ('Location: edit.php'); 
 }
 } 
?>

 <br />
 <br />

 <center>
  <small>TESTE.</small>
 </center>


</body>
</html>

<?
break;
 endswitch;
 else:
 header( 'Location: ../ ');
 endif;
?>

Here's how I opened another topic , that is, when I do submit does not update in the database, but if I change the elseif to if , when I go to edit the post it gets the list that I have in the table below of the edition.

Is there any way to resolve this?

    
asked by anonymous 24.04.2015 / 23:38

1 answer

1

When doing the update inform the two fields that are in it, title and ID . When testing the commented lines, then the error message is displayed and the page is not redirected.

 $queryupdate = "UPDATE testes SET Tile = :Title WHERE ID= :ID";   
 $q = $db->prepare($queryupdate);
 $q->execute(array(":Title" => $Title)); /// <------- está faltando o bind de :ID

Change the last line to:

 $q->execute(array(":Title" => $Title, ":ID" => $ID));
    
24.04.2015 / 23:50