MYSQL PHPでの レコードの削除ができたりできなかったりする(エラーメッセージはない)DELETE文

実現したいこと

MySQLから引っ張ってきたテーブル表示(tableタグのtdタグ)の削除ボタンから該当レコードを削除したい。

前提

vscode、xamppを使っています。
MySQLから表示させたテーブルの表示の削除ボタンからレコードを削除する仕組みを作り、id=1のレコードを削除することに成功しましたが、
その後なぜコードを変えてないのに、削除出来たり出来なくなったりしました。もちろんスーパーリロードしています。
削除されていなくても、(index4.phpの削除ボタンを押すと、delete.phpに飛び、try-cacthで削除する仕組みですが、)
キャッチされずにエラーメッセージにならず削除しましたという表示になります。
phpmyadminからテーブルを確認しても削除されていません。
どこが間違っているか教えて頂けないでしょうか?

発生している問題・エラーメッセージ

なし

該当のソースコード

index4.php

1<?php 2// データベース接続 3try{ 4 $db = new PDO('mysql:dbname=crm;host=localhost;charset-utf8','root','your_password'); 5} catch(PDOException $e) { 6 echo '接続エラー:' . $e->getMessage(); 7 exit; 8} 9 10// //データベース取得 11$sql = 'select trueid, id, name, gender, score,standardDay,remind from list'; 12$rec = $db->prepare($sql); 13$rec->execute(); 14$rec_list = $rec->fetchAll(PDO::FETCH_ASSOC); 15 16 17//データベース切断 18$db = null; 19 20?> 21<!DOCTYPE html> 22<html lang="en"> 23<head> 24 <meta charset="UTF-8"> 25 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 26 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> 27 <!-- <script src="csv2.js"></script> --> 28 <title>Document</title> 29</head> 30<body> 31 <!-- CSVダウンロード --> 32<!-- <form action="index4.php" method="POST"> 33 <button name="csv_output" onclick="proc1();" type="submit">Download as CSV</button> 34</form> --> 35<br> 36 37<div class="navigation"> 38 <a href="index.php">データベース一覧ページ</a> 39</div> 40 41<table border="1" style="border-collapse: collapse" id="csv"> 42<tr> 43 <th>インクリメントID</th> 44 <th>ID</th> 45 <th>名前</th> 46 <th>性別</th> 47 <th>スコア</th> 48 <th>日付</th> 49 <th>スコア2</th> 50</tr> 51 52<!-- データベース表示 --> 53<?php foreach ($rec_list as $rec) { ?> 54<tr> 55<td><?php echo $rec['trueid'];?></td> 56<td><?php echo $rec['id'];?></td> 57<td><?php echo $rec['name'];?></td> 58<td><?php echo $rec['gender'];?></td> 59<td><?php echo $rec['score'];?></td> 60<td><?php echo $rec['standardDay'];?></td> 61<td><?php echo $rec['remind'];?></td> 62<!-- <td class="save"><form action="sent.php" method="post" onSubmit="CheckSave()"><input type="submit" value="保存"></form></td> 63<td class="delete"><form action="delete.php" method="post" onSubmit="CheckDelete()"><input type="submit" value="削除"></form></td> --> 64<!-- <td class="save"><a href="sent.phpid=" class="link_confirm1">保存</a></td> --> 65 <td class="delete"><a href="delete.phpid=" class="link_confirm">削除</a></td> 66</tr> 67<?php } ?> 68</table> 69<!-- <a href="#" id="download">CSVダウンロード</a> --> 70<script src="input.js"></script> 71<!-- <button id="csv">CSVでダウンロード</button> 72<script src="csv.js"></script> --> 73</body> 74</html> 75 76

delete.php

12<?php 3// データベース接続 4try{ 5 $db = new PDO('mysql:dbname=crm;host=localhost;charset-utf8','root','your_password'); 6 7 $stmt = $db->prepare('DELETE FROM list WHERE id = :id'); 8 9 $stmt->execute(array(':id' => $_GET["id"])); 10 11 echo "削除しました"; 12 13 14} catch(Exception $e) { 15 echo 'エラー発生:' . $e->getMessage(); 16 exit; 17} 18 19 20 21//データベース切断 22$db = null; 23 24?> 25<a href="index4.php">データベース一覧へ</a>

input.js

1$(function() { 2 3 $("tr td:nth-child(1)").addClass("Increments"); 4 // $("tr td:last-child").after('<td><form method="post" action="sent.php"><button class="save" value="1" type=submit>保存</button></form></td>'); 5 // $("tr td:last-child").after('<td><button class="save" value="1" type=submit>保存</button></td>'); 6 7 8 $('td').each(function(i){ 9 10 var text=$(this).text(); 11 $(this).attr('name',text); 12 $(this).attr('value',text); 13 14 }); 15 16 17 18 $('.delete a').each(function(i){ 19 $(this).attr('href','delete.php?id='+(i+1)); 20 21 }); 22 23 24 25 26}); 27 28 29 30 31document.addEventListener('dblclick',({target})=>{ 32 33if(target.classList.contains('Increments')||target.classList.contains('save')) 34 return; 35 36if(target.matches('#tb1 td')){ 37target.contentEditable=true; 38target.focus(); 39} 40}); 41document.addEventListener('keydown',e=>{ 42if(e.key=="Enter"){ 43 var text=e.target.innerText; 44 e.target.setAttribute('value', text) 45 e.target.blur(); 46} 47}); 48document.addEventListener('blur',({target})=>{ 49if(target instanceof HTMLElement && target.matches('[contentEditable]')){ 50 var text=target.innerText; 51 target.setAttribute('value', text) 52 target.removeAttribute('contentEditable'); 53} 54},true); 55 56 57 58// 削除ボタンダイアログ 59document.addEventListener('DOMContentLoaded', function() { 60 const linkConfirms = Array.prototype.slice.call(document.querySelectorAll('.link_confirm')); 61 linkConfirms.forEach(function(linkConfirm) { 62 linkConfirm.addEventListener('click', function(event) { 63 const resultConfirm = confirm('外部のサイトに移動します。よろしいですか?'); 64 if(!resultConfirm) { 65 event.preventDefault(); 66 } 67 }); 68 }); 69}, false);

試したこと

index4.phpの削除ボタンを押すと、delete.phpに飛ぶ前にダイアログが出るのでその影響かもと思いましたが、
ダイアログを出さないようにしても解決しませんでした。

コメントを投稿

0 コメント