Ver Mensaje Individual
  #2 (permalink)  
Antiguo 07/01/2003, 16:21
Avatar de BVis
BVis
 
Fecha de Ingreso: noviembre-2002
Ubicación: Barcelona
Mensajes: 104
Antigüedad: 21 años, 5 meses
Puntos: 0
Explicate un poco más...

Como conectas a la base de datos??

Mira, aquí te pego un ejemplo de conexión usando COM.

http://www.devshed.com/Server_Side/P...COM/page6.html

El código lo he mejorado un poquillo:

Código PHP:
<?php

// Esto lo hago para pasar las variables Globales de los arrays $_GET[]
// y $_POST[] a variables 'normales'.
foreach ($_REQUEST as $clave => $valor) {
  $
$clave $valor;
}

// Metemos toda la salida en un buffer
ob_start();

$DB_Conn = new COM("ADODB.Connection") or die("No puedo comenzar ADO");
$DB_Conn->Open("phpcom"); // Nombre del enlace que haremos.

// Ponemos valores por defecto
if(!isset($nextopid) && !isset($previousopid))
{
$nextopid "list";
$previousopid "";
}

// Si lo que queremos es borrar algo...
if($nextopid == "delete")
{
// Borramos el registro seleccionado
$Query "DELETE FROM coins WHERE id = $id";
$RS_Record $DB_Conn->Execute($Query);
// Vemos la lista de nuevo.
$previousopid=$nextopid;
$nextopid="list";
}

if(
$nextopid == "edit_process")
{
// Ejecutamos la consulta
$Query "UPDATE coins SET name = '".trim($name)."',country ='".trim($country)."',weight = ".trim($weight).",year = ".trim($year).",remarks = '".trim($remarks)."' WHERE id =$id";
$RS_Record $DB_Conn->Execute($Query);
// redirect browser back to list
$previousopid=$nextopid;
$nextopid="list";
// end of "edit" processor
}

if(
$nextopid == "add_process")
{
// open connection
// run query
$Query "INSERT INTO coins(name, country, weight, year,
remarks)
VALUES('"
.trim($name)."','".trim($country)."',".trim($weight).",".trim($year).",'".trim($remarks)."')";
$RS_Record $DB_Conn->Execute($Query);
// redirect browser to list
$nextopid="list";
$previousopid=$nextopid;
// end of "add" processor
}

// make a choice as to what function is needed
if($nextopid == "list")
{
// check to see what the previous operation was
// if null, do nothing
// if value exists, display an appropriate message
// to indicate the results of that operation
if(isset($previousopid) && $previousopid != "")
{
 switch (
$previousopid)
 {
 case 
"add_process":
      echo 
"<h3>Nuevo registro añadido</h3>";
      break;
 case 
"edit_process":
      echo 
"<h3>Registro actualizado</h3>";
      break;
 case 
"delete":
      echo 
"<h3>Registro borrado</h3>";
      break;
 default:
      echo 
"&nbsp;";
 }
}
?>
<h4 align="left">Coin Listing</h4>
<table border="2" cellpadding="5" cellspacing="1" width="90%">
  <tr>
      <th width="5%" align="right">#</th>
      <th width="20%" align="left">Name</th>
      <th width="20%" align="left">Country</th>
      <th width="15%" align="right">Weight <br>(in gms.)</th>
      <th width="5%" align="right" >Year</th>
      <th width="20%" align="left">Remarks</th>
      <th width="5%" align="right" >&nbsp;</th>
      <th width="5%" align="right" >&nbsp;</th>
  </tr>
<?php
// open up a connection to the database
// execute a query
$RS_Record $DB_Conn->Execute("SELECT * FROM coins");
// iterate through the recordset
while (!$RS_Record->EOF)
{
  
// get the field data into variables
  
$id $RS_Record->Fields('id');
  
$name $RS_Record->Fields('name');
  
$country $RS_Record->Fields('country');
  
$weight $RS_Record->Fields('weight');
  
$year $RS_Record->Fields('year');
  
$remarks $RS_Record->Fields('remarks');
  
?>
  <tr>
      <td width="5%" align="right"><?php echo $id->value?></td>
      <td width="20%" align="left"><?php echo $name->value?></td>
      <td width="20%" align="left"><?php echo $country->value?></td>
      <td width="5%" align="right"><?php echo $weight->value?></td>
      <td width="10%" align="right"><?php echo $year->value?></td>
      <td width="35%" align="left"><?php echo $remarks->value?></td>
      <td width="5%" align="right"><a href="<?php echo $_SERVER['PHP_SELF']."?nextopid=edit&previousopid=list&id=".$id->value;?>">Editar</a></td>
      <td width="5%" align="right"><a href="<?php echo $_SERVER['PHP_SELF']."?nextopid=delete&previousopid=list&id=".$id->value;?>">Eliminar</a></td>
  </tr>
  <?php
  
// go to the next record
  
$RS_Record->MoveNext();
}
?>
  <tr>
      <td colspan="8" align="right"><a href="<?php echo $_SERVER['PHP_SELF']."?nextopid=add&previousopid=list";?>">Añadir Nuevo</a></td>
  </tr>
</table>
<?php
// clean up
$RS_Record->Close();
$DB_Conn->Close();
$RS_Record null;
$DB_Conn null;
// end of "list" block
}

if(
$nextopid == "add" || ($nextopid == "edit" && isset($id)))
{
// code to display HTML form to add or modify records
?>
<h4 align="left"><?php echo ucfirst($nextopid)." Coin";
?></h4> <?php
if ($nextopid == "edit" && isset($id)){
// if this is an edit operation
// use the ID to retrieve the record from the table

// run query
$query "SELECT name, country, weight, year, remarks FROM coins where id = $id";
$RS_Record $DB_Conn->Execute($query);
// iterate through recordset
while (!$RS_Record->EOF)
{
$name $RS_Record->Fields('name');
$name $name->value;
$country $RS_Record->Fields('country');
$country $country->value;
$weight $RS_Record->Fields('weight');
$weight $weight->value;
$year $RS_Record->Fields('year');
$year $year->value;
$remarks $RS_Record->Fields('remarks');
$remarks $remarks->value;
$RS_Record->MoveNext();
}
// clean up
$RS_Record->Close();
$DB_Conn->Close();
$RS_Record null;
$DB_Conn null;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<?php if(isset($id)) {?><input type="hidden" name="id" value="<?php echo $id;?>"><?php ?>
<input type="hidden" name="previousopid" value="<?php echo $nextopid;?>">
<input type="hidden" name="nextopid" value="<?php echo $nextopid;?>_process">
<table border="2" cellpadding="10" cellspacing="1" width="90%">
  <tr>
      <th align="left">Coin characteristics</th>
      <th align="left"><?php echo ucfirst($nextopid); ?> Value</th>
  </tr>
  <tr>
      <td align="right">Name</td>
      <td align="left"><input type="text" name="name" size="30" maxlength="30" value="<?php if(isset($name)) echo trim($name);?>"></td>
  </tr>
  <tr>
      <td align="right">Country</td>
      <td align="left"><input type="text" name="country" size="30" maxlength="30" value="<?php if(isset($country)) echo trim($country);?>"></td>
  </tr>
  <tr>
      <td align="right">Weight (in gms)</td>
      <td align="left"><input type="text" name="weight" size="10" maxlength="10" value="<?php if(isset($weight)) echo trim($weight);?>"></td>
  </tr>
  <tr>
      <td align="right">Year</td>
      <td align="left"><input type="text" name="year" size="10" maxlength="10" value="<?php if(isset($year)) echo trim($year);?>"></td>
  </tr>
  <tr>
      <td align="right">Remarks</td>
      <td align="left"><textarea name="remarks" cols="40" rows="4"><?php if(isset($remarks)) echo trim($remarks);?></textarea></td>
  </tr>
  <tr>
      <td colspan="2" align="right"><input type="submit" value="<?php echo ucfirst($nextopid);?> Coin">&nbsp;&nbsp;<input type="reset" value="Clear above form"></td>
  </tr>
  <tr>
      <td colspan="2" align="right"><a href="<?php echo $_SERVER['PHP_SELF']."?nextopid=list"?>">Back to listing</a></td>
  </tr>
</table>
</form>
<?
// end of "add" or "edit" condition
}
// page footer
// display buffer contents
ob_end_flush();

?>
1 Saludo.
__________________
"Yo opino lo mismo que BVis, el de 'forosdelweb' " - Bill Gates