2

Hola amigos buenas noches, tengo problemas con mi codigo ya que logre que funcionara, pero no consigo que modifique el registro en la base de datos.

<?php require_once('Connections/clientes.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']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE suscriptor SET numero=%s, cliente=%s, paquete=%s, oportuno=%s, lista=%s, elector=%s, serie1=%s, serie2=%s, serie3=%s WHERE numero=%s",
                       GetSQLValueString($_POST['numero'], "text"),
                       GetSQLValueString($_POST['cliente'], "text"),
                       GetSQLValueString($_POST['paquete'], "text"),
                       GetSQLValueString($_POST['oportuno'], "text"),
                       GetSQLValueString($_POST['lista'], "text"),
                       GetSQLValueString($_POST['elector'], "text"),
                       GetSQLValueString($_POST['serie1'], "text"),
                       GetSQLValueString($_POST['serie2'], "text"),
                       GetSQLValueString($_POST['serie3'], "text"));

  mysql_select_db($database_clientes, $clientes);


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

mysql_select_db($database_clientes, $clientes);
$valor = $_GET['numero'];
$query_Recordset1 = "SELECT * FROM suscriptor WHERE numero=$valor";
$Recordset1 = mysql_query($query_Recordset1, $clientes) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SGC - Sistema de gestion de clientes</title>
<style type="text/css">
body {margin:0;}
ul.topnav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

ul.topnav li {float: left;}

ul.topnav li a {
  display: inline-block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  transition: 0.3s;
  font-size: 17px;
}

li a.active {
    background-color: #4CAF50;
    color: white;
}

li a:hover:not(.active) {
    background-color: #555;
    color: white;
}

ul.topnav li a:hover {background-color: #111;}

ul.topnav li.icon {display: none;}

@media screen and (max-width:680px) {
  ul.topnav li:not(:first-child) {display: none;}
  ul.topnav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.topnav.responsive {position: relative;}
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}
body,td,th {
    font-family: Verdana, Geneva, sans-serif;
    font-size: 12px;
    text-align: center;
}
</style>
</head>

<body>
<ul class="topnav">
  <li><a href="index.php">Inicio</a></li>
  <li><a href="nvocliente.php">Ingresar registro</a></li>
  <li><a href="consult.php">Buscar registro</a></li>
  <li><a class="active" href="econsult.php">Editar registro</a></li>
  <li><a href="dconsult.php">Eliminar registro</a></li>
  <li><a href="edtcliente.php">Global registros</a></li>
  <li class="icon">
    <a href="javascript:void(0);" onclick="myFunction()">&#9776;</a>
  </li>
</ul>
<script>
function myFunction() {
    document.getElementsByClassName("topnav")[0].classList.toggle("responsive");
}
</script>
<h1>Modificar registro (No sirve)</h1>
<br>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">numero:</td>
      <td><?php echo $row_Recordset1['numero']; ?></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">cliente:</td>
      <td><input type="text" name="cliente" value="<?php echo $row_Recordset1['cliente']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">paquete:</td>
      <td><input type="text" name="paquete" value="<?php echo $row_Recordset1['paquete']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">oportuno:</td>
      <td><input type="text" name="oportuno" value="<?php echo $row_Recordset1['oportuno']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">lista:</td>
      <td><input type="text" name="lista" value="<?php echo $row_Recordset1['lista']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">elector:</td>
      <td><input type="text" name="elector" value="<?php echo $row_Recordset1['elector']; ?>" size="32"></td>
      <tr valign="baseline">
      <td nowrap align="right">serie1:</td>
      <td><input type="text" name="serie1" value="<?php echo $row_Recordset1['serie1']; ?>" size="32"></td>
      <tr valign="baseline">
      <td nowrap align="right">serie2:</td>
      <td><input type="text" name="serie2" value="<?php echo $row_Recordset1['serie2']; ?>" size="32"></td>
      <tr valign="baseline">
      <td nowrap align="right">serie3:</td>
      <td><input type="text" name="serie3" value="<?php echo $row_Recordset1['serie3']; ?>" size="32"></td>
    </tr>
    </tr>
    </tr>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Actualizar registro"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1">
  <input type="hidden" name="numero" value="<?php echo $row_Recordset1['numero']; ?>">
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>
10
  • tengo duda: ¿qué es sprintf?
    – user75901
    el 11 sep. 2018 a las 4:35
  • 4
    Tu código en varios puntos parece una re-invención de la rueda. Da la impresión de que haces un enorme esfuerzo por sanear los datos, y lo malo es que no terminan realmente saneados. Esta consulta: UPDATE suscriptor SET numero=%s, cliente=%s, paquete=%s, oportuno=%s, lista=%s, elector=%s, serie1=%s, serie2=%s, serie3=%s WHERE numero=%s sigue siendo altamente vulnerable a ataques de inyección SQL. Aquí lo que conviene es usar consultas preparadas (si es posible pasar a PDO o mysqli) y limpiar un poco el código de tantas cosas que son redundantes e inútiles (sin ánimo de ofender).
    – A. Cedano
    el 11 sep. 2018 a las 4:47
  • 4
    Las funciones que empiezan por mysql_ están declaradas obsoletas hace ya tiempo, se nota que aprendiste de un tutorial antiguo. Sería bueno que pases a PDO o a mysqli, que te permitirán usar consultas preparadas, que hacen con más facilidad, claridad y seguridad, funciones como lo que intentas en tus llamadas a GetSQLValueString. Además, ya no necesitarás para este caso hacer uso de funciones como addslashes. Tu código necesita una modernización urgente, no vale la pena que pierdas tiempo intentando hacerlo funcionar así como está.
    – A. Cedano
    el 11 sep. 2018 a las 5:01
  • 3
    Mira esta respuesta, se explica de forma sencilla, sin perderse en demasiados detalles, la forma en que funciona básicamente mysqli y también PDO. En este mismo sitio hay un gran número de preguntas sobre el tema, y si buscas en la red sobre PDO, mysqli o consultas preparadas, encontrarás mucha más información.
    – A. Cedano
    el 11 sep. 2018 a las 5:17
  • 2
    Aparte de apoyar lo que te ha comentado @A.Cedano, veo que construyes la cadena ($updateSQL) que actualiza los datos pero no la usas en ningún momento. Al igual que haces algún select más adelante y lo ejecutas, con el update debes hacer lo mismo el 11 sep. 2018 a las 5:49

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.