PHP bulletin board deletion function

Asked 2 years ago, Updated 2 years ago, 47 views

I am currently creating a bulletin board, but I cannot implement the deletion function.
I think the sql conditions and the basic form configuration are not good.

It's going to be long, so I've omitted a part of it.
If you don't have enough information, please point it out.

index.php

<body>
<divid="wrap">
  <divid="head">
    <h1> Bulletin Board </h1>
  </div>
  <divid="content">
      <div>
      <label for="view_name">Display name</label>
      <input id="view_name" type="text" name="view_name" value="<?php if(!empty($_SESSION['view_name']){echo$_SESSION['view_name']);}?>">
      </div><br>
      <form action="index.php" method="post" id="form"><!--javascript:void(0)-->
          <textarea name="message" id="message" cols="70" rows="15" placeholder="Please leave a note">/textarea><br>
          <input type="submit" value="write" id="write">
      </form>
      <!--
      <form id="form_1" method="post" accept-charset="utf-8" return false>
          <p>Name<input type="text" name="userid" id="userid"></p>
          <p>Password<input type="text" name="password" id="password"></p>
      </form>
      <button id="ajax">ajax</button>
      </div>
      -->
     <hr>
    <hr>
  </div>
  <?php
    $prin=$db->query("SELECT* FROM tb/*ORDER BY BAN DESC*/");
    while($fet=$prin->fetch()):
      echo "ID: ".nl2br($fet['id']). "<br>";
      echonl2br($fet['mes'])."<br>";

      // Display Name Input Check
      if(empty($_POST['view_name']){
        $error_message[]='Enter a display name.';
      } else{
        $clean['view_name'] = h($_POST['view_name'], ENT_QUOTES);

        // Save Display Name to Session
        $_SESSION['view_name'] = $clean['view_name'];
      }

  ?>
    <!--- pop-up
    <divid="popup" style="width:200px;display:none;padding:30px30px;border:2px solid#000;margin:auto;">
      Are you sure you want to delete it?<br/>
      <button id="ok" onclick="okfunc()">Delete</button>
      <button id="no" onclick="nofunc()">Cancel</button>
    </div>
    -->
    <divvid="functions">
      <divid="bottons" style="display:inline-flex">
        <form action="delete.php" method="post">
          <input type="submit" value="delete" id="delete">
        </form>
      </div>

delete.php

<?php
session_start();
require('dbconnect.php');

$id = $_POST ['id'];
$del=$db->prepare("DELETE FROM tb WHERE id=$id");// The conditions here are not working well
$del->execute();

header('Location:index.php'); exit();
?>

php sql

2022-09-30 10:23

1 Answers

1, prepare, execute is used differently.There are examples of how to specify "prepar SQL statements" using named parameters and "prepar SQL statements using question mark parameters."

$del=$db->prepare("DELETE FROM tb WHERE id=?");
$del->execute(array($id)));

2.Delete I want to delete There is no input tag to specify the post article ID ($_POST['id']), so I cannot delete it because the post article ID is not sent to the server.

Please check the link below for details.
https://www.php.net/manual/ja/pdo.prepare.php

Additional
It seems to have been solved with GET parameters, but I would like you to complete your original intention
Add a sample script to post in jquery

<?php
if(isset($_POST['id'])){
    echo intval($_POST['id']); exit;
};
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <metacharset="UTF-8">
    <title>Title> /title>
    <script type="text/javascript" src="//code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body>
<script>
    function send_post_id(pid){
        $.post("a.php", {id:pid}).done(function(html){alert(html);});
    }
</script>
<a href="#"onclick="send_post_id('12345')">POST send</a>
</body>
</html>

Save it as a.php when you actually try it.


2022-09-30 10:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.