php - insert delete button of specific row from a database displayed in table -


hello have following code displaying database results, in 3th td of table insert delete button delete table record of data next button, should code in 3th td of tabe delete button ?

<?php $con=mysqli_connect("localhost","table","password","database"); if (mysqli_connect_errno()) {     echo "failed connect mysql: " . mysqli_connect_error(); } $result = mysqli_query($con,"select * recetas_galletas"); echo "<table border='1'> <tr>     <th>title</th>     <th>description</th> </tr>";  while($row = mysqli_fetch_array($result)) {     echo "<tr>";     echo "<td>" . $row['title'] . "</td>";     echo "<td>" . $row['description'] . "</td>";     echo "<td>" . delete button "</td>";     echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> 

there several ways archive this, first , and important need field on database table can identify record want delete, example primary key in form of id or unique key.

you can creating link text delete.php page or can use jquery , ajax or can use inner form.

you want have authorized users use pages need login page session well.

you can see here example of login page sessions.

the simplest 1 link delete page, see example here:

<?php $con = mysqli_connect("localhost","table","password","database"); // check connection if (mysqli_connect_errno()) {     die("failed connect mysql: " . mysqli_connect_error()); }  if (!$result = mysqli_query($con,"select * recetas_galletas")) {     die("error: " . mysqli_error($con)); } ?> <table border='1'> <tr> <th>title</th> <th>description</th> </tr> <?php while($row = mysqli_fetch_array($result)) { ?> <tr> <td><?php echo $row['title']; ?></td> <td><?php echo $row['description']; ?></td> <td><a href="delete.php?id=<?php echo $row['id']; ?>">delete</a></td> </tr> <?php } mysqli_close($con); ?> </table> 

then on delete page have this:

<?php // database info $db_host = ''; $db_user = ''; $db_pass = ''; $db_name = '';  if (!isset($_get['id'])) {     echo 'no id given...';     exit; }  $con = new mysqli($db_host, $db_user, $db_pass, $db_name); if ($con->connect_error) {     die('connect error (' . $con->connect_errno . ') ' . $con->connect_error); }  $sql = "delete recetas_galletas id = ?"; if (!$result = $con->prepare($sql)) {     die('query failed: (' . $con->errno . ') ' . $con->error); }  if (!$result->bind_param('i', $_get['id'])) {     die('binding parameters failed: (' . $result->errno . ') ' . $result->error); }  if (!$result->execute()) {     die('execute failed: (' . $result->errno . ') ' . $result->error); }  if ($result->affected_rows > 0) {     echo "the id deleted success."; } else {     echo "couldn't delete id."; } $result->close(); $con->close(); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -