Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Tutorial - Deleting the data in your database

php



+ Font mai mare | - Font mai mic



Tutorial - Deleting the data in your database  

Why would this be useful? If we had a database of employees, and one left, we don't need their details anymore, so we can delete



them from the database.

First of all, we need to set up a form that will ask us what we want to delete -

<html>

<body>

<?php

$db = pg_connect('dbname=friends');

$query = 'SELECT * FROM friends';

$result = pg_exec($db, $query);

if (!$result)

$numrows = pg_numrows($result);

$row=0;

printf ('<table border=1>');

printf ('<tr><td>ID</td><td>First Name</td><td>Surname</td></tr>');

do

while ($row < $numrows);

printf ('</table><br>');

pg_close();

?>

Just type in the ID number to delete.

<form action='delete.php' method='post'>

ID to Delete : <input type='text' name='id' size='4' length='4'

value=''><br>

<input type='submit' name='submit' value='Delete It'>

<input type='reset' name='reset' value='Clear the Form'>

</form>

</body>

</html>

This is almost the same form as we used for the previous example, with only the 'form action' changing, and we only

need to put in the ID number. Again, we're displaying the whole database just so it's easier for us to work out what we're going

to delete. Next, our delete script.

<html>

<body>

<?php

$db = pg_connect('dbname=friends');

$query = 'DELETE FROM friends where id='$id'';

$result = pg_exec($db, $query);

if (!$result)

printf ('Deleted from the database successfully');

pg_close();

?>

</body>

</html>

Pretty simple isn't it? We'll just look at our 'delete' command in a bit more detail.

$query = 'DELETE FROM friends where id='$id'';

The format of the command is pretty simple -

DELETE from table where table1=value1

It's pretty simple, we just tell it which ID to delete, and it deletes all of the information for that particular ID. If we

didn't tell it what ID to delete, it would delete everything, so make sure you give it an ID or something else to reference!

Note, this won't tell you that the ID doesn't exist. If you put in, for example, 99 for the ID, and hit submit, it would tell you

that it had successfully deleted it from the database.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 779
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved