You are on page 1of 39

Escuela Superior de Comercio N 49 Cap. Gral.

Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

Abrir una conexin


<?php
print "Opening the connection to the database server<br>";
$link = mysql_connect("localhost","root");
print "The connection worked. The link is: $link <br>";
print "Closing the connection<br>";
mysql_close( $link );
?>

Abrir una base de datos


<?php
print "Opening the connection to the database server<br>";
mysql_connect("localhost", "root");
print "Selecting a database<br>";
$result = mysql_select_db( "northwind" );
if ( $result ) {
print "Database selected successfully<br>";
}
else{
print "There was a problem with the database selection<br>";
}
?>

Ejecutar un Update
<html><head><title>MySQL Query in PHP</title></head>
<body bgcolor="lightblue">
<font size='+1' face='arial'>
<?php
print "Opening the connection to the database server<br>";
$link = mysql_connect("localhost", "root", "");
print "Selecting the 'northwind' database<br>";
mysql_select_db("northwind");
$query="UPDATE Shippers SET phone='(777) 430-2346'
WHERE CompanyName = 'Federal Shipping'";
$result= mysql_query($query);
$rows_affected=mysql_affected_rows($link); /* numbers of rows
affected by the most recently executed query */
echo "You have updated $rows_affected rows.<br>";
Programacin II Unidad 8 Bases de Datos - Pgina N: 194

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

if ($result){
print "Query successful!<br>";
}
else{
die("Query failed.". mysql_error());
}
print "Closing the connection<br>";
mysql_close( $link );
?>
</body>
</html>

Recuperar registros
<html><head><title>MySQL Fetch a Row</title></head>
<body bgcolor="lightblue">
<font size='+1' face='arial'>
<?php
print "Opening the connection to the database server<br>";
$link = mysql_connect("localhost", "root", "");
print "Selecting the 'northwind' database<br>";
mysql_select_db("northwind");
$query="SELECT CompanyName, Phone from Shippers";
$result_set= mysql_query($query);
if ($result_set){
print "Fetch the first row of data.<br>";
$record= mysql_fetch_row($result_set);
// print_r($record);
foreach ($record as $value){
print "<em>$value </em>"; // print the row
}
print "<br>";
}
else{ die("Query failed.". mysql_error());
}
print "Closing the connection<br>";
mysql_close( $link );
?>
Programacin II Unidad 8 Bases de Datos - Pgina N: 195

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

</body>
</html>

Cantidad de registros
<?php
mysql_connect("localhost", "root", "");
mysql_select_db( "northwind" );
print "Executing SQL...<br>";
$result_set = mysql_query( "SELECT ShipName FROM orders");
$num_rows = mysql_num_rows( $result_set );
print "<b>There are a total of $num_rows ship names
in the \"orders\" table.";
?>

Mostrar registros en tablas de HTML


<?php
mysql_connect("localhost", "root");
mysql_select_db( "northwind" );
print "Executing SQL...<br>";
$result_set = mysql_query( "SELECT * FROM customers");
print "<table>";
// Print the headers
for( $c=0; $c<mysql_num_fields( $result_set ); $c++ ) {
print "<th>". mysql_field_name( $result_set, $c ) .
"</th>";
}
// print all the rows
while( $record = mysql_fetch_row( $result_set ) ) {
print "<tr>";
for( $c=0; $c<mysql_num_fields( $result_set ); $c++ ) {
print "<td>". $record[$c] ."</td>";
}
print "</tr>";
}
print "</table>";
?>

Galera de Arte
Programacin II Unidad 8 Bases de Datos - Pgina N: 196

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

contact_us.php
<?php
extract($_REQUEST);
if( isset($submit) ) {
$status = "";
$success = true;
$message = "";
foreach( $_REQUEST as $key=>$value ) {
if( $value=="" ) {
$status .= "Please enter your $key.<br>";
$success = false;
}
$message .= "$key\t=\t$value\r\n";
}
if ( $success ) {
mail("manager@thecanvasgallery.com", "Request for contact", $message, "From: $email" );
}
}
?>
<html>
<body>
<h1>Request for contact</h1>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">
<table>
<? if( !$success ) { ?>
<tr><td colspan="2"><font color="red"><?=$status?><br></font></td></tr>
<? } else { ?>
<tr><td colspan="2"><font color="blue">Your message has been sent. Thank you!<br></font></td></tr>
<? } ?>
<tr><td>Name</td><td><input type="text" name="name"/></td></tr>
<tr><td>Email</td><td><input type="text" name="email"/></td></tr>
<tr><td>Phone</td><td><input type="text" name="phone"/></td></tr>
<tr><td colspan="2"><textarea rows="5" name="message"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit"/></td></tr>
</table>
</form>
</body>
</html>

Programacin II Unidad 8 Bases de Datos - Pgina N: 197

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

admin_art_edit.php
<?php
extract($_REQUEST);
mysql_connect("localhost", "root", "password")
mysql_select_db("test") or die(mysql_error());

or die(mysql_error());

if( isset($submit) ) {
$status = "";
if( $title=="" ) {
$status = "Please enter the art title.<br>";
} else {
// Connect to the database and insert the new artist
if( isset( $id) && $id!="" ) {
$sql = "UPDATE Art SET title='$title', " .
"price='$price',
description='$description',
image='$image',
artist_id='$artist_id'" .
"WHERE id='$id'";
} else {
$sql = "INSERT INTO Art (title, price, description, image, artist_id ) ".
"VALUES ('$title', '$price', '$description', '$image', '$artist_id' )";
}
mysql_query( $sql )

or die(mysql_error());

$status = "SUCCESSFULLY updated $title";


}
} elseif ( isset($id) ) {
$sql = "SELECT Art.title, Art.description, Art.price,
Art.image, Art.artist_id
FROM Art, Artist WHERE Art.artist_id=Artist.id AND
Art.id='$id'";
$resultset = mysql_query( $sql )
or die(mysql_error());
$row = mysql_fetch_assoc( $resultset );
extract( $row );
} else {
$id=""; $title=""; $description=""; $price=""; $image=""; $artist_id=0;
}
?>
<? include("admin_header.php") ?>
<h1>Art Update Screen</h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="get">
Programacin II Unidad 8 Bases de Datos - Pgina N: 198

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<input type="hidden" name="id" value="<?=$id?>">


<table>
<? if (isset($status)) {?>
<tr><td colspan="2"><b><?=$status?></b><br><br></td></tr>
<? } ?>
<tr><td>Title</td><td><input type="text" name="title" value="<?=$title?>" /></td></tr>
<tr><td>Artist</td>
<td><select name="artist_id">
<option value="">Please Select</option>
<?php
$sql = "SELECT * FROM Artist";
$resultset = mysql_query( $sql )
or die(mysql_error());
while( $artist = mysql_fetch_assoc( $resultset ) ) {
if ( $artist['id'] == $artist_id ) $check=" selected=\"SELECTED\" "; else
$check="";
print '<option value="' . $artist['id'] . "\" $check >" . $artist['name'] .
"</option>\n";
}
?>
</select></td>
</tr>
<tr><td>Description</td><td><textarea rows="10" cols="60" name="description"
value="<?=$description?>" ><?=$description?></textarea></td></tr>
<tr><td>Price</td><td><input type="text" name="price" value="<?=$price?>" /></td></tr>
<tr><td>Image</td><td><input type="text" name="image" value="<?=$image?>" />
<small>(Path to the file relative to the root of the website)</small></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit"/></td></tr>
</table>
</form>
<? include("admin_footer.php") ?>

admin_artist_insert.php
<?php
extract($_REQUEST);
if( isset($submit) ) {
$status = "";
if( $name=="" ) {
$status = "Please enter the artist's name.<br>";
} else {
// Connect to the database and insert the new artist
$sql = "INSERT INTO Artist (name, email, phone, bio)" .
Programacin II Unidad 8 Bases de Datos - Pgina N: 199

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

"VALUES ('$name', '$email', '$phone', '$bio')";


mysql_connect("localhost", "root", "password")

or

die(mysql_error());
mysql_select_db("test")
or die(mysql_error());
mysql_query( $sql )
$status = "SUCCESSFULLY inserted $name";

or die(mysql_error());

}
}
?>
<? include("admin_header.php") ?>
<h1>Artist Insert Screen</h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="get">
<table>
<? if (isset($status)) {?>
<tr><td colspan="2"><b><?=$status?></b><br><br></td></tr>
<? } ?>
<tr><td>Name</td><td><input type="text" name="name"/></td></tr>
<tr><td>Email</td><td><input type="text" name="email"/></td></tr>
<tr><td>Phone</td><td><input type="text" name="phone"/></td></tr>
<tr><td>Bio</td><td><textarea rows="15" cols="60" name="bio"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit"/></td></tr>
</table>
</form>
<? include("admin_footer.php") ?>

admin_artist_edit.php
<?php
extract($_REQUEST);
mysql_connect("localhost", "root", "password")
mysql_select_db("test")
or die(mysql_error());

or die(mysql_error());

if( isset($submit) ) {
$status = "";
if( $name=="" ) {
$status = "Please enter the artist's name.<br>";
} else {
// Connect to the database and insert the new artist
Programacin II Unidad 8 Bases de Datos - Pgina N: 200

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

$sql = "UPDATE Artist SET name='$name', " .


"email='$email', phone='$phone', bio='$bio'" .
"WHERE id='$id'";
mysql_query( $sql )

or die(mysql_error());

$status = "SUCCESSFULLY updated $name";


}
} else {
$sql = "SELECT * from Artist WHERE id='$id'";
$resultset = mysql_query( $sql )
or die(mysql_error());
$row = mysql_fetch_assoc( $resultset );
extract( $row );
}
?>
<? include("admin_header.php") ?>
<h1>Artist Update Screen</h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="get">
<input type="hidden" name="id" value="<?=$id?>">
<table>
<? if (isset($status)) {?>
<tr><td colspan="2"><b><?=$status?></b><br><br></td></tr>
<? } ?>
<tr><td>Name</td><td><input type="text" name="name" value="<?=$name?>" /></td></tr>
<tr><td>Email</td><td><input type="text" name="email" value="<?=$email?>" /></td></tr>
<tr><td>Phone</td><td><input type="text" name="phone" value="<?=$phone?>" /></td></tr>
<tr><td>Bio</td><td><textarea
rows="15"
cols="60"
name="bio"
value="<?=$bio?>"
><?=$bio?></textarea></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Submit"/></td></tr>
</table>
</form>
<? include("admin_footer.php") ?>

artist_detail.php
<?php
extract($_REQUEST);
mysql_connect("localhost", "root", "password")
mysql_select_db("test")
or die(mysql_error());

or die(mysql_error());

Programacin II Unidad 8 Bases de Datos - Pgina N: 201

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

$sql = "SELECT * from Artist WHERE id='$id'";


$resultset = mysql_query( $sql )
or die(mysql_error());
$row = mysql_fetch_assoc( $resultset );
extract( $row );
?>
<? include("header.php") ?>
<h1><?=$name?></h1>
<table width="100%">
<tr><td colspan="2"><?=$bio?><br><br></td></tr>
<tr><td>Email <a href="mailto:<?=$email?>"><?=$email?></a></td></tr>
<tr><td>Phone <?=$phone?></td></tr>
<tr><td colspan="2"><hr><br></td></tr>
<?
$sql = "SELECT Art.id, Art.title, Art.price, Art.image, Art.description, Artist.name ".
"FROM Art Art, Artist Artist " .
"WHERE Artist.id='$id' AND Artist.id=Art.artist_id";
$recordset = mysql_query( $sql )
or die(mysql_error());
?>
<?
while( $row=mysql_fetch_assoc($recordset) ) {
print "<tr><td><h2>".$row['title']."</h2>\$". number_format($row['price'],2);
print "<br>" .$row['description'];
// Display the image if one exists
if ( isset($row['image']) && $row['image'] != "" ) {
print '<br><br><img src="'. $row['image'] . '" align="center">';
}
print "<br><br><hr></td></tr>\n";
}
?>
</table>
<? include("footer.php") ?>

admin_header.php
<?php
session_start();
// Check if the user is logged in
if( !isset($_SESSION['authorized']) || $_SESSION['authorized'] != 'yes' ) {
Programacin II Unidad 8 Bases de Datos - Pgina N: 202

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

header( "Location: admin_login.php" );


exit();
}
?>
<html>
<head><title>Art Gallery Administration</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body align="center">
<img src="images/CanvasLogo-plain.gif"><br>
<h1>Art Gallery Administration</h1>
[<a href="index.php">HOME</a>]
[<a href="admin_artist_list.php">Artist List</a>]
[<a href="admin_artist_insert.php">New Artist</a>]
[<a href="admin_art_list.php">Art List</a>]
[<a href="admin_art_edit.php">New Art</a>]
<div align="center">
<hr><br>

footer.php
<br><br>
<table width="100%" bgcolor="#000000">
<tr>
<td class="orange" width="33%"><div align="left">1200 9th Ave, San
Francisco</div></td>
<td
class="orange"
width="33%"><div
align="right">www.TheCanvasGallery.com</div></td>
<td class="orange" width="34%"><div align="center">Phone (415)5040060</div></td>
</tr>
</table><br><br>
</font></td>
</tr>
</table>
</html>

index.php
<?php
// Connect to the database and insert the new artist
$sql = "SELECT * FROM Artist ORDER BY name";
mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
Programacin II Unidad 8 Bases de Datos - Pgina N: 203

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

$recordset = mysql_query( $sql ) or die(mysql_error());


function art_for_artist( $artist_id ) {
$sql = "SELECT * FROM Art WHERE artist_id='$artist_id' LIMIT 1;";
$recordset = mysql_query( $sql ) or die(mysql_error());
$recordset = mysql_query( $sql ) or die(mysql_error());
$row = mysql_fetch_assoc( $recordset);
return $row['image'];
}
?>
<? include("header.php") ?>
<h1>Welcome To The Canvas Gallery</h1>
<p> Welcome to The Canvas Gallery. Please choose an artist from the list below to view the details. </p>
<table cellpadding="15">
<?
while( $row=mysql_fetch_assoc($recordset) ) {
$image = art_for_artist( $row["id"] );
print '<tr><td><a href="artist_detail.php?id='. $row['id'].
'"><img src="'.$image.'" width="150" border="0"><br></td>';
print '<td><strong><a href="artist_detail.php?id='. $row["id"]. '">'
.$row["name"] .'</a></strong><br>' .
$row["phone"] ."<br><a href=\"mailto:\"".
$row["email"] ."\">". $row["email"] ."</a>\n";
print "</td></tr>\n";
}
?>
</table>
<? include("footer.php") ?>

admin_login.php
<?php
extract($_REQUEST);
if( isset($login) && $login=="admin" &&
isset($password) && $password="guess" ) {
session_start();
$_SESSION['authorized'] = 'yes';
header( "Location: admin_artist_list.php" );
exit();
}
Programacin II Unidad 8 Bases de Datos - Pgina N: 204

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

?>
<html>
<head><title>Art Gallery Administration</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body align="center">
<img src="images/CanvasLogo-plain.gif"><br>
<h1>Art Gallery Administration</h1>
<center>
<h1>Please Login</h1>
<small>(Use 'admin' and 'guess' for login and password)<br><br></small>
<form>
<table>
<tr><td>Login:</td><td><input type="text" name="login"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password"></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" value="Login"></td></tr>
</table>
</form>
</center>
</html>

header.php
<html>
<head>
<title>The Canvas Gallery, San Francisco Art Gallery, Music Club, Bar, and Restaurant, Open Mic, Poetry,
Film</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table cellSpacing=0 cellPadding=0 align=center border=0 borderColor="#CCCCCC" width="563">
<tr>
<td><br><IMG src="images/header.jpg" border="0">
<div align="left">
[<a href="index.php">HOME</a>]
[<a href="contact.php">Contact Us</a>]
[<a href="admin_artist_list.php">Administration</a>]
</div>
</td>
</tr>

Programacin II Unidad 8 Bases de Datos - Pgina N: 205

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<tr>
<td width="100%"><font face="Verdana, Arial, Helvetica, sans-serif"><br>

admin_art_list.php
<?php
// Connect to the database and insert the new artist
$sql = "SELECT Art.id, Art.title, Art.price, Artist.name FROM Art Art, Artist Artist " .
"WHERE Artist.id=Art.artist_id ORDER BY Artist.name";
mysql_connect("localhost", "root", "password")
or die(mysql_error());
mysql_select_db("test")
or die(mysql_error());
$recordset = mysql_query( $sql )
or die(mysql_error());
?>
<? include("admin_header.php") ?>
<h1>Art List</h1>
<table cellpadding="5">
<th>Art</th><th>Artist</th><th>Price</th>
<?
while( $row=mysql_fetch_assoc($recordset) ) {
print '<tr><td><a href="admin_art_edit.php?id='. $row["id"]. '">' .$row["title"] .'</a></td>' .
"<td>". $row["name"] ."</td><td><div align=right>$".
number_format($row["price"],2) ."</div></td></tr>\n";
}
?>
</table>
<? include("admin_footer.php") ?>

admin_footer.php
<br><hr>
</div>
</body>
</html>

admin_artist_list.php
<?php
// Connect to the database and insert the new artist
mysql_connect("localhost", "root", "password")
mysql_select_db("test")
or die(mysql_error());

or die(mysql_error());

Programacin II Unidad 8 Bases de Datos - Pgina N: 206

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

extract($_REQUEST);
if( isset($action) && $action="delete" ) {
$sql = "DELETE FROM Artist WHERE id='$id'";
mysql_query( $sql )
or die(mysql_error());
}
$sql = "SELECT * FROM Artist ORDER BY name";
$recordset = mysql_query( $sql )
or die(mysql_error());
?>
<? include("admin_header.php") ?>
<h1>Artist List</h1>
<table cellpadding="5">
<th>Artist</th><th>Phone</th><th>Email</th><th>Action</th>
<?
while( $row=mysql_fetch_assoc($recordset) ) {
print '<tr><td><a href="admin_artist_edit.php?id='. $row["id"]. '">' .$row["name"] .'</a></td>' .
"<td>". $row["phone"] ."</td><td>". $row["email"] ."</td>".
'<td><a
href="admin_artist_list.php?action=delete&id='.
$row["id"]."\">Delete</a></td></tr>\n";
}
?>
</table>
<? include("admin_footer.php") ?>

contact.php
<?php
extract($_REQUEST);
if(isset($submit)) {
$body = "\r\nContact Requested:\r\n\r\n $Name\r\n $Phone\r\n $Email\r\n $Message\r\n";
mail( "public@marakana.com", "Contact Form", $body, "From: form@TheCanvasGallery.com" );
mail( $Email, "Contact Form", $body, "From: form@TheCanvasGallery.com" );
$status = '<br><strong><font color="red">Your message has been sent. Thank
you!</font><br><br>';
}
?>
<? include("header.php") ?>
<h1>Contact Us</h1>
Programacin II Unidad 8 Bases de Datos - Pgina N: 207

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<?=$status?>
<p> Please fill our your contact information and we will contact you within 24 hours. </p>
<form>
<table cellpadding="5">
<tr><td>Name</td><td><input type="text" name="Name"></td></tr>
<tr><td>Phone</td><td><input type="text" name="Phone"></td></tr>
<tr><td>Email</td><td><input type="text" name="Email"></td></tr>
<tr><td>Message</td><td><textarea name="Message" rows="5"></textarea></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" name="submit" value="Send"></td></tr>
</table>
</form>
<? include("footer.php") ?>

Empleados
Ej. Emp_Add_Row.html
<html>
<head>
<title>Formulario de Ingreso de Empleados</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>&nbsp;</p>
<form name="ABMEmp" method="post" action="Emp_add_row.php">
<table width="287" border="0" align="center" cellpadding="3">
<tr bgcolor="#FFFFFF">
<td colspan="2">
<div align="center"><b>Introduzca los datos del empleado</b></div>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
<div align="right">Cdigo </div>
</td>
<td width="162">
<input type="text" name="Empno" size="5">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
Programacin II Unidad 8 Bases de Datos - Pgina N: 208

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<div align="right">Nombre</div>
</td>
<td width="162">
<input type="text" name="Ename" size="25">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
<div align="right">Puesto</div>
</td>
<td width="162">
<input type="text" name="Job" size="25">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
<div align="right">Jefe</div>
</td>
<td width="162">
<input type="text" name="Mgr" size="5">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
<div align="right">Fecha Ing.</div>
</td>
<td width="162">
<input type="text" name="Hiredate" size="15">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
<div align="right">Sueldo</div>
</td>
<td width="162">
<input type="text" name="Sal" size="15">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
<div align="right">Comisin</div>
</td>
<td width="162">
<input type="text" name="Comm" size="15">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="107">
<div align="right">Departamento</div>
Programacin II Unidad 8 Bases de Datos - Pgina N: 209

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

</td>
<td width="162">
<input type="text" name="Deptno" size="5">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2">
<div align="center">
<input type="submit" name="Submit" value="Enviar">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>

Ej. Emp_Add_Row.php

Programacin II Unidad 8 Bases de Datos - Pgina N: 210

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<html>
<head>
<title>Alta de Empleados</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>&nbsp;</p>
<?php
extract($_REQUEST);
$con = mysql_connect("localhost","root","")
or die("No se ha podido establecer la conexin con el servidor!");
$resp = mysql_select_db("practica")
or die("No se ha podido seleccionar la base de datos!");
$sqlquery = "INSERT INTO Emp VALUES(". $Empno .",'". $Ename . "','".
$Job . "'," . $Mgr . ",'" . $Hiredate . "'," . $Sal . "," . $Comm . "," . $Deptno . ")";
$queryresult = mysql_query($sqlquery) or die("No se puede ejecutar la sentencia insert!");
echo "<table border=1 align=center width=500>";
echo " <tr> ";
echo " <td> ";
echo " Cdigo";
echo " </td>";
echo " <td>".$Empno. "</td>";
echo " </tr>";
echo " <tr> ";
echo " <td > ";
echo "
Nombre";
echo " </td>";
echo " <td >".$Ename. "</td>";
echo " </tr>";
echo " <tr> ";
echo " <td > ";
echo "
Puesto";
echo " </td>";
echo " <td>".$Job. "</td>";
echo " </tr>";
echo " <tr> ";
echo " <td > ";
echo " Jefe";
echo " </td>";
echo " <td>".$Mgr. "</td>";
echo " </tr>";
echo " <tr> ";
echo " <td > ";
echo "
Fecha Ing";
echo " </td>";
echo " <td>".$Hiredate. "</td>";
Programacin II Unidad 8 Bases de Datos - Pgina N: 211

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

echo " </tr>";


echo " <tr> ";
echo " <td > ";
echo "
Sueldo";
echo " </td>";
echo " <td>".$Sal. "</td>";
echo " </tr>";
echo " <tr> ";
echo " <td > ";
echo "
Comisin";
echo " </td>";
echo " <td>".$Comm. "</td>";
echo " </tr>";
echo " <tr> ";
echo " <td > ";
echo "
Departamento";
echo " </td>";
echo " <td>".$Deptno. "</td>";
echo " </tr>";
echo "</table>";
?>
</body>
</html>

Programacin II Unidad 8 Bases de Datos - Pgina N: 212

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

Ej. Emp_db_browser.php
<html>
<head>
<title>Explorador de registros de Empleados</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?php
extract($_REQUEST);
$con = mysql_connect("localhost","root","")
or die("No se ha podido establecer la conexin con el servidor!");
$resp = mysql_select_db("practica")
or die("No se ha podido seleccionar la base de datos!");
$consultasql = "SELECT * from emp";
$resultadosql = mysql_query($consultasql);
Programacin II Unidad 8 Bases de Datos - Pgina N: 213

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

$nro_registros = mysql_num_rows($resultadosql);
print "Su consulta devuelve $nro_registros registros <br>\n";
echo "<table width=900 border=1 align=center>";
echo " <tr>";
for ($i=0; $i < mysql_num_fields($resultadosql); $i++)
{
$longitud = mysql_field_len($resultadosql, $i);
echo " <td width=$longitud> <center><b>".
mysql_field_name($resultadosql,$i) ."</b></center></td>\n";
}
echo " <td width=100> <center><b>Accin</b></center></td>\n";
echo " </tr>\n";
while ($fila=mysql_fetch_array($resultadosql))
{
echo " <tr>\n";
echo " <td>".$fila["EMPNO"]."</td>\n";
echo " <td>".$fila["ENAME"]."</td>\n";
echo " <td>".$fila["JOB"]."</td>\n";
echo " <td>".$fila["MGR"]."</td>\n";
echo " <td>".$fila["HIREDATE"]."</td>\n";
echo " <td>".$fila["SAL"]."</td>\n";
echo " <td>".$fila["COMM"]."</td>\n";
echo " <td>".$fila["DEPTNO"]."</td>\n";
echo <td><A
href=\"Emp_display.php?Empno=".$fila["EMPNO"]."\">Editar</a></td>\n";
echo " </tr>\n";
}
echo "</table>\n";
mysql_close( $con );
?>
</body>
</html>

Programacin II Unidad 8 Bases de Datos - Pgina N: 214

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

Ej. Emp_display.php
<html>
<head>
<title>Formulario de actualizacin de empleados</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="maildata" method="post" action="Emp_changes.php">
<table width="250" border="1" align="center">
<?php
extract($_REQUEST);
$con = mysql_connect("localhost","root","")
or die("No se ha podido establecer la conexin con el servidor!");
$resp = mysql_select_db("practica")
or die("No se ha podido seleccionar la base de datos!");
$consultasql = "SELECT * from emp where empno=" .$Empno;
Programacin II Unidad 8 Bases de Datos - Pgina N: 215

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

$resultadosql = mysql_query($consultasql);
if($row=mysql_fetch_array($resultadosql))
{
echo "<tr> ";
echo " <td> Cdigo </td>";
echo " <td width=\"150\"> ". $row["EMPNO"] . " </td>";
echo " <td> <input type=\"hidden\" Name=\"Empno\"
value=\"".$row["EMPNO"]."\" ></td>";
echo "</tr>";
echo "<tr> ";
echo " <td> Nombre </td>";
echo " <td>";
echo " <input type=\"text\" Name=\"Ename\"
value=\"".$row["ENAME"]."\" >";
echo " </td> ";
echo "</tr>";
echo " <tr> ";
echo " <td> Puesto </td>";
echo " <td> ";
echo " <input type=\"text\" Name=\"Job\"
value=\"".$row["JOB"]."\" >";
echo " </td>";
echo "</tr>";
echo " <tr> ";
echo " <td> Jefe </td>";
echo " <td> ";
echo " <input type=\"text\" Name=\"Mgr\"
value=\"".$row["MGR"]."\" >";
echo " </td>";
echo "</tr>";
echo " <tr> ";
echo " <td> Fecha Ing </td>";
echo " <td> ";
echo " <input type=\"text\" Name=\"Hiredate\"
value=\"".$row["HIREDATE"]."\" >";
echo " </td>";
echo "</tr>";
echo " <tr> ";
echo " <td> Sueldo </td>";
echo " <td> ";
echo " <input type=\"text\" Name=\"Sal\"
value=\"".$row["SAL"]."\" >";
echo " </td>";
echo "</tr>";
echo " <tr> ";
echo " <td> Comisin </td>";
echo " <td> ";
Programacin II Unidad 8 Bases de Datos - Pgina N: 216

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

echo " <input type=\"text\" Name=\"Comm\"


value=\"".$row["COMM"]."\" >";
echo " </td>";
echo "</tr>";
echo " <tr> ";
echo " <td> Departamento </td>";
echo " <td> ";
echo " <input type=\"text\" Name=\"Deptno\"
value=\"".$row["DEPTNO"]."\" >";
echo " </td>";
echo "</tr>";
echo "<tr> ";
echo " <td> ";
echo " <center> ";
echo " <input type=\"submit\" name=\"Submit\"
value=\"Enviar\">";
echo " </center>";
echo " </td>";
echo "</tr>";
}
?>
</table>
</form>
</body>
</html>

Programacin II Unidad 8 Bases de Datos - Pgina N: 217

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

Ej. Emp_changes.php
<html>
<head>
<title>Modificacin de Empleados</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>&nbsp;</p>
<?php
extract($_REQUEST);
$con = mysql_connect("localhost","root","")
or die("No se ha podido establecer la conexin con el servidor!");
$resp = mysql_select_db("practica")
or die("No se ha podido seleccionar la base de datos!");
$consultasql = "UPDATE emp SET ename=\"" . $Ename . "\", job=\"" . $Job . "\" , mgr=" .
$Mgr .
Programacin II Unidad 8 Bases de Datos - Pgina N: 218

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

", hiredate=\"". $Hiredate . "\", sal=" . $Sal . ", comm=" . $Comm . ", deptno=" . $Deptno .
" where empno=".$Empno;
echo $consultasql . "<br>\n";
$result = mysql_query($consultasql)
or die("No es posible realizar la consulta SQL");
echo "El registro se ha actualizado con xito.";
?>
</body>
</html>

Programacin II Unidad 8 Bases de Datos - Pgina N: 219

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

conectar.php
<?php
//En el filezilla:
//Servidor: t24m06.labs.escuelaurquiza.edu.ar
//Usuario: ftp_t24m06
//Contrasea: la misma del papel
//Guardarlo en el directorio web
//Programa para listar.
//Se ejecuta desde el explorer:
//http://t24m06.labs.escuelaurquiza.edu.ar/listado.php
//$conexion = mysql_connect("localhost","dbu_t24m06","mcn87wa");
$conexion = mysql_connect("localhost","root","");
//Seleccionar una base de datos MySQL
//$labase = mysql_select_db("db_t24m06",$conexion);
$labase = mysql_select_db("practica",$conexion);
?>

listado.php
<?php
include 'conectar.php';
//Enviar una consulta MySQL
//http://ar2.php.net/manual/es/function.mysql-query.php
$result = mysql_query("select DEPTNO Numero, DNAME Nombre, LOC Localidad from dept");
if (!$result) {
//Cierra la conexin de MySQL
//http://ar2.php.net/manual/es/function.mysql-close.php
mysql_close();
die("no se obtuvieron datos");
}
//Obtiene el nmero de campos en un resultado
//http://ar2.php.net/manual/es/function.mysql-num-fields.php
$cant_campos = mysql_num_fields($result);
print "<table border='1'><tr>";
//Cabecera de la tabla
Programacin II Unidad 8 Bases de Datos - Pgina N: 220

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

for ($i=0;$i<$cant_campos;$i++){
$un_campo = mysql_fetch_field($result);
print "<td>{$un_campo->name}</td>";
}
print "</tr>";
print "<tr>";
//Filas de la tabla
while($fila = mysql_fetch_row($result))
{
print "<tr>";
foreach ($fila as $campo )
print "<td>$campo</td>";
print "</tr>";
}
print "</table>";
//Libera la memoria del resultado
//ar2.php.net/manual/es/function.mysql-free-result.php
mysql_free_result($result);
//Cierra la conexin de MySQL
//http://ar2.php.net/manual/es/function.mysql-close.php
mysql_close();
?>

Programacin II Unidad 8 Bases de Datos - Pgina N: 221

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

alta.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="alta.php" method="post" name="formulario" target="_parent" id="formulario">
<table width="354" border="1">
<tr>
<td width="115" align="right">Numero:</td>
<td width="223"><input name="txt_deptno" type="text" id="txt_deptno"></td>
</tr>
<tr>
<td align="right">Nombre:</td>
<td><input type="text" name="txt_dname" id="txt_dname"></td>
</tr>
<tr>
<td align="right">Localidad:</td>
<td><input type="text" name="txt_loc" id="txt_loc"></td>
</tr>
</table>
<p>
<input type="submit" name="btn_enviar" id="btn_enviar" value="Enviar">
</p>
</form>
</body>
Programacin II Unidad 8 Bases de Datos - Pgina N: 222

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

</html>

alta.php
<?php
extract($_REQUEST);
include 'conectar.php';
//Arma la instruccin SQL
$campos = "deptno, dname, loc";
$valores = $txt_deptno . "," . "'" . $txt_dname . "','" . $txt_loc . "'";
$sqlinsert = "insert into dept (" . $campos . ") values (" . $valores . ")";
//Enviar una consulta MySQL
//http://ar2.php.net/manual/es/function.mysql-query.php
mysql_query($sqlinsert);
print "SQL ejecutado: " .$sqlinsert . "<br>";
print "Registros agregados: " . mysql_affected_rows();
//Cierra la conexin de MySQL
//http://ar2.php.net/manual/es/function.mysql-close.php
mysql_close();

Programacin II Unidad 8 Bases de Datos - Pgina N: 223

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

?>

modificar.html
Programacin II Unidad 8 Bases de Datos - Pgina N: 224

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="modificar.php" method="post" name="formulario" target="_parent" id="formulario">
<table width="354" border="1">
<tr>
<td width="115" align="right">Numero:</td>
<td width="223"><input name="txt_deptno" type="text" id="txt_deptno"></td>
</tr>
</table>
<p>
<input type="submit" name="btn_enviar" id="btn_enviar" value="Enviar">
</p>
</form>
</body>
</html>

modificar.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
Programacin II Unidad 8 Bases de Datos - Pgina N: 225

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<body>
<?php
extract($_REQUEST);
include 'conectar.php';
//Enviar una consulta MySQL
//http://ar2.php.net/manual/es/function.mysql-query.php
$result = mysql_query("select * from dept where deptno=$txt_deptno") or die("No se ejecut");
//Recupera una fila de resultado como un array asociativo, un array numrico o como ambos
//http://ar2.php.net/manual/es/function.mysql-fetch-array.php
$result_array=mysql_fetch_array($result);
?>
<form action="modificar2.php" method="post" name="formulario" target="_parent" id="formulario">
<input name="deptno" type="hidden" value="<?php print $txt_deptno ?>"/>
<table width="354" border="1">
<tr>
<td width="115" align="right">Nombre:</td>
<td width="223"><input name="txt_dname" type="text" id="txt_dname" value="<?php print
$result_array['DNAME'] ?>"></td>
</tr>
<tr>
<td align="right">Localidad:</td>
<td><input type="text" name="txt_loc" id="txt_loc" value="<?php print $result_array['LOC'] ?>"></td>
</tr>
</table>
<p>
<input type="submit" name="btn_enviar" id="btn_enviar" value="Enviar">
</p>
<?php
//Libera la memoria del resultado
//ar2.php.net/manual/es/function.mysql-free-result.php
mysql_free_result($result);
//Cierra la conexin de MySQL
//http://ar2.php.net/manual/es/function.mysql-close.php
mysql_close();
?>
</form>
</body>
</html>
Programacin II Unidad 8 Bases de Datos - Pgina N: 226

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

Programacin II Unidad 8 Bases de Datos - Pgina N: 227

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

modificar2.php
<?php
extract($_REQUEST);
include 'conectar.php';
//Arma la instruccin SQL
$sqlupdate = "update dept set "
. "DNAME='" . $txt_dname
. "',LOC='" . $txt_loc
. "' where DEPTNO=" . $deptno;
//Enviar una consulta MySQL
//http://ar2.php.net/manual/es/function.mysql-query.php
mysql_query($sqlupdate);
print "SQL ejecutado: " . $sqlupdate . "<br>";
print "Registros agregados: " . mysql_affected_rows();
//Cierra la conexin de MySQL
//http://ar2.php.net/manual/es/function.mysql-close.php
mysql_close();
?>

Programacin II Unidad 8 Bases de Datos - Pgina N: 228

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

baja.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
Programacin II Unidad 8 Bases de Datos - Pgina N: 229

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="baja.php" method="post" name="formulario" target="_parent" id="formulario">
<table width="354" border="1">
<tr>
<td width="115" align="right">Numero:</td>
<td width="223"><input name="txt_deptno" type="text" id="txt_deptno"></td>
</tr>
</table>
<p>
<input type="submit" name="btn_enviar" id="btn_enviar" value="Enviar">
</p>
</form>
</body>
</html>

Programacin II Unidad 8 Bases de Datos - Pgina N: 230

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

baja.php
<?php
extract($_REQUEST);
if ($txt_deptno != ""){
include 'conectar.php';
$sqldelete = "delete from dept where deptno=$txt_deptno";
//Enviar una consulta MySQL
//http://ar2.php.net/manual/es/function.mysql-query.php
mysql_query($sqldelete);
print $sqldelete . "<br>";
print "Registros eliminados: " . mysql_affected_rows();
//Cierra la conexin de MySQL
//http://ar2.php.net/manual/es/function.mysql-close.php
mysql_close();
}
?>

Programacin II Unidad 8 Bases de Datos - Pgina N: 231

Escuela Superior de Comercio N 49 Cap. Gral. Justo Jos de Urquiza


Carrera Analista de Sistemas de Computacin Nivel Superior

Programacin II Unidad 8 Bases de Datos - Pgina N: 232

You might also like