Update does not work in database

Asked

Viewed 111 times

2

Good staff, had already made a topic here about it and solved it right away, but then I noticed that when I fixed this problem I got another one. Here is 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 the another topic, I mean, when I do submit does not update the database, but if I change the elseif for if, when I go to edit the post is the list I have in the table below the edit.

Is there any way to fix this?

1 answer

1

When doing the update inform the two fields that are in it, title and ID. When performing tests review the lines of header() so 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));
  • I already did and the error remains, only from the refresh page but does not update anything

  • What error? could put the message

  • No error, is checking it now, just refresh and no message appears ..

  • put this on top: error_reporting(E_ALL);&#xA;ini_set('display_errors', 1); and even then I don’t get error message

  • @thecreator, maybe the problem is this line if(isset($_SESSION['user_data'])): of a $_SESSION scan or print_r

  • Array ( [loggedin] => 1 [username] => testuser1 [user_data] => Array ( [memberID] => 7 [username] => testuser1 [password] => $2y$10$t7q7XNa00.8ikOQ2lwZP7OF.1mlv4P9jlNvELuNWSSq0jacWIM.ou [email] => [email protected] [level] => 1 [active] => Yes [avatar] => [resetToken] => [resetComplete] => No [color] => CE9715 ) )

  • I decided to put if instead of ifelse and close the if after the submit

  • now I have another question, can go to chat?

  • @thecreator can speak

  • for example, I have a 'a href' that from Submit to form, as I get it to give Submit and to appear a confirmation message and only if I give ok is that it ends the accao?

  • @thecreator, you need to call a javascript function in href. What shows this message is the confirm()

Show 6 more comments

Browser other questions tagged

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