简体   繁体   中英

Trying to add a Delete to my table (mysql/pdo/php)

I'am trying to make a delete button to my table so i can easily remove items from the list with just pressing the delete button. but i cant figure out how to do it so i would like to ask if someone cud help me

Here's my code atm index.php

 ?php include 'connection.php' ?> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="css/style.css"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- <link href="css/bootstrap.css" rel="stylesheet" type="text/css"> --> <link href="css/bootstrap-3.3.7.css" rel="stylesheet" type="text/css"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <div class="container"></div> <div class="row"> <div id="header" class="header col-md-12"> <h1 class="page-header" id="header">Invertering</h1> </div> </div> <!--Input --> <div class="row"> <div class="col-md-12"> <form method="post" id="input" > <input type="text" name="namn" id="tasklabel" placeholder="namn"/> <input type="text" name="antal" id="tasklabel" placeholder="antal"/> <input type="text" name="lagerplats" id="tasklabel" placeholder="lagerplats"/> <br /> <input id="submit" type="submit" value="Lägg till"> <br /> </form> </div> </div> <div class="row"> <div class="col-md-12"> <form method="post" id="input" action="delete.php" > <input id="test" type="text" name="id" id="tasklabel" placeholder="Id att ta bort"/> <br /> <input id="submit" type="submit" value="Ta bort"> <br /> </form> </div> </div> <div class="row"> <div id="table" class="col-md-12"> <li><a href="delete.php?id=<?php echo $row['id']; ?>">Delete</a></li> <?php echo "<table style='border: solid 1px black; margin: auto; width: 50%; padding: 10px; text-align:center;'>"; echo "<tr><th style='text-align:center; border: solid 1px black;'>Namn</th><th style='text-align:center; border: solid 1px black;'>Antal</th><th style='text-align:center; border: solid 1px black;'>Lagerplats</th><th style='text-align:center; border: solid 1px black;'>Id</th><th style='text-align:center; border: solid 1px black;'>Ta bort</th></tr>"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "<td style='width:150px; text-align:center; border: solid 1px black;'>" . parent::current(). "</td>"; } function beginChildren() { echo "<tr>"; } function endChildren() { echo "</tr>" . "\\n"; } } try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT namn, antal, lagerplats, id FROM inventory"); $stmt->execute(); $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo "</table>"; ?> </div> </div> </body> </html> 

then here is my delete function

 <?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "inventory"; $id=$_POST['id']; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // sql to delete a record $sql = "DELETE FROM inventory WHERE id=$id"; // use exec() because no results are returned $conn->exec($sql); echo "Record deleted successfully"; echo "<html><meta http-equiv=\\"refresh\\" content=\\"1;URL='http://localhost/invertering/index.php'\\"><p>Please wait 1 seconds...</p></html>"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?> 

And here is the connection

 <?php $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "inventory"; $namn=$_POST['namn']; $antal=$_POST['antal']; $lagerplats=$_POST['lagerplats']; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } ?> <?php if ($namn == null) { print "<script>alert('var god och skriv något...')</script>"; } else { $sql = "INSERT INTO inventory (namn, antal, lagerplats) VALUES ('$namn', '$antal', '$lagerplats')"; $conn->exec($sql); } ?> <?php $conn = null; ?> 

Ps: i know the code are bad but iam doing my best atm

Though I haven't seen your code in depth, but the way you are trying to implement looks fine.

Just call some function using javascript on button click and get the response. If delete successful then either hide that entry or refresh the entire table, whichever is more convenient.

If error recieved, then show an error message using a popup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM