Thema: Verbindung aufnehmen, Datensätze löschen


Datensätze abfragen und löschen

MySQL

In einem PHP-Programm können die Datensätze mit der Anweisung delete gelöscht werden.
Normalerweise muss die Bearbeitung eines Datensatzes (erzeugen, löschen oder löschen) mit Hilfe eines Benutzerrechtes erlaubt werden. Der Einfachheit halber soll es hier aber angenommen werden, dass jeder Benutzer Datensätze löschen kann. Das Formular und der Code werden hier unten zusammengefasst:

Datensätze löschen

<?php
  	/* Verbindung mit MyQSL aufnehmen */
	mysql_connect("localhost", "YourUsername", "YourPassword");
	$dblink =mysql_select_db("YourDB");
  if (isset($_POST["delete"])) {
       $sqlstr = "delete from lab_tblpersonen where id = " . $_POST["oripn"];
      $res = mysql_query($sqlstr) or die ("keine Verbindung mit der Datenbank ".mysql_error());
      $num = mysql_affected_rows();
      if ($num>0) {
         echo "<p><font color='#999999'>";
         echo "Der Datensatz wurde gel&ouml;scht.";
         echo "</font></p>";
      } else {
         echo "<p><font color='#993300'>";
	      echo "Der Datensatz wurde nicht gel&ouml;scht.";
	      echo "</font></p>";
      }
      mysql_free_result($res);
  }
  if (isset($_POST["auswahl"])) {
     $sqlstr = "select * from lab_tblpersonen where id = " . $_POST["auswahl"];
     $res = mysql_query($sqlstr);
     $dsatz = mysql_fetch_assoc($res); ?>
 
     <form  class="mb-2" action="c57.php" method="post">
     <div class="form-group">
     			<label class="control-label">Id</label>
           <input type="text" name="id" value="<?php echo $dsatz["id"]; ?>"/>
           </div>
     <div class="form-group">
     			<label class="control-label">Nachname</label>
           <input type="text" name="nn" value="<?php echo $dsatz["name"]; ?>"/>
           </div>
     <div class="form-group">
     			<label class="control-label">Vorname</label>
           <input type="text" name="vn" value="<?php echo $dsatz["vorname"]; ?>"/>
           </div>
     <div class="form-group">
     			<label class="control-label">Personalnummer</label>
           <input type="text" name="pn" value="<?php echo $dsatz["personalnummer"]; ?>"/>
           </div>
     <div class="form-group">
     			<label class="control-label">Gehalt</label>
           <input type="text" name="ge" value="<?php echo $dsatz["gehalt"]; ?>"/>
           </div>
     <div class="form-group">
     			<label class="control-label">Zulage</label>
           <input type="text" name="zl" value="<?php echo $dsatz["zulage"]; ?>"/>
           </div>
     <div class="form-group">
     			<label class="control-label">Geburtstag</label>
           <input type="text" name="gt" value="<?php echo date('d.m.Y', strtotime($dsatz["geburtstag"])); ?>"/>
           </div>    
     <div class="form-group">
           <input type="hidden" name="oripn" value="<?php echo $_POST["auswahl"]; ?>"/>
           </div>

     <div class="form-group">
           <button type="submit" name="delete" class="btn btn-primary" >
           <span class="fa fa-send"></span> Delete</button>           
           </div>  
     </form><hr />
<?php     mysql_free_result($res);
   } ?>
   <p align="center"><b>W&auml;hlen Sie aus, welcher Datensatz gel&ouml;scht werden soll:</b>
   <form  action="c57.php" method="post">
<?php   $sqlstr = "select * from lab_tblpersonen";
   $res = mysql_query($sqlstr) or die ("keine Verbindung mit der Datenbank ".mysql_error());
   $num = mysql_num_rows($res);  ?>
	<table class="table striped">
	<thead>
	<tr><th>Lfd. Nr.</th><th>Name</th>
	<th>Vorname</th><th>Personalnummer</th>
	<th>Gehalt</th><th>Zulage</th><th>Geburtstag</th></tr>
	</thead><tbody>
 <?php	while ($dsatz = mysql_fetch_assoc($res)) { ?>
		<tr>
      <td><?php echo $dsatz["id"]; ?></td>
		<td><?php echo $dsatz["name"]; ?></td>
		<td><?php echo $dsatz["vorname"]; ?></td>
		<td><?php echo$dsatz["personalnummer"]; ?></td>
		<td><?php echo number_format($dsatz["gehalt"],2,",","."); ?></td>
		<td><?php echo number_format($dsatz["zulage"],2,",","."); ?></td>
		<td><?php echo $dsatz["geburtstag"]; ?></td>
		<td><input type="checkbox" name="auswahl" value="<?php echo $dsatz["id"]; ?>" /></td>
		</tr>      
<?php	} ?>
	</tbody></table>
	<label class="control-label"></label><button type="submit" class="btn btn-primary" >
           <span class="fa fa-send"></span> Data view</button>

	</form>	
<?php   
	mysql_free_result($res);
 
?>


Datensätze löschen

Wählen Sie aus, welcher Datensatz gelöscht werden soll:

Lfd. Nr.Name VornamePersonalnummer GehaltZulageGeburtstag
134 Mustermann Marie 123456 3.000,00 3,00 2001-03-06
90 Lazret Amine 0 4.567,00 456,00 0000-00-00
91 Bu Sh 202002455 2.200,00 220,00 1989-12-11
102 sj atj 999 9.999,99 456,00 2020-12-20
135 Mustermann Marie 123456 3.000,00 3,00 2003-11-05
104 Heinz Holler 2147483647 6.500,00 5.000,00 2030-11-00
64 Müller Maria 2110 3.600,00 200,00 2030-11-00
131 0 0,00 0,00 0000-00-00
118 Sweater Knit 630 5.000,00 420,00 2020-01-20
113 Müller Max 54 3.000,00 200,00 2020-07-20
132 peters 0 0,00 0,00 0000-00-00
117 Gödecke Britt 0 4.600,00 400,00 0000-00-00
127 Ortiz Diego 0 3.000,00 500,00 1997-02-13