C
C
Chesterfield252021-07-13 23:02:55
PHP
Chesterfield25, 2021-07-13 23:02:55

Why is the record update not happening?

In CRUD the application does not update records? It displays the data of the required record in the form, and when I change this record and try to save it, nothing changes!

Record Change Form

<tr>
<td><?=$value->id ?></td>
<td><?=$value->title; ?></td>
<td><a href="<?=$value->url; ?>" target="_blank"><?=$value->title ?></a></td>
<td><a href="?id=<?=$value->id; ?>" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#edit<?=$value->id; ?>"><i class="fa fa-edit"></i></a>
    <a href="" class="btn btn-danger"><i class="fa fa-trash-alt"></i></a></td>
</tr>

<!-- Modal edit start -->
<div class="modal fade" id="edit<?=$value->id; ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Edit</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
      <form action="" method="post">
          <div class="form-group">
            <small>Title</small>
            <input type="text" class="form-control" name="name" value="<?=$value->title; ?>" placeholder="Name">
          </div>
          <div class="form-group">
            <small>Url</small>
            <input type="text" class="form-control" name="url" value="<?=$value->url; ?>" placeholder="Url">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            <button type="submit" name="edit" class="btn btn-primary">Save</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<!-- Modal edit end -->

<?php } ?>


Create record request

// Create xf_type

$title = $_POST['name'];
$url = $_POST['url'];
$get_id_type = $_GET['id'];

if (isset($_POST['add'])) {
  $sql = ("INSERT INTO `xf_type` (`title`, `url`) VALUES (?,?)");
  $query1 = $pdo->prepare($sql);
  $query1->execute([$title, $url]);
  if($query1){
    header("Location: ". $_SERVER['HTTP_REFERER']);
  }
}


Record update request

// Update xf_type

if(isset($_POST['edit'])){
  $sql = ("UPDATE `xf_type` SET `title`=?, `url`=? WHERE `id`=?");
  $query1 = $pdo->prepare($sql);
  $query1->execute([$title, $url, $get_id_type]);
  if($query1){
    header("Location: ". $_SERVER['HTTP_REFERER']);
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2021-07-14
@SilenceOfWinter

when you add a post, you redirect without adding an id to the link

$id = $pdo->lastInsertId();
header('Location: '. filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_ENCODED) . '?id=' . $id);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question