Contacto | #php_para_torpes | Enlázanos | ¿Quiénes somos?
24 usuarios Online (0)
Darse de alta en la web | Recuperar password   
Inicio / Foros / PHP / Restar Campos
4 respuestas recibidas | 55 visitas | Categoría PHP

Restar Campos

Avatar de Hamwe
Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1

(Nivel 1 - 3 posts)

#0 Offline Hamwe Usuario 28 abr 08 (12:49)  
Hola buenas estoy haciendo una web donde refleja las existencias del almacén. Tengo 2 campos que son Exmplrs(Ejemplares), rtrr(Retirar).Mi problema es que no se como tengo que hacer para que un usuario meta una cantidad en retirar y al aceptar el campo ejemplares se actualice restando la cantidad que se acaba de retirar.

Si alguién me puede ayudar se lo agradecería ya que lo necesito para el trabajo

Saludos

Os dejo el código:

Código PHP


<?php require_once('Connections/prueba.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch (
$theType) {
    case
"text":
      
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case
"long":
    case
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case
"double":
      
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case
"date":
      
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return
$theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "listadotlf.php" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  
$updateSQL = sprintf("UPDATE existencias SET ano_pblccn=%s, ttlo=%s, isbn=%s, pdf=%s, exmplrs=%s, ttl=%s WHERE id=%s",
                       
GetSQLValueString($_POST['ano_pblccn'], "int"),
                       
GetSQLValueString($_POST['ttlo'], "text"),
                       
GetSQLValueString($_POST['isbn'], "text"),
                       
GetSQLValueString($_POST['pdf'], "text"),
                       
GetSQLValueString($_POST['exmplrs'], "int"),
                       
GetSQLValueString($_POST['ttl'], "int"),
                       
GetSQLValueString($_POST['id'], "int"));

  
mysql_select_db($database_prueba, $prueba);
  
$Result1 = mysql_query($updateSQL, $prueba) or die(mysql_error());

  
$updateGoTo = "listadotlf.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    
$updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s", $updateGoTo));
}

if ((isset(
$_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
  
$updateSQL = sprintf("UPDATE existencias SET ano_pblccn=%s, ttlo=%s, isbn=%s, pdf=%s, exmplrs=%s, ttl=%s WHERE id=%s",
                       
GetSQLValueString($_POST['ano_pblccn'], "int"),
                       
GetSQLValueString($_POST['ttlo'], "text"),
                       
GetSQLValueString($_POST['isbn'], "text"),
                       
GetSQLValueString($_POST['pdf'], "text"),
                       
GetSQLValueString($_POST['exmplrs'], "int"),
                       
GetSQLValueString($_POST['ttl'], "int"),
                       
GetSQLValueString($_POST['id'], "int"));

  
mysql_select_db($database_prueba, $prueba);
  
$Result1 = mysql_query($updateSQL, $prueba) or die(mysql_error());

  
$updateGoTo = "listadotlf.php";
  if (isset(
$_SERVER['QUERY_STRING'])) {
    
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    
$updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  
header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_prueba, $prueba);
$query_rs_existencias = "SELECT existencias.id, existencias.ano_pblccn, existencias.ttlo, existencias.isbn, existencias.pdf, existencias.exmplrs, existencias.ttl, existencias.rtrr FROM existencias ORDER BY existencias.ano_pblccn";
$rs_existencias = mysql_query($query_rs_existencias, $prueba) or die(mysql_error());
$row_rs_existencias = mysql_fetch_assoc($rs_existencias);
$totalRows_rs_existencias = mysql_num_rows($rs_existencias);
?>
<html>
<head>
<title>Listado Existencias do almacén</title>
</head>

<body>
<br><br><br><form name="form1" method = "GET" action = "buscador.php">
<strong><center align>Búsqueda na base de datos:</strong>
<input type="text" name="buscar" size="20">
<input type="submit" value="buscar">
</form></center>
<p>&nbsp;</p>
   <p><strong>Insertar Novo rexistro </br>
   
<form method="POST" name="form" action="insertar2.php">
  <table align="">
    <tr valign="baseline">
      <td nowrap align="right">Ano Publicación:</td>
      <td><input type="text" name="ano_pblccn" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Título:</td>
      <td><input type="text" name="ttlo" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Isbn:</td>
      <td><input type="text" name="isbn" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Pdf:</td>
      <td><input type="text" name="pdf" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Exenplares:</td>
      <td><input type="text" name="exmplrs" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" name="accion" value="insertar"></td>
    </tr>
  </table>

</form>
   </strong></p>
   <?php do { ?>

  <table align=center width="90%" border="1" STYLE="table-layout:fixed">
    <tr>


      <td align=center width="4%"><font size="2px"><?php echo $row_rs_existencias['ano_pblccn']; ?>  </td>
      <td align=center width="25%"><font size="2px"><?php echo $row_rs_existencias['ttlo']; ?></td>
      <td align=center width="10%"><font size="2px"><?php echo $row_rs_existencias['isbn']; ?></td>
      <td align=center width="3%"><font size="1"><?php echo $row_rs_existencias['pdf']; ?></td>  
      <td align=center width="1%"> <font size="2px"><div align="center"><a href="formulario.php?id= <?php echo $row_rs_existencias['id']; ?>"><img src="actualizar.jpg" width="23" height="20" border="0"></a></div></td>
       <td align=center width="1%"> <font size="2px"><div align="center"> <script language="Javascript">
function preguntar(){
eliminar=confirm("¿Desexas eliminar este rexistro?");
if (eliminar)
//Redireccionamos si das a aceptar
window.location.href ="borra.php?id=%d"; //página web a la que te redirecciona si confirmas la eliminación
else
//Y aquí pon cualquier cosa que quieras que salga si le diste al boton de cancelar
alert('Non se ha puido eliminar o rexistro...')
}
</script>
       <a href="javascript:preguntar()"><img src="papelera.jpg" width="23" height="20" border="0"> </a></div></td>
             <td align=center width="3%"><font size="2px"><?php echo $row_rs_existencias['exmplrs']; ?></td>  
          <td align=center width="1%"><font size="2px"><?php echo $row_rs_existencias['rtrr']; ?></td>  
      <td align=center width="3%"><font size="2px"><?php echo $row_rs_existencias['ttl']; ?></td>
       </size>
    </tr>
  </table>
  
   
<?php } while ($row_rs_existencias = mysql_fetch_assoc($rs_existencias)); ?>

<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>

<?php
mysql_free_result
($rs_existencias);
?>

con update -

Avatar de jurena
Nivel 5 Nivel 5 Nivel 5 Nivel 5 Nivel 5 Nivel 5 Nivel 5

(Nivel 5 - 729 posts)

#1 Offline jurena Usuario 28 abr 08 (14:01)  
update nombretabla set datocampo=datocampo-datoarestar
naturalmente, puedes utilizar variables
igual que - puedes usar para sumar +

Re: Restar Campos

Avatar de Hamwe
Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1

(Nivel 1 - 3 posts)

#2 Offline Hamwe Usuario 28 abr 08 (14:12)  
Gracias por la respuesta,

y el update donde lo establezco? dentro de la tabla donde llamo a cada uno de los campos?

Re: Restar Campos

Avatar de Hamwe
Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1 Nivel 1

(Nivel 1 - 3 posts)

#3 Offline Hamwe Usuario 28 abr 08 (14:40)  
Lo del update ya sabía que es el código que debo utilizar lo que no se es donde lo tengo que situar para que si yo establezco en el campo retirar 2 articulos al actualizar estos sean restados del campo Ejemplares

Re: Restar Campos

Avatar de krowork
Nivel 3 Nivel 3 Nivel 3 Nivel 3 Nivel 3 Nivel 3 Nivel 3

(Nivel 3 - 199 posts)

#4 Offline krowork Usuario 29 abr 08 (11:00)  
Hola Hamwe,

podrías explicar un poco mejor el caso? Porque hay un campo rtrr en la BD? Es la acumulación de articulos que hay que retirar de ese articulo? Si todo fuera bien lo que haya en retirar debería ser restado de exemplares??? Desde cuando hay tiempos compuestos en gallego: 'Non se ha puido eliminar o rexistro...'??? :-P

http://krowork.blogspot.com

Responder mensaje

Para poder participar debes estar registrado e identificado. Si no estás registrado como usuario de PHP-Hispano, :: Registrar ::
Login / Password   

Web alojada en Zilos

php-hispano.net 2002 - 2008 | XHTML 1.0
Datos Legales | Webmaster