Введение в СУБД MySQL


Example.12.5


<?php

$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

if ($submit) {

// here if no ID then adding else we're editing

if ($id) {

$sql = "UPDATE employees SET first='$first',last='$last',

address='$address',position='$position'

WHERE id=$id";

} else {

$sql = "INSERT INTO employees (first,last,address,position)

VALUES

('$first','$last','$address','$position')";

}

// run SQL against the DB

$result = mysql_query($sql);

echo "Record updated/edited!

";

} else if ($delete) {

// delete a record

$sql = "DELETE FROM employees WHERE id=$id";

$result = mysql_query($sql);

echo "$sql Record deleted!

";

} else {

// this part happens if we don't press submit

if (!$id) {

// print the list if there is not editing

$result = mysql_query("SELECT * FROM employees",$db);

while ($myrow = mysql_fetch_array($result)) {

printf("%s %s \n", $PHP_SELF,

$myrow["id"], $myrow["first"],

$myrow["last"]);

printf("(DELETE)
",

$PHP_SELF, $myrow["id"]);

}

}

?>

">ADD A RECORD

<?php

if ($id)

{

// editing so select a record

$sql = "SELECT * FROM employees WHERE id=$id";

$result = mysql_query($sql);

$myrow = mysql_fetch_array($result);

$id = $myrow["id"];

$first = $myrow["first"];

$last = $myrow["last"];

$address = $myrow["address"];

$position = $myrow["position"];

// print the id for editing

?>

<?php

}

?>

First name:

Last name:

Address:

Position:

<?php

}

?>




Начало  Назад  Вперед